---
title: "Exchange a sign-in code for an account token"
method: POST
path: "/api/v2/auth/token"
tags: ["Sign in"]
---

# Exchange a sign-in code for an account token

`POST /api/v2/auth/token`

## Request

- Base URL: `https://www.onlyworlds.com`
- URL: `https://www.onlyworlds.com/api/v2/auth/token`
- Auth: none required

The last step of Sign in with OnlyWorlds: the tool's own page, back from the
sign-in with `code` and `state` in its address, checks `state` against the one it
sent, then posts the code with its PKCE verifier. No credential on the request.
Send it once, before any other request, and take `code` out of the address bar
first: a code is single use, so a reload would spend it on a refusal.

A tool's registry entry may ask for consent that outlives its tokens
(`consent_outlives_tokens`): revoking its token at sign-out or when its page
closes then keeps the person's Continue, so the next sign-in to its https address
asks nothing, and only Disconnect on the account page ends it. A tool's registry
entry may ask for a lifetime on its tokens (`token_lifetime_hours`, 1 to 720): the
token it gets expires that many hours after the exchange, and an expired token is
answered 401 key_revoked on every route, exactly as a revoked one, so the tool
signs the person in again.

A listed tool that only reads may add `scope=world:read` to the sign-in request:
the token it gets here is read-only, and the answer says so with `scope:
"world:read"`. It reads everything the person's account reads; every write (a world
key minted, a world made, another token minted, an AI call) is refused with 403
permission_error, except revoking the token itself. Without a scope the answer has
no `scope` field and the token writes as the account does, but for a listed tool the
registry marks read-only, whose token is read-only whatever it asks, and for the
clients below. A read-only sign-in gets no AI door token: `ai_uses` with its code is 422
invalid_request (param ai_uses), and the code is spent.

A client that registered itself, or one whose client_id starts `worlds-only-` (a game,
client_id `worlds-only-games`, among them), always gets a read-only token, and a
narrower one: it reads the list of the account's worlds (its rows carry no `keys`),
and in a world it names with `world` the world, its
elements, search, metadata and what changed in it (GET /changes); it may revoke
itself. Every other route that takes the account token refuses it with 403
permission_error; a world key's route, a reader route called without `world` and the
old /api/worldapi routes but their metadata answer it as they answer any account
token. Its token ends at its registry entry's lifetime when the entry has one, else
720 hours after the exchange.

## Request body

- object
  - `grant_type` 'authorization_code', required
  - `code` string, required: The code the return address carried. Single use, valid for 60 seconds.
  - `code_verifier` string, required: The PKCE verifier whose S256 hash was the request's code_challenge.
  - `client_id` string, required: As in the sign-in request.
  - `redirect_uri` string, required: As in the sign-in request, byte for byte.
  - `ai_uses` string[]: Optional: also mint an AI door token for these AI uses (their slugs, as GET /api/v2/account/ai lists them), answered as `ai_token`. An unknown use is 422 and spends the code.
  - `ai_lifetime_minutes` integer: Optional, with ai_uses only: the AI door token ends this many minutes after the exchange, or at midnight UTC if that comes first. Omitted: at midnight UTC.

## Response `200`

Signed in: an `ow_a_` account token for the person, shown only here, and who they are; with `ai_uses` in the body, an AI door token beside it (`ai_token`). Cache-Control: no-store.

