Skip to content

T3Client

T3Client is what connect() returns. Most scripts never build one directly. See the collections, writes and reports guides for worked examples.

T3Client

T3Client(*, auth: Auth | None = None, base_url: str = DEFAULT_HOST, license: str | None = None, profile: Profile | None = None, spec: SpecMode = 'auto', rate_limit: float | None = 10.0, concurrency: int = 10, timeout: float = DEFAULT_TIMEOUT, log_http: bool = False, transport: BaseTransport | None = None, verify: bool | str = True, proxy: str | None = None)

A configured connection to the T3 API.

Usually made by t3api_utils.connect. Use it as a context manager, or call close, to release its connection pool.

base_url instance-attribute

base_url = base_url.rstrip('/')

license instance-attribute

license = license

profile instance-attribute

profile = profile

concurrency instance-attribute

concurrency = concurrency

http instance-attribute

http: Client = build_client(base_url=self.base_url, auth=auth, timeout=timeout, rate_limit=rate_limit, log_http=log_http, transport=transport, verify=verify, proxy=proxy)

The underlying httpx2.Client: auth, base URL, retries and logging are already set. Use it for routes missing from the published spec.

credential_kind property

credential_kind: CredentialKind | None

"secret_key", "jwt" or None.

sandbox property

sandbox: bool

True once the client uses a Metrc sandbox credential (enter_sandbox).

hostname property

hostname: str | None

The Metrc hostname behind the credential, such as sandbox-me.metrc.com.

It comes from whoami, which is called the first time it's needed.

spec property

spec: Registry

The endpoint registry, loaded the first time it is needed.

secret_keys property

secret_keys: SecretKeys

List, create, rotate and delete secret keys (SecretKeys).

tag_lists property

tag_lists: TagLists

Create, fill and resolve tag lists (TagLists).

secret_key property

secret_key: str | None

The secret key in use, if the credential is one.

close

close() -> None

Close the connection pool. The client can't be used afterwards.

refresh_spec

refresh_spec() -> Registry

Fetch the live spec now, replacing the copy in use.

request

request(method: str, endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, json: Any = None, files: Any = None, data: Any = None, submit: bool | None = None, timeout: float | None = None, **path_params: Any) -> Any

Call any endpoint in the spec.

endpoint is an endpoint ID (METRC_ACTIVE_ITEMS) or a path (/v2/items or /v2/items/{object_id}). Path parameters are keyword arguments. license fills licenseNumber; it defaults to the client's license when the endpoint requires one.

Returns parsed JSON, bytes for other content, or None for an empty body.

get

get(endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, include: str | Sequence[str] | None = None, timeout: float | None = None, **path_params: Any) -> Any

GET an endpoint. object_id= picks the single-object route.

include= attaches related data on super endpoints, as in collect.

download

download(endpoint: str, to: str | Path, *, license: Any = None, params: Mapping[str, Any] | None = None, timeout: float | None = None, **path_params: Any) -> Path

Save a file endpoint (a manifest or COA PDF, an item photo) to to.

The response is streamed to a temporary file next to to and renamed when it's complete, so a failed download never leaves a partial file. Parent folders are created. Returns the path.

client.download("/v2/transfers/manifest", "manifests/0001234567.pdf",
                license="LIC-00001", params={"manifestNumber": "0001234567"})

post

post(endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, json: Any = None, files: Any = None, data: Any = None, submit: bool | None = None, dynamic: bool = False, timeout: float | None = None, **path_params: Any) -> WriteResult

POST. Routes that write to Metrc are a dry run unless submit=True.

Most Metrc write routes take a list, even for one object (json=[body]). dynamic=True asks T3 to run the route's dynamic checks as well (applyDynamicValidation). See the writes guide.

put

put(endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, json: Any = None, files: Any = None, data: Any = None, submit: bool | None = None, dynamic: bool = False, timeout: float | None = None, **path_params: Any) -> WriteResult

PUT. Routes that write to Metrc are a dry run unless submit=True.

Most Metrc write routes take a list, even for one object (json=[body]). dynamic=True asks T3 to run the route's dynamic checks as well (applyDynamicValidation). See the writes guide.

patch

patch(endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, json: Any = None, files: Any = None, data: Any = None, submit: bool | None = None, dynamic: bool = False, timeout: float | None = None, **path_params: Any) -> WriteResult

PATCH. Routes that write to Metrc are a dry run unless submit=True.

