Torii user guide — contents

Docs · Torii

API tokens

An API token is how a script, a pipeline or a host talks to Torii. Each token belongs to one organisation, carries one role, expires, and can be revoked.

What a token looks like

torii_<organisation id, 32 hex characters>_<secret>
  • The organisation id is a routing hint. It tells Torii which organisation to look inside. It authorises nothing: the whole token must still match a row that exists within that organisation. Pairing one organisation's secret with another organisation's id is refused like any other bad token.
  • The secret is 256 random bits. Torii stores only a SHA-256 hash of the token, so a copy of the database is not a set of working credentials.
  • The value is shown once. It is returned when the token is created and cannot be read back afterwards, by you or by minatolabs. If you lose it, revoke it and create another.

Using a token

Send it as a bearer token on every request. In this guide TORII_CONTROL_PLANE_URL is the address of the Torii service and TORII_API_TOKEN is your token; the console uses the same two names in the commands it gives you. The API is served under /v1 on the same address as the console, so on the hosted service the address is https://torii.minatolabs.com.

Any API call
curl -sS "$TORII_CONTROL_PLANE_URL/v1/hosts" \
  -H "Authorization: Bearer $TORII_API_TOKEN"

Keep tokens in a secret store or an environment variable, never in a file you commit. Every route under /v1 accepts a bearer token and nothing else; a browser session cookie is not accepted there. API reference lists every route.

Creating a token in the console

You need a role that may create tokens: owner, admin, security or developer. A read-only member does not see the button.

  1. Open Settings and find the API tokens card.
  2. Choose Create token. The dialog is titled Create an API token.
  3. In Name, say what the token is for, so you can tell tokens apart later. Up to 100 characters.
  4. Choose a Role. The list offers only roles whose every permission you hold yourself. When Security is offered it is selected for you, because it is the least a host needs to enrol.
  5. Choose Expires after: 7 days, 30 days (the default here), 90 days or 1 year.
  6. Choose Create token. The value appears under Copy this token now — it is shown once. Choose Copy, store it, then choose Done.

If the browser refuses the copy, the console selects the token text instead and tells you to press the copy shortcut yourself. Closing the dialog discards the value; it cannot be shown again.

Creating a token with the API

The same roles may create tokens through the API, using a token they already hold.

Create a token
curl -sS -X POST "$TORII_CONTROL_PLANE_URL/v1/api-tokens" \
  -H "Authorization: Bearer $TORII_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name": "ci-read", "role": "readonly", "expires_in_days": 30}'
FieldRequiredRules
nameYesUp to 100 characters. Say what the token is for.
roleNoOne of owner, admin, security, developer, readonly. Defaults to readonly.
expires_in_daysNoDefaults to 90. Values below 1 become 1 and values above 365 become 365.
The response, returned once
{
  "id": "<token id>",
  "name": "ci-read",
  "role": "readonly",
  "token": "torii_<organisation id>_<secret>",
  "token_prefix": "torii_<first 10 characters of the organisation id>",
  "created_at": "<timestamp>",
  "expires_at": "<timestamp>",
  "warning": "This value is shown once and is not recoverable."
}

A token cannot outrank its creator

Torii compares the permissions of the role you ask for with the permissions you hold. If the new token would be able to do anything you cannot, the request is refused with 403 and role_exceeds_creator, and the attempt is written to the audit log. Without this rule, creating a token would be a one-request path from read-only to owner. The console makes the same check against your own role before it sends anything, and answers A token cannot have more access than you do. with the permissions the role would add.

Listing and revoking

Owners and admins can list every token in the organisation. The list never contains a token value, only what identifies it:

List tokens
curl -sS "$TORII_CONTROL_PLANE_URL/v1/api-tokens" \
  -H "Authorization: Bearer $TORII_API_TOKEN"
FieldMeaning
nameThe name given at creation.
token_prefixThe first 16 characters, enough to recognise a token in a log.
created_atWhen it was created.
expires_atWhen it stops working.
last_used_atWhen it last authenticated a request, or empty if never.
revoked_atWhen it was revoked, or empty if it is still active.

In the console the same list is the table in the API tokens card, with the columns Name, Prefix, Created, Last used (Never if it has not been used) and State (Active or Revoked). Members who may not list tokens see You don't have access to this in the card instead.

Owners, admins and the security role can revoke a token. Revocation takes effect on the next request and is the only control that works after a leak.

  1. In the API tokens table, choose Revoke on the token's row. Only active tokens have the button.
  2. Read the confirmation: anything using the token stops working immediately, and this cannot be undone.
  3. Choose Revoke token. The row then shows Revoked.

Because a security member cannot list tokens, they see no table to revoke from in the console. They can still revoke through the API with the token's id:

Revoke a token
curl -sS -X POST "$TORII_CONTROL_PLANE_URL/v1/api-tokens/$TOKEN_ID/revoke" \
  -H "Authorization: Bearer $TORII_API_TOKEN"

Revoking a token that is already revoked, or that does not exist in your organisation, answers 404.

What the API answers

StatusMeaning
400The body is missing or malformed: invalid_body, invalid_name or invalid_role.
401No token, a malformed one, an expired or revoked one, or one that does not belong to the organisation it names. All of these get the same answer.
403The token is valid and its role lacks the permission the route needs. The response names the permission.
404The thing you asked for does not exist in your organisation. Something that exists in another organisation gets this same answer.
429Too many requests. The response carries Retry-After: 60. See the limits below.

Limits

LimitDefault
Requests per organisation600 per minute
Requests per IP address300 per minute
Sign-in requests per IP address10 per minute
Request body1 MiB

On the hosted service the per-address limits are not yet counted per caller: behind its load balancer every caller shares them. A 429 can therefore come from other people's traffic. Wait the number of seconds in Retry-After and try again.