- object
  - `token` string, required: An `ow_a_` account token, shown only in this answer. Send it as `Authorization: Bearer` on the /account routes (a client that registered itself, or one whose client_id starts `worlds-only-` (a game's among them): on the list of worlds and the reads of a world that the `scope` description lists). It is listed on the person's account page, named for the tool and the browser (a native app: by the tool's name alone; a client that registered itself: by the host it returns the person to), where they can revoke it.
  - `token_id` string, uuid, required: The token's id: a tool done with the token revokes it with `DELETE /api/v2/account/tokens/{token_id}`, sending the token itself as the bearer; the world keys it minted go with it, unless the tool holds another live token that is not read-only, or the registry marks it as asking each time (its keys stay).
  - `token_type` 'bearer', required
  - `scope` 'world:read': When the sign-in request asked `scope=world:read`, and always for a client that registered itself, a game (client_id `worlds-only-games`), another client whose client_id starts `worlds-only-`, or a listed tool the registry marks read-only: the token is read-only. Another listed tool's reads as the account does, and every write is refused with 403 permission_error ("This token is read-only: it comes from a read-only sign-in and cannot write. Use a write key with its PIN, or sign in again without scope world:read where the tool asked for it."), but one: revoking itself with `DELETE /api/v2/account/tokens/{token_id}`. A client that registered itself, or one whose client_id starts `worlds-only-` (a game's among them), gets a token that reads worlds and nothing else: GET /api/v2/account/worlds (its rows carry no `keys`), and a world it names with `world` (GET /api/v2/world, an element type's list and one element, GET /api/v2/search, and the metadata at /api/metadata/ and /api/worldapi/metadata/), and it may revoke itself. Every other route that takes the account token refuses it with 403 permission_error ("This token reads worlds only: the list of the account's worlds, their elements, search, metadata and a world, and what changed in them. It reaches nothing else on the account."). The rest answer it as they answer any account token: a world key's route, a reader route called without `world`, and the old /api/worldapi routes but their metadata. It ends at its registry entry's lifetime when the entry has one, else 720 hours after the exchange (`expires_at`). Another listed tool's sign-in that asked no scope carries no `scope` field, and its token writes as the account does.
  - `expires_at` string, date-time, nullable, required: When the token stops working: for a tool whose registry entry asks for a lifetime (token_lifetime_hours), that many hours after the exchange; for a client that registered itself, or a game or another client whose client_id starts `worlds-only-` with none, 720 hours after it; null for another listed tool with none, whose token works until it is revoked.
  - `account` object, required
    - `id` string, uuid, required
    - `username` string, required
  - `ai_token` object: Only when the body asked for it with `ai_uses`: an AI door token, beside the account token above, shown only in this answer. It opens GET /api/v2/account/ai and POST /api/v2/account/ai/{use} for the uses it names, as the person, on their token rating, and nothing else (every other route 401 wrong_audience; another use 403 use_not_granted). Revoking the account token leaves it live, so a tool may revoke the account token at once and keep this one for the AI calls.
    - `token` string, required: The `ow_a_` AI door token. Send it as `Authorization: Bearer` on the AI door's routes.
    - `token_id` string, uuid, required: Its id: a tool done with it revokes it with `DELETE /api/v2/account/tokens/{token_id}`, sending the token itself as the bearer.
    - `token_type` 'bearer', required
    - `audience` 'ai', required
    - `ai_uses` string[], required: The uses it opens, as asked.
    - `expires_at` string, date-time, required: When it stops working: the next midnight UTC, or sooner when `ai_lifetime_minutes` asked. Past it, 401 key_revoked.

## Other responses

- `400`: The code cannot be exchanged (invalid_grant): unknown, used, expired, or issued for another client_id, redirect_uri or code_verifier. One body for every case, and the code is spent either way. A used code sent again also revokes the token its first exchange minted.
- `422`: The body is not JSON (a form-encoded body included), not an object, or a field is missing or not text, or grant_type is not authorization_code, or ai_uses is not a list of 1 to 8 AI uses this server runs (an unknown use included), or ai_lifetime_minutes is not 1 to 1440 or comes without ai_uses (invalid_request; `param` names the field), or ai_uses is sent with the code of a read-only sign-in: one asked with scope world:read, or any of a client that registered itself, of one whose client_id starts `worlds-only-` (a game's among them) or of a listed tool the registry marks read-only (param ai_uses; a read-only sign-in gets no AI door token). Whether the code is spent: a form-encoded body or a JSON object (a missing or bad field included) spends the code it carried; a body that is not valid JSON, is not an object, or has any other content type does not.
- `429`: Too many sign-in attempts from this address (rate_limited): this route and the password sign-in share one count, 40 in 5 minutes. The route reads no body then and spends nothing: a code sent with the request is still good. Retry after the Retry-After header's seconds. Or too many requests: over the request budget (code too_many_requests, type rate_limited): 300 requests a minute per key or account token, 60 a minute per address for a request with neither. Retry after the Retry-After header's seconds.
- `503`: The server is busy: every request slot stayed full for 10 s (code server_busy, type api_error). Retry after the Retry-After header's seconds.

## Changes

- **2026-11-28** `e45847b3f48f`: 4 info
  - added the optional property `error/limit` to the response with the `400` status
  - added the optional property `error/limit` to the response with the `422` status
  - added the optional property `error/limit` to the response with the `429` status
  - added the optional property `error/limit` to the response with the `503` status
- **2026-11-26** `f921dd878e35`: 4 warning
  - added the new `write_not_granted` enum value to the `error/code` response property for the response status `400`
  - added the new `write_not_granted` enum value to the `error/code` response property for the response status `422`
  - added the new `write_not_granted` enum value to the `error/code` response property for the response status `429`
  - added the new `write_not_granted` enum value to the `error/code` response property for the response status `503`
- **2026-11-26** `7ef7860703bb`: 4 warning
  - added the new `narratives_only` enum value to the `error/code` response property for the response status `400`
  - added the new `narratives_only` enum value to the `error/code` response property for the response status `422`
  - added the new `narratives_only` enum value to the `error/code` response property for the response status `429`
  - added the new `narratives_only` enum value to the `error/code` response property for the response status `503`
- …earlier changes not shown

[Operation history](https://skmtc.dev/onlyworlds/apis/onlyworlds-api/changes/api/v2/auth/token/post.md)

---

[API](https://skmtc.dev/onlyworlds/apis/onlyworlds-api.md) - [All operations](https://skmtc.dev/onlyworlds/apis/onlyworlds-api/llms.txt) - [OpenAPI document](https://skmtc.dev/onlyworlds/apis/onlyworlds-api/revisions/6c819f2e4bbd?raw)
