Skip to content

Read your synced usage in a script or update session tags. Personal API access is free. Use a Tokematic API credential, separate from your AI vendor credentials.

Use the Tokematic API on YouTube

Transcript

Use the Tokematic API to pull your usage records into your own script or dashboard, and to set work tags on sessions. Personal API access is free. We will use a personal space and a credential that has only the permissions the script needs. This credential is for Tokematic, separate from any AI vendor key.

In web Settings, open Account, then Credentials. Under Tokematic API Credentials, choose Create credential and give it a clear label, such as Personal dashboard. Choose Personal as the owner. Read usage records lets the script read. Add tags to sessions is separate; turn it on only if the script will set tags. Neither permission can change a usage record.

The credential is shown once. Copy it into your password manager or secret store, tick I saved this credential and keep it out of source code, screenshots and command history. Requests send it in the Authorization header as a bearer credential. Anyone holding it can do what it is allowed to do.

In Credentials, open Connection details and copy the group prefix. It identifies your space. Put it in the records request shown here, ask for Usage Report records and send your credential in the Authorization header. The response contains records and a next cursor; keep following the cursor until it is empty. You can filter by field and date, and ask only for changes since a version to keep a dashboard current.

To tag a session, first list the tagged sessions and take the provider and vendor session ID from that response. The usage record ID is a different identifier. With Add tags to sessions permission, send the update shown here. It sets Customer to Acme and Work type to Research. Fields you leave out stay as they were. Send null to clear a field. Comment holds a free-text note. Usage records are untouched.

Responses to your credential carry an allowance header, so your script can see how much it has left before it runs out. A rate limit response uses status four twenty-nine with a retry-after value; wait that long. Allowances have hourly, daily and monthly windows shared by every credential you own, so a new credential does not reset them.

When a script is finished, return to Credentials and revoke its credential. New requests with it fail at once. Data the script already copied stays wherever it put it. If you are replacing a credential, update the script before revoking the old one.

Start with Read usage records. Add tags to sessions only for scripts that change tags. Revoke a credential when its script retires.

Create a credential

  1. In web Settings, open Account > Credentials.
  2. Under Tokematic API Credentials, choose Create. Give the credential a label that names its job and choose Personal as its owner.
  3. Select Read usage records. Select Add tags to sessions only if the script needs to change tags.
  4. Save the credential in a secure store when it appears. You can see it only once. Keep it out of source code, screenshots and command history.

Send the credential in the HTTP Authorization header using the Bearer scheme. These permissions do not allow a script to overwrite source usage records.

Find the group prefix

Choose Connection details on the credential's row. Copy Group prefix and the API base URL. For a group that uses the group records API, you can also copy Records endpoint.

The prefix is the short code used in /v2/groups/{prefix}/.... It is not the credential secret or the internal group ID. The details belong to that specific credential, even if you have switched workspaces. An organization using project transport needs project-scoped access instead of these group routes.

Read usage records

Send GET /v2/groups/{prefix}/records?type=UsageReport&limit=10 or send QUERY /v2/groups/{prefix}/records with a JSON body such as {"type":"UsageReport","limit":10}. Keep following the returned next_cursor with cursor until it is empty. Keep the same record type filter on each page.

The response contains stored usage fields and record IDs. Protected fields remain encrypted. A usage record ID is not a session ID. These group reads cover the records synced to that group rather than every local record on your computer.

Update an existing session's tags

With Read usage records, send GET /v2/groups/{prefix}/session-tags to list existing tagged sessions. Choose the row to update and use its provider and sessionId. This list does not include every untagged session. Here, sessionId is the vendor's session ID, not the Tokematic Session ID used by tmatic --session.

With Add tags to sessions, send PUT /v2/groups/{prefix}/sessions/{provider}/{sessionId}/tags with a JSON body such as:

{"customer":"Acme","workType":"Research"}

This sets two key/value pairs. The default classification keys are customer, costCenter and workType. ticket stores a ticket reference. comment is a free-text note. Use an approved value when the organization restricts that field.

Send {"workType":null} to clear only Work type. Fields left out of the request stay unchanged. Read the response to check the saved values.

The older /annotations/{recordId} route stores one plain label on a usage record. It is separate from session work tags. See work tags for current custom-tag and policy limits.

Handle limits and end access

On a 429 response, wait for the server's Retry-After period. Credentials for the same owner share allowance counters, so creating a new credential does not reset them.

When the script no longer needs access, choose Revoke on its credential. Future requests will fail. Copies the script already downloaded remain where it saved them.

Next video: Share a session

2. For Engineers: Pace, Sandboxes & Proving Value

All guides