# POST /v1/token

URL: https://docs.folkways.io/docs/realtime-api/token

Exchange a publishable key for an anonymous, read-only token, for public apps without a backend.

Trades a [publishable key](/docs/console/publishable-keys) for an anonymous, read-only token. The app then connects
with that token like any other. JSON, not frames.

## Request [#request]

```http
POST https://realtime.folkways.net/v1/token
Authorization: Bearer <publishable key>
Content-Type: application/json

{ "cap": [{ "topic": "scores/match-7", "ops": ["subscribe"] }], "ttl_secs": 600 }
```

Every field is optional; an empty body asks for the defaults.

| Field | Default | Meaning |
| --- | --- | --- |
| `cap` | all of the key's rights | The rights for this token. Each must fall within the key's, or `Forbidden` |
| `ttl_secs` | `600` | Lifetime in seconds, at most 900 and never past the key's own expiry |
| `kind` | `"anonymous"` | Anything else is `Forbidden` |
| `sub` | | Must be left out: a publishable key never names a user |

## Response [#response]

```json title="200 OK"
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…",
  "expires_at_ms": 1792000600000,
  "identity": null,
  "kind": "anonymous",
  "capabilities": [{ "topic": "scores/match-7", "ops": ["subscribe"] }]
}
```

To refresh before `TokenExpiring`, exchange the key again and send `Reauth`.

## Errors [#errors]

| Status | `error` | When |
| --- | --- | --- |
| `401` | `AuthRequired` | No `Authorization: Bearer` header |
| `401` | `AuthInvalid` | Malformed, unknown, revoked or expired key, or a wrong secret |
| `403` | `Suspended` | The key is paused, or the segment is suspended |
| `403` | `Forbidden` | `sub` or a non-anonymous `kind` given; `cap` wider than the key; the segment doesn't allow anonymous connections |
| `400` | `InvalidArgument` | The body isn't a token request |
| `429` | `RateLimited` | Too many failed attempts from this address in the last minute. `retry_after_ms` and `Retry-After` say how long to wait |
| `503` | `Unavailable`, `Draining` | Try again shortly |

Errors are JSON: `{ "code": 204, "error": "Suspended", "message": "api key paused" }`.

CORS allows any origin: publishable keys are meant for browser apps. A paused or revoked key stops new exchanges
within about 30 seconds, and connections using its tokens close within about a minute.
