Authentication¶
The T3 API supports two authentication formats for most endpoints:
- Credential authentication: a short-lived JWT obtained by exchanging your Metrc credentials at
/v2/auth/credentials. - Secret key authentication: a long-lived opaque key that you generate once and then pass on every request.
Both formats produce the same effective access. Pick the one that matches how your integration runs.
Credential Authentication¶
How it works¶
- Your client sends your Metrc username, password, and hostname (plus an email or a one-time password in states that ask for one) to
POST /v2/auth/credentials. - T3 signs in to Metrc on your behalf and returns a JWT bearer token.
- Your client sends the JWT with every subsequent request:
- When the JWT expires, your client must repeat step 1 to get a new one.
import requests
response = requests.post(
"https://api.trackandtrace.tools/v2/auth/credentials",
json={
"hostname": "ca.metrc.com",
"username": "YOUR_METRC_USERNAME",
"password": "YOUR_METRC_PASSWORD",
},
)
response.raise_for_status()
access_token = response.json()["accessToken"]
States that ask for more¶
Some states' Metrc logins also ask for an email or a one-time password. GET /v2/states lists every state with requiresEmail and requiresOtp, and needs no authentication, so you can check before logging in:
import requests
states = requests.get("https://api.trackandtrace.tools/v2/states").json()
maryland = next(state for state in states if state["hostname"] == "md.metrc.com")
print(maryland["requiresEmail"], maryland["requiresOtp"]) # True False
Send email when requiresEmail is true. When requiresOtp is true, send a current one-time password as otp to POST /v2/auth/credentials, or your multi-factor setup key as otpSeed when creating a secret key.
Where to use it¶
- Interactive scripts you run manually from a laptop and that finish in one session.
- Notebook-driven analysis where you authenticate once at the top of the notebook and discard the token at the end.
- Postman / curl exploration of the API.
Credential authentication is the right default when you control the script end-to-end and don't want T3 to retain any version of your password.
Secret Key Authentication¶
How it works¶
- You generate a secret key once via
POST /v2/auth/secretkey, supplying your Metrc credentials. - T3 checks the credentials with Metrc and returns an opaque secret-key UUID.
- Your client sends the secret key on every request, either in a header (preferred) or as a query parameter: or
- T3 handles signing in to Metrc, so your integration never handles a token or a refresh.
How your credentials are stored¶
A secret key stores the Metrc credentials you submitted, encrypted. T3 decrypts them only to sign in to Metrc for you, and never logs them. Deleting the key deletes them.
When your Metrc credentials change¶
A secret key holds the credentials you submitted when you created it. If any of the following change in Metrc, the key starts returning 401 Unauthorized with the code METRC_LOGIN_FAILURE:
- Password
- Email (states with
requiresEmail) - Username
- Multi-factor setup key (states with
requiresOtp)
To keep using the same key, send the new credentials to PUT /v2/auth/secretkey/<uuid>. T3 checks them with Metrc before saving, and the key's value does not change, so nothing that uses it needs updating. The username and hostname must match the ones the key was created with; for a different Metrc user, create a new key.
import requests
requests.put(
f"https://api.trackandtrace.tools/v2/auth/secretkey/{SECRET_KEY}",
json={
"hostname": "ca.metrc.com",
"username": "YOUR_METRC_USERNAME",
"password": "YOUR_NEW_METRC_PASSWORD",
},
).raise_for_status()
A scheduled report running on a key whose credentials stopped working pauses until you save its schedule again.
Where to use it¶
- Long-running scripts (data pipelines, scheduled reports) that should not have to handle JWT refresh.
- Server-to-server integrations that benefit from one fixed credential rather than an in-memory token.
- Spreadsheet sync and other tools that need a stable URL with auth embedded.
Secret key authentication is the right default when your integration runs unattended or when re-authenticating periodically is a burden.
Comparing the Two¶
sequenceDiagram
participant C as Your client
participant T as T3 API
participant M as Metrc
rect rgba(126,87,194,.08)
Note over C,M: Credential auth
C->>T: POST /v2/auth/credentials with Metrc credentials
T->>M: Sign in on your behalf
T-->>C: Short lived JWT
C->>T: Every request carries Authorization: Bearer jwt
Note over C: When the JWT expires, the client repeats the exchange
end
rect rgba(126,87,194,.08)
Note over C,M: Secret key auth
C->>T: Every request carries X-T3-API-Key
T->>M: Sign in on your behalf
T-->>C: Response, no token handling in the client
end | Property | Credential Auth | Secret Key Auth |
|---|---|---|
| Where credentials live | Caller's environment only | Stored encrypted by T3 |
| What you send per request | Authorization: Bearer <jwt> | X-T3-API-Key: <key> (or ?secretKey=<key>) |
| Lifetime | JWT expires; client must re-auth | Until you delete it |
| Survives a Metrc password change? | Yes, the next exchange uses the new password | Yes, once you send the new password with PUT |
| Best for | Interactive scripts, ad-hoc analysis | Long-running scripts, automation, spreadsheet sync |
| Revocation | Wait for JWT to expire | Delete the secret key |
Managing Secret Keys¶
Create a key on the secret-key page, or with the endpoints below:
| Action | Endpoint |
|---|---|
| Generate | POST /v2/auth/secretkey |
| List | GET /v2/auth/secretkey |
| Update credentials | PUT /v2/auth/secretkey/<uuid> |
| Delete | DELETE /v2/auth/secretkey/<uuid> |
The list response includes each key's createdAt, lastUsedAt (so you can identify dormant keys), and isEnabled and isManaged flags.
Each generate call creates a new, separate key, even for credentials you have used before. A Metrc account can hold 25 secret keys. Generating another returns a 409 with code SECRET_KEY_LIMIT_EXCEEDED; delete one first. Store your key and reuse it rather than generating one each time a script runs.
When you delete a key, it stops authenticating immediately; there is no grace period.
A managed key (isManaged: true) can't be deleted. Deleting one returns a 403 with code SECRET_KEY_MANAGED.
Next Steps¶
- Generate your first secret key at the secret-key page.
- Make your first request in Getting Started.
t3api-utilshandles both.t3api logincreates a secret key and saves it to your OS keyring, andt3.connect()uses it, or a key in theT3_SECRET_KEYenvironment variable.- Refer to the authentication endpoints in the interactive API documentation.
- Example scripts:
basics/connect-and-whoami.py(show which credential and Metrc host you're connected with),basics/scheduled-job.py(run unattended withT3_SECRET_KEY).