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.
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.
sandbox
property
¶
True once the client uses a Metrc sandbox credential (enter_sandbox).
hostname
property
¶
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.
secret_keys
property
¶
List, create, rotate and delete secret keys (SecretKeys).
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.
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
|
|
None
|
filter_logic
|
Literal['and', 'or'] | None
|
|
None
|
sort
|
str | None
|
|
None
|
mask
|
str | Sequence[str] | None
|
Fields to return ( |
None
|
include
|
str | Sequence[str] | None
|
Related data to attach, on super collections
( |
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).
states
¶
Every Metrc state, with requiresEmail and requiresOtp (cached).
enter_sandbox
¶
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 one of this credential's licenses in a terminal; returns the license number.
pick_endpoint
¶
Pick an endpoint from the spec in a terminal.