
# API keys

Give each app, service or person their own API key, so you can see what each one spends and stop one without touching the others.

**Dashboard**

1. Choose the key's workspace at the top of the sidebar, open **API keys** and choose **Create key**.
2. Under **Start from**, choose **Coding agent**, **Production service**, **Contractor** or **Choose everything myself**. A template fills in the fields that follow.
3. Enter a **Name**, and under **Who it belongs to**, choose **You (personal)** or **Workspace key**.
4. Under **Models this key can call**, keep **All permitted models** or choose **Only the models I choose**. **Advanced** holds the expiry, the request limit and the budget.
5. Choose **Create key**.
6. In **Save your new key**, choose **Copy key** and store it, then choose **Done**.

![The API keys page listing four active keys with their workspace, models and expiry](/images/dashboard/api-keys.png)

The key is shown this once, with setup snippets for **curl**, the SDKs and coding agents, and now appears in the list.

Start an application's key from **Production service**, a workspace key with a default limit of 600 requests a minute per replica. **Coding agent** keys expire after 30 days with $500 a month. **Contractor** keys expire after 30 days with $500 in total.

A **Coding agent** key is your own, so it needs your own account. Signed in with the master key, choose **Create account** beside it first. A template you can't start from says why under its name.

You need: an organisation Owner or Admin account, or a Workspace admin or Developer account in the workspace.

**HTTP API**

```bash
curl -sS "$TOKEDEX_URL/control/v1/inference-keys" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "support-bot-prod",
    "owner": { "kind": "service", "workspaceId": "<workspace-id>" },
    "allowedModels": null,
    "expiresIn": "90d"
  }'
```

The gateway answers `201`. Store `token` now: it is shown this once.

```json
{
  "object": "inference_key",
  "id": "01M7V8WE…",
  "name": "support-bot-prod",
  "status": "active",
  "owner": { "kind": "service", "userId": null, "workspaceId": "ws_01M7V8K7…" },
  "allowedModels": null,
  "expiresAt": "2027-02-18T01:24:38.931Z",
  "token": "tokedex_ik_01M7V8WE…"
}
```

You need: an admin key with the `key-manager` or `admin` preset in `TOKEDEX_ADMIN_KEY`, and `export TOKEDEX_URL='http://127.0.0.1:4000'`.

`allowedModels` is required: a list of model IDs, or `null` for every model the workspace allows. `expiresIn` is `1d`, `7d`, `30d`, `90d`, `1y` or `never`, the default. Without `owner`, the key lands in **Default**. `limits: { "rpm": 60 }` sets a limit of 60 requests a minute per replica.

A new key is shortened to fit the workspace's limits. Only an organisation Owner or Admin can give a key its own budget. See [Budgets](/gateway/budgets) and [Rate limits](/gateway/errors-and-limits#rate-limits).

## Personal and workspace keys

A personal key suits your editor, coding agent or experiments, and stops working when its holder leaves the workspace. A workspace key suits an application, a shared service or automation, and keeps working when people leave.

**Dashboard**

Only the holder ever sees a personal key's secret. To give someone one, an administrator offers it from **Account → Personal keys → Create a key**, under **Who holds it**, with **Offer it to a person**. The person opens the key under **Account → Personal keys** and chooses **Pick up the key**.

**HTTP API**

Only the holder sees a personal key's secret, so an admin key offers one instead: send `"owner": { "kind": "personal", "userId": "<user-id>", "workspaceId": "<workspace-id>" }` with `"secretless": true`. The key is created with `"status": "awaiting_pickup"` and `"token": null`, and the person picks it up under **Account → Personal keys** in the dashboard.

## Find a key

**Dashboard**

On **API keys**, filter by status, **Unused 30 days**, **Expiring in 7 days**, **Outside the policy** (expiring later than the workspace now allows) or holder. To find the key behind a value in a log, paste it into **Paste a key to find it**. Disable keys under **Unused 30 days**, so fewer secrets can leak, and revoke them once nobody asks for them back.

**HTTP API**

```bash
curl -sS "$TOKEDEX_URL/control/v1/inference-keys?workspaceId=<workspace-id>&limit=20" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY"
```

```json
{
  "object": "list",
  "data": [{ "id": "01M7V8WE…", "name": "support-bot-prod", "status": "active", "lastUsedAt": null, "expiresAt": "2027-02-18T01:24:38.931Z" }],
  "page": { "next": null, "hasMore": false, "limit": 20 }
}
```

A key's secret is never listed. Filter with `status`, `q` (part of a name, or the start of a key ID), `idle=30d`, `expiring=7d` or `outsideLimits=true`, and pass `page.next` as `cursor` for the next page. A key's ID is the part of its token after `tokedex_ik_`, up to the next `_`.

## Rotate a key

**Dashboard**

1. Open the key from **API keys**, or your own from **Account → Personal keys**, and choose **Rotate**.
2. Under **Keep the current secret working for**, choose **End it now**, **1 hour**, **24 hours** (the default) or **7 days**, then choose **Rotate**.
3. Copy the new key and update every client that uses it.
4. Once **Secrets** on the **Overview** tab shows nothing uses the old secret, choose **End grace now**.

**HTTP API**

```bash
curl -sS -X POST "$TOKEDEX_URL/control/v1/inference-keys/<key-id>/rotate" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "grace": "24h" }'
```

The response holds the new key in `token`, and the old secret under `secrets` with `"state": "grace"` until its `notAfter`. `grace` is `now`, `1h`, `24h` (the default) or `7d`. Once nothing uses the old secret, end it with `POST …/<key-id>/secrets/<generation>/retire`.

The key keeps its ID, budget and history. Only a personal key's holder can rotate it. Rotate a key when someone who has seen its secret leaves, with a grace period so clients move over before the old secret stops.

On a gateway with Postgres replicas, the replica that handles the change applies it immediately. The others pick it up on their next poll, every two seconds by default (`TOKEDEX_CONFIG_POLL_INTERVAL_MS`). Until then, another replica can refuse the new secret or still accept a secret you ended, even after **End it now** or **End grace now**.

If you lose the rotation response before saving the secret, inspect the key's generation on the replica that handled the rotation. If you can only read through the load balancer, wait at least one configured polling interval before checking because another replica can still show the previous generation. The new secret is available only in the rotation response. Once you confirm the change, rotate again after any active grace period ends, or end that grace period during the new rotation.

## Disable or revoke a key

**Dashboard**

Open the key and choose **Disable** to refuse it until you choose **Enable**, or **Revoke** to stop it for good. **Delete permanently** then removes its record.

**HTTP API**

```bash
curl -sS -X POST "$TOKEDEX_URL/control/v1/inference-keys/<key-id>/revoke" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "reason": "compromised" }'
```

The response is the key with `"status": "revoked"`. `POST …/disable` and `…/enable` work the same way, without a body. `DELETE /control/v1/inference-keys/<key-id>` removes a revoked key's record and returns `"deleted": true`.

A disabled key gets `403` `key_disabled`, and a revoked one `401` `invalid_api_key`. Revoking ends a budget set on that key alone; its usage and spend history stay. Revoke a key that may have leaked, then create a replacement.

A disabled or revoked key can still work on another replica until its next [configuration poll](#rotate-a-key). A restored backup brings keys back as they were. [Backup and restore](/gateway/backup-restore)

## Give a script an admin key

An admin key (`tokedex_mk_…`) calls the management endpoints, not models.

**Dashboard**

1. Choose the workspace the script manages, or **All workspaces** for the whole gateway, and open **API keys → Admin keys**.
2. Choose **Create admin key**, enter a **Name**, choose what it can do and when it expires, and choose **Create key**.
3. Copy the key, which is shown this once.

Creating an admin key asks you to confirm it's you when your sign-in is not recent.

For a script that acts as you, create a personal access token under **Account → Access tokens** instead. It lasts at most 90 days. `tdx auth login` creates one for the CLI when you approve its code in the browser, as the [CLI quickstart](/cli/quickstart) shows.

**HTTP API**

```bash
curl -sS "$TOKEDEX_URL/control/v1/management-keys" \
  -H "Authorization: Bearer $TOKEDEX_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "research-ci",
    "preset": "workspace-key-manager",
    "owner": { "kind": "service", "workspaceId": "<workspace-id>" },
    "scope": { "workspaceIds": ["<workspace-id>"] },
    "expiresIn": "90d"
  }'
```

```json
{ "object": "management_key", "id": "01M7VBF0…", "name": "research-ci", "preset": "workspace-key-manager", "status": "active", "expiresAt": "2027-02-18T02:09:43.473Z", "adjustments": null, "token": "tokedex_mk_01M7VBF0…" }
```

The key reaches only that workspace. For one that manages the whole gateway, leave out `owner` and `scope` and choose a preset such as `key-manager`. This needs the `admin` preset, or `workspace-admin` for its own workspace. `GET /control/v1/permissions` lists every preset with its permissions, and `grants` names permissions directly in place of `preset`.

Personal access tokens are created only by their owner: in the dashboard, or by approving `tdx auth login` in it.

Before any admin key exists, create the first one with the master key, and keep it in `TOKEDEX_ADMIN_KEY`. The token is shown once:

```bash
export TOKEDEX_ADMIN_KEY="$(curl -sS "$TOKEDEX_URL/control/v1/management-keys" \
  -H "Authorization: Bearer $TOKEDEX_MASTER_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "name": "ci-admin", "preset": "admin", "expiresIn": "90d" }' \
  | jq -r .token)"
```

| Preset | Can |
| --- | --- |
| `admin` | Everything an admin key can do |
| `key-manager` | Create and manage API keys; read their budgets, spend and usage |
| `viewer` | Read keys, budgets, spend and usage |
| `auditor` | Every read, the audit log included, except captured prompts and responses |
| `finance` | Read spend, usage, budgets, prices and workspaces |
| `operator` | Manage models and gateway settings; read usage and traces |
| `credential-admin` | Manage provider credentials |
| `workspace-admin`, `workspace-key-manager`, `workspace-viewer` | Administer, manage the keys of, or read the workspaces in `scope.workspaceIds` |

Give a script the smallest preset that works, scoped to its workspace where it can be, and an expiry, so a leaked admin key can do little for long. A script that creates keys in one workspace needs `workspace-key-manager`, and across the gateway `key-manager`; a spend export needs `finance`.

The [CLI](/cli) reads an admin key from `TOKEDEX_TOKEN`, as [Scripts and agents](/cli/scripts-and-agents#choose-how-to-pass-credentials) in the CLI docs shows.

An admin key works only while the person who created or last rotated it is active and still holds everything it can do. Rotate it to keep it working when they leave.

An admin key a workspace owns lives 90 days unless you choose, at most a year, and never longer than the admin key that created it. The response's `adjustments` names any change; a gateway-wide admin key has no such cap.

## Recommendations

- **Give each application its own key.** A leaked key is then revoked without stopping the others, and spend is reported per key.
- **Limit a key to the models it needs.** A leaked key then reaches only those.
- **Give each key a budget,** either its own or the workspace's default per API key, to limit a runaway client's spend.

Next: [Connect your app](/gateway/connect-your-app), [Budgets](/gateway/budgets) or [Model access and limits](/gateway/model-access-and-limits).