Most Metrc write routes take a list, even for one object (json=[body]). dynamic=True asks T3 to run the route's dynamic checks as well (applyDynamicValidation). See the writes guide.

delete

delete(endpoint: str, *, license: Any = None, params: Mapping[str, Any] | None = None, json: Any = None, submit: bool | None = None, dynamic: bool = False, timeout: float | None = None, **path_params: Any) -> WriteResult

DELETE. Routes that write to Metrc are a dry run unless submit=True.

collect

collect(endpoint: str, *, license: LicenseArg = None, filter: str | Sequence[str] | None = None, filter_logic: Literal['and', 'or'] | None = None, sort: str | None = None, mask: str | Sequence[str] | None = None, include: str | Sequence[str] | None = None, page_size: int | None = 100, progress: bool | None = None, params: Mapping[str, Any] | None = None, **path_params: Any) -> list[dict[str, Any]]

Load every page of a collection, in parallel, as one list.

Parameters:

Name Type Description Default
endpoint str

A collection's endpoint ID or path.

required
license LicenseArg

One license, or a list to load one after another. Defaults to the client's license.

None
filter str | Sequence[str] | None

field__op:value filters (one string or a list).

None
filter_logic Literal['and', 'or'] | None

"and" (default on the server) or "or".

None
sort str | None

field:asc or field:desc.

None
mask str | Sequence[str] | None

Fields to return (collectionMask).

None
include str | Sequence[str] | None

Related data to attach, on super collections (/v2/packages/active/super and so on): a list such as ["labResults", "history"]. Each value is sent as its own include parameter. Values the spec doesn't list are warned about.

None
page_size int | None

Rows per page.

100
progress bool | None

Show a progress bar. Defaults to on when stderr is a terminal.

None
params Mapping[str, Any] | None

Any other query parameters, by their spec names.

None

A page that still fails after retries fails the whole call.

iter_rows

iter_rows(endpoint: str, *, license: LicenseArg = None, filter: str | Sequence[str] | None = None, filter_logic: Literal['and', 'or'] | None = None, sort: str | None = None, mask: str | Sequence[str] | None = None, include: str | Sequence[str] | None = None, page_size: int | None = 100, params: Mapping[str, Any] | None = None, **path_params: Any) -> Iterator[dict[str, Any]]

Like collect, but yields rows one page at a time.

bulk

bulk(endpoint: str, rows: Iterable[Mapping[str, Any]], *, license: str | None = None, method: str | None = None, submit: bool | Literal['ask'] = False, confirm: bool = False, on_error: OnError = 'stop', log_to: str | Path | None = None, progress: bool | None = None, dynamic: bool = False, batch_size: int = 1, **path_params: Any) -> BulkResult

Validate every row with a dry run, then submit (see t3api_utils.writes.bulk).

report

report(endpoint: str, *, licenses: LicenseArg = None, format: str | None = None, to: str | Path | None = None, columns: str | Sequence[str] | None = None, filter: str | Sequence[str] | None = None, filter_logic: Literal['and', 'or'] | None = None, sort: str | None = None, row_limit: int | None = None, delivery: Literal['inline', 'email'] | None = None, email: str | None = None, params: Mapping[str, Any] | None = None) -> Any

Download a server-side report (see t3api_utils.reports.report).

report_url

report_url(endpoint: str, *, licenses: LicenseArg = None, format: str = 'csv', columns: str | Sequence[str] | None = None, filter: str | Sequence[str] | None = None, filter_logic: Literal['and', 'or'] | None = None, sort: str | None = None, row_limit: int | None = None, params: Mapping[str, Any] | None = None) -> str

A report URL with the secret key in it, for spreadsheets (see t3api_utils.reports.report_url).

whoami

whoami() -> dict[str, Any]

The authenticated user (GET /v2/auth/whoami).

licenses

licenses() -> list[dict[str, Any]]

Licenses this credential can use (GET /v2/licenses).

states

states() -> list[dict[str, Any]]

Every Metrc state, with requiresEmail and requiresOtp (cached).

enter_sandbox

enter_sandbox() -> None

Swap the credential for a Metrc sandbox JWT (POST /v2/auth/sandbox).

It also clears license: the sandbox has its own licenses, and it answers a request for a production license with no rows rather than an error. Then it calls whoami and logs the sandbox hostname at INFO.

pick_license

pick_license() -> str

Pick one of this credential's licenses in a terminal; returns the license number.

pick_endpoint

pick_endpoint(kind: Kind | None = 'collection') -> Endpoint

Pick an endpoint from the spec in a terminal.