API Scripts¶
T3 API scripts are written in Python with t3api-utils, the T3 Python library, and run with uv. The T3 API examples repository has ready-to-run scripts for common jobs.
Running Python Scripts with UV¶
T3 Scripts
Inline Packages¶
This lets you declare your dependencies at the top of the script using special comments. When run with uv run example.py, uv automatically resolves and installs those dependencies in an isolated environment.
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = ["t3api-utils>=2.1.0"]
# ///
Use "t3api-utils[tui]>=2.1.0" instead when the script browses rows in the terminal with t3.inspect().
Virtual Environments¶
You can install dependencies into a standard Python virtual environment managed by uv venv:
In a uv project, run uv add t3api-utils and run scripts with uv run.
t3api-utils¶
t3api-utils is the T3 Python library and t3api command. It handles login and secret keys, loads entire collections in parallel, sends writes as a dry run unless you pass submit=True, and downloads server-side reports.
- Documentation, starting with Getting started
- GitHub repository
Version 2 is a rewrite. A script written for 1.x needs the migration guide, or t3api-utils<2 in its dependencies.
Logging In¶
Log in once from the command line:
It asks for your Metrc login and creates a secret key, which it saves in your operating system's keyring. Your Metrc password is not stored. Scripts then connect without asking for anything.
If you skip this step, the first t3.connect() in a terminal asks for your login instead. On a server or in a scheduled job, set the T3_SECRET_KEY environment variable.
A First Script¶
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = ["t3api-utils>=2.1.0"]
# ///
import t3api_utils as t3
with t3.connect() as client:
license = client.license or client.pick_license()
packages = client.collect(
"METRC_ACTIVE_PACKAGES",
license=license,
filter=["isOnHold__eq:false"],
mask=["label", "item.name", "quantity", "unitOfMeasureAbbreviation"],
)
print(f"Saved {len(packages):,} packages to {t3.to_csv(packages, 'output/')}")
client.licenseis the default license saved witht3api license pick. Without one,client.pick_license()lists your licenses and asks.METRC_ACTIVE_PACKAGESis a T3 endpoint ID. The path/v2/packages/activeworks too.collect()loads every page in parallel.filterandmaskrun on the server, so only the rows and fields you asked for come back.to_csv()turns nested fields into columns such asitem.name. Given a folder, it names the file after the data, the license and the time.- A license number your login doesn't have raises
t3.UnknownLicensebefore any request is sent.
Writing to Metrc¶
Writes are a dry run unless you pass submit=True. A dry run checks the request's format. It does not check that the records you name exist. bulk() dry-runs every row, then submits them:
result = client.bulk(
"/v2/packages/change-locations",
[{"id": 1234567, "locationId": 8901, "actualDate": "2026-11-30"}],
license=license,
submit="ask",
log_to="output/move-results.csv",
)
print(result.summary())
submit="ask" shows the dry run's results and asks before sending anything. log_to records every row's outcome, even when a run stops partway.
Sandbox¶
The Metrc sandbox is a separate copy of Metrc with test data. Pass sandbox=True to connect(), or set T3_SANDBOX=1 to point any script at it without editing it:
- Licenses differ. The sandbox has its own license numbers, such as
SF-SBX-1-101. The saved default license is ignored there, so scripts ask you to pick one. - Submits are real, against test data. Try every write script in the sandbox before running it on a real license.
- Data is sparse. Many sandbox licenses have no packages or transfers, so empty results are normal.
- Your Metrc account needs sandbox access.
report_url()doesn't work there. It needs a secret key, and a sandbox session uses a JWT.
Example Scripts¶
The T3 API examples repository holds scripts built on t3api-utils. Each one is a single file, so you can download just the one you need. Start with:
| Folder | Scripts |
|---|---|
getting-started/ | Log in, list your licenses, and export one license's active packages |
basics/ | One library feature each: filters, supercollections, reports, tag lists, search, dry runs, errors, scheduled jobs |
packages/ | Move, adjust, finish, note and split packages; export lab results and history |
items/ | Create items and strains, discontinue items, upload and download item photos |
transfers/ | Download manifest and COA PDFs, export and void outgoing transfers |
sales/ | Export sales receipts, and void them |
cultivation/ | Move plants, change growth phase, record plant waste, finish harvests, package a harvest |
Every script:
- runs with no arguments, and asks for what it needs, such as a license or an input file
- takes the same flags for unattended runs:
-llicense,-pprofile,--sandbox,-iinput file,-ooutput, and-yto submit without asking - checks every row, then asks before changing Metrc data, with "No" as the default answer
- writes its output, and a log of every row it submitted, to
output/
Each script driven by a CSV comes with a .sample.csv showing the columns it expects. To write your own in the same style, follow the repository's style guide.
Next Steps¶
- Use Supercollections to load packages + lab results (or transfers + packages) in a single call.
- Use Reports to dump entire Metrc datasets without pagination.
- Point an AI model at the library with AI & LLM Integration.
- Learn about the OpenAPI spec, including how to feed a pruned subset to an LLM or code generator.
- Refer to the T3 API documentation to explore all the available endpoints.