Getting started¶
This page walks through the whole library in the order you'd use it: log in, connect, pick a license, load data, look at it, write it back, and use the server's reports and tag lists. Each step links to a guide page with the details.
The examples use LIC-00001 as the license number. Use one of yours.
1. Run scripts with uv¶
uv can install a script's dependencies for you. Put this header at the top of the file:
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = ["t3api-utils[tui]>=2.0"]
# ///
import t3api_utils as t3
client = t3.connect()
print(client.whoami()["username"])
and run it with:
uv builds a throwaway environment with t3api-utils in it. The [tui] extra
is only needed for the inspector in step 7; drop it otherwise. If you work in a
uv project instead, uv add t3api-utils and run scripts with uv run as
usual.
2. Log in and save a secret key¶
The T3 API accepts two kinds of credential:
| Credential | Lasts | Use it for |
|---|---|---|
| Secret key | Until you delete it, or until the Metrc login behind it changes | Everything. This is the default. |
| JWT | 24 hours | A quick session when you don't want a key |
A secret key is made from your Metrc login once. After that T3 signs in to Metrc for you, so scripts never need your password. Create one with:
(uv run t3api login inside a uv project, or
uvx --from t3api-utils t3api login with nothing installed.)
It asks:
- Do you already have a T3 secret key? If you do, paste it and you're done.
- Metrc hostname, username and password. Type a state abbreviation such as
CA, or a hostname such asca.metrc.com. States that need an email or a one-time password are asked for them. - Create a secret key so you don't have to log in again? Say yes. In states with a one-time password, you'll be asked for your OTP seed: the base32 secret behind your authenticator app, not a 6-digit code. T3 needs the seed because it signs in to Metrc for you later.
- Pick a default license for this profile? Optional, but it saves passing
license=everywhere.
The key goes into your OS keyring (macOS Keychain, Windows Credential Manager,
or Secret Service on Linux) under a profile named default. Your Metrc
password is not stored.
If you decline the secret key, t3api login saves a JWT instead. It expires
after 24 hours, and the next connect() in a terminal asks you to log in again
and offers the key again.
You can also create a key in the browser at https://api.trackandtrace.tools/v2/pages/secret-key and paste it in step 1.
More in Authentication.
3. Connect¶
connect() looks for a credential in this order and stops at the first one it
finds:
connect(secret_key=...)orconnect(jwt=...)- The
T3_SECRET_KEYorT3_JWTenvironment variable - The saved profile
- The login flow from step 2, but only if a person is there to answer (a
terminal or Jupyter). Otherwise it raises
AuthRequiredwith setup instructions, so scheduled jobs fail fast instead of hanging.
It then checks the credential with one GET /v2/auth/whoami call.
Use the client as a context manager so its connections close when the script is done:
Several Metrc logins? Save each to its own profile and pick one by name:
4. Licenses¶
Most endpoints need a license number.
In a terminal, client.pick_license() shows them as a numbered table and
returns the number you choose. A common pattern is to use the profile's default
license when there is one, and ask otherwise:
client.license is the default license: connect(license=...), then
T3_LICENSE, then the profile's. Set the profile's with
t3api license pick or t3api license set LIC-00001. When there is a default,
calls that need a license use it, and you can leave license= out.
5. Load a collection¶
collect() loads every page of a collection, fetching pages in parallel:
packages = client.collect("METRC_ACTIVE_PACKAGES", license=lic)
print(len(packages), packages[0]["label"])
Each row is a plain dict, exactly as the API returns it. Every row carries its
own licenseNumber.
Let the server do the narrowing. Filters use field__operator:value, sorting
uses field:asc or field:desc, and mask returns only the fields you name:
flower = client.collect(
"METRC_ACTIVE_ITEMS",
license=lic,
filter=["name__contains:flower"],
sort="name:asc",
mask=["id", "name", "productCategoryName"],
)
license also takes a list, which loads each license in turn and joins the
results:
For collections too big to hold in memory, iter_rows() takes the same
arguments and yields rows one page at a time:
One object by its Metrc ID:
More in Collections.
6. Find endpoints¶
METRC_ACTIVE_ITEMS is a T3 endpoint ID. Any path from the spec works too:
client.collect("/v2/items", license=lic) is the same call. To find one,
search from the command line:
or from Python:
In a terminal, client.pick_endpoint() shows a searchable table of
collections. The library reads the T3 OpenAPI spec at runtime, so endpoints T3
adds later work without upgrading. More in
Endpoints and the spec.
7. Inspect rows¶
With the [tui] extra installed, browse rows in a full-screen terminal viewer:
Use Left and Right to move between rows, / to search every
nested value, ? for help and Q or Esc to quit. The same
viewer opens files from the command line: t3api inspect packages.csv.
8. Filter against a CSV and export¶
filter_rows() keeps the rows that match a CSV file. The CSV's header names
the columns to compare:
Lines in the CSV that match no row are logged as a warning
(on_unmatched="error" raises instead). Dotted columns such as item.name
reach into nested fields.
Export to CSV or JSON:
CSV files are written as UTF-8 with a byte-order mark so Excel opens them
correctly. Nested objects become dotted columns (item.name) and lists are
written as JSON text. Leave the path out and the file is named after the data
model, license and time. More in Local data.
9. Write to Metrc¶
Writes that reach Metrc are a dry run unless you pass submit=True. A dry
run sends the request to T3, which checks its format, required fields and types
and stops before Metrc. It doesn't check that the records you name exist:
result = client.post("/v2/items/discontinue", license=lic, json={"id": 12345})
print(result.dry_run, result.message) # True Dry run
When the dry run passes, send it for real:
result = client.post("/v2/items/discontinue", license=lic, json={"id": 12345}, submit=True)
print(result.dry_run, result.message) # False Success
A validation failure raises t3.ValidationError with the server's explanation
in .detail.
Most Metrc write routes take a list of objects, even for one, so their json=
is [{...}]. /v2/items/discontinue is one of the few that take a single
object.
A few T3 routes, such as tag lists and secret keys, act immediately and have
no dry run. Passing submit= to one of them raises ValueError, so you can't
mistake one for the other. More in Writes.
10. Write many rows¶
bulk() validates every row with a dry run first, and only then submits:
items = client.collect("METRC_ACTIVE_ITEMS", license=lic, filter=["name__contains:sample"])
result = client.bulk(
"/v2/items/discontinue",
rows=[{"id": item["id"]} for item in items],
license=lic,
submit=True,
log_to="discontinue-results.csv",
)
print(result.summary())
If any row fails validation, nothing is submitted. In a terminal you're shown
the failures and asked whether to send just the valid rows; in a script,
on_error="skip" does that without asking. Leave out submit=True to only
validate, or pass submit="ask" to see the results and be asked before
anything is submitted. log_to writes one line per row with its status and
message.
11. Download a report¶
T3 builds reports on the server. They're faster than loading a collection when all you want is a spreadsheet:
The format follows the file extension (.csv, .xlsx or .json), or pass
format=. Choose columns, filter and sort the same way as collect():
client.report(
"ACTIVE_PACKAGES_REPORT",
licenses=["LIC-00001", "LIC-00002"],
to="packages.csv",
columns=["label", "quantity", "unitOfMeasureAbbreviation", "locationName"],
filter=["isOnHold__eq:false"],
sort="label:asc",
)
List every report with t3api endpoints --kind report. More in
Reports and tag lists.
12. Tag lists¶
A tag list is a list of handles (usually package tag labels) kept on the server. T3 resolves it to the objects those handles stand for. It's the quickest way to go from a column of scanned labels to full package records:
tl = client.tag_lists.from_csv(
"labels.csv",
column="label",
license=lic,
endpoint="METRC_ACTIVE_PACKAGES",
name="Room A",
)
packages = tl.resolve()
Resolving is all or nothing. If any label matches no package, or more than one,
resolve() raises t3.TagListResolveError listing each problem handle:
try:
packages = tl.resolve()
except t3.TagListResolveError as exc:
for problem in exc.errors:
print(problem["handle"], problem["reason"])
More in Reports and tag lists.
13. See the HTTP traffic¶
To see every request and response, set T3_LOG_HTTP:
Secret keys, tokens, passwords and OTP values are masked in the log. More in Errors and logging.
14. Use the command line¶
Much of the above works without writing Python:
t3api whoami
t3api pull METRC_ACTIVE_PACKAGES -l LIC-00001 -o packages.csv
t3api pull METRC_ACTIVE_ITEMS --filter name__contains:flower --inspect
t3api report ITEMS_REPORT -l LIC-00001 -o items.xlsx
t3api tag-lists create labels.csv --column label --endpoint METRC_ACTIVE_PACKAGES
t3api tag-lists resolve <list-id> -o room-a.csv
Every command is listed in The t3api command.
Troubleshooting¶
| What you see | What to do |
|---|---|
AuthRequired: No T3 credential found |
Run t3api login in a terminal, or set T3_SECRET_KEY. |
AuthError with a secret key |
The key was deleted, or your Metrc password, email or OTP changed. Run t3api keys rotate. |
AuthError with a JWT |
It expired (JWTs last 24 hours). Run t3api login and accept the secret key. |
SpecError: ... needs: license= |
Pass license=, or set a default with t3api license pick. |
UnknownEndpoint |
Check the spelling against t3api endpoints. If T3 added it recently, run t3api spec refresh. |
NotInteractive |
A picker or prompt ran without a terminal. Pass the value in code instead. |
ImportError mentioning Textual |
Install the extra: pip install "t3api-utils[tui]". |
| No rows, or fewer than expected | Check the license and filters, then rerun with T3_LOG_HTTP=1 to see what the API sent back. |
Next steps¶
- The Guide pages cover each topic in depth.
- Example scripts
- T3 API docs and the T3 wiki
- Coming from 1.x? See Migrating from 1.x.