# API Keys

> Inspect, create, update, and delete AnyRouter LLM API keys.


# API Keys API

List, create, update and delete the **LLM API keys** (prefix `sk-ar-`) your workspace uses for inference.

::endpoint{method="POST" path="/api/v1/keys"}

:::note
The CRUD routes (`GET`/`POST /api/v1/keys`, `PATCH`/`DELETE /api/v1/keys/{hash}`) authenticate with a **Management key** (`ak_…`) carrying `read:llm-keys` / `write:llm-keys` scope, or a signed-in dashboard session. An **LLM key** (`sk-ar-…`) is **not** accepted here, except on `GET /api/v1/auth/key` to inspect itself.
:::

## Request

`POST /api/v1/keys` mints a new LLM key:

| Field | Type | Required | Description |
|---|---|---|---|
| `name` | string | Yes | Human-readable key name. |
| `expires_at` | string \| null | No | ISO 8601 expiration timestamp. |
| `limit` | number \| null | No | Credit cap for the key. |
| `limit_reset` | `daily` \| `weekly` \| `monthly` \| null | No | Rolling window `limit` is measured over. `null` is measured over 30 days, not for the key's lifetime. |
| `include_byok_in_limit` | boolean | No | Whether BYOK usage counts toward the key limit. |
| `rate_limit` | number | No | Requests per minute for this key. |
| `disabled` | boolean | No | Create the key in a disabled state. |
| `allowed_models` | string[] \| null | No | Restrict the key to specific model ids. Ids are matched without regard to case. `anyrouter/auto` and `anyrouter/free` only route to listed models that are in their own pool; to use any other listed model, call it directly. |
| `allowed_endpoints` | string[] \| null | No | Restrict the key to specific API paths. |
| `tpm` | number \| null | No | Tokens-per-minute ceiling. |

### Other routes

| Method & path | Purpose |
|---|---|
| `GET /api/v1/auth/key` | Return metadata about the **LLM key** sent as the Bearer token. |
| `GET /api/v1/key` | OpenRouter-compatible current-key info. With an LLM key (`sk-ar-…`, including sign-in app keys) it returns only that key: `data.label`, `limit`, `limit_remaining`, `usage`, `is_free_tier`, `rate_limit`. With a dashboard session or Management key it returns the workspace's newest active key. |
| `GET /api/v1/keys` | List workspace keys. Pass `?include_disabled=true` to include disabled keys. |
| `GET /api/v1/keys/{hash}` | Return one workspace key by its stored hash. |
| `PATCH /api/v1/keys/{hash}` | Update `name`, `disabled`, `include_byok_in_limit`, `limit`, `limit_reset`, `rate_limit`, or `expires_at`. |
| `DELETE /api/v1/keys/{hash}` | Soft-delete a key by hash. |
| `POST /api/v1/keys/{hash}/rotate` | Issue a new secret for the same key. The old secret stops working immediately; the key's `id`, limits, restrictions and usage history are unchanged. |

## Response

`POST /api/v1/keys` returns the plaintext secret **once** alongside the key record:

```json
{
  "key": "sk-ar-v1-actual-secret-only-shown-once",
  "data": {
    "id": "key_123",
    "hash": "f3b8...",
    "name": "ci-smoketest",
    "label": "sk-ar-v1",
    "created_at": "2026-04-24T00:00:00.000Z",
    "updated_at": null,
    "expires_at": null,
    "disabled": false,
    "real_usage": 0,
    "real_usage_daily": 0,
    "real_usage_weekly": 0,
    "real_usage_monthly": 0,
    "real_usage_monthly_requests": 0,
    "limit": null,
    "limit_remaining": null,
    "limit_reset": null,
    "include_byok_in_limit": false,
    "rate_limit": 30,
    "is_free_tier": true,
    "is_management_key": false,
    "creator_user_id": "user_123",
    "last_used_at": null,
    "allowed_models": ["openai/gpt-5.4-mini"],
    "allowed_endpoints": ["/api/v1/chat/completions"],
    "tpm": null
  }
}
```

`limit_remaining` is `limit` minus spend **in that window** (`daily`: last 24h, `weekly`: 7 days, `monthly` or `null`: 30 days), so it can rise as the window rolls.

`POST /api/v1/keys/{hash}/rotate` returns the same envelope with a **new** plaintext secret and **new** `hash`. `id`, `name`, `limit`, `allowed_*` and spend totals carry over, so `/key?id=` and `api_key_id` references keep resolving. The old secret stops working the moment the call returns. No request body; two concurrent rotates of one key return `409`.

`GET /api/v1/keys` returns a `{ "object": "list", "data": [...] }` envelope of key records (same shape, without the plaintext `key`). `GET /api/v1/auth/key` returns a compact record:

```json
{
  "key": {
    "prefix": "sk-ar-v1",
    "name": "production-backend",
    "created_at": "2026-04-24T00:00:00.000Z",
    "rate_limit": 60,
    "usage": 128,
    "limit": null,
    "is_free_tier": true
  }
}
```

`DELETE /api/v1/keys/{hash}` performs a soft-delete and returns:

```json
{
  "deleted": true,
  "id": "key_123"
}
```

## Examples

::::tabs

:::tab{label="curl"}
```bash
curl https://anyrouter.dev/api/v1/keys \
  -X POST \
  -H "Authorization: Bearer ak_your-management-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "ci-smoketest",
    "rate_limit": 30,
    "allowed_models": ["openai/gpt-5.4-mini"],
    "allowed_endpoints": ["/api/v1/chat/completions"]
  }'
```
:::

:::tab{label="Python"}
```python
import httpx

resp = httpx.post(
    "https://anyrouter.dev/api/v1/keys",
    headers={"Authorization": "Bearer ak_your-management-key"},
    json={
        "name": "ci-smoketest",
        "rate_limit": 30,
        "allowed_models": ["openai/gpt-5.4-mini"],
        "allowed_endpoints": ["/api/v1/chat/completions"],
    },
)
print(resp.json()["key"])  # shown only once
```
:::

:::tab{label="TypeScript"}
```typescript
const resp = await fetch("https://anyrouter.dev/api/v1/keys", {
  method: "POST",
  headers: {
    Authorization: "Bearer ak_your-management-key",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "ci-smoketest",
    rate_limit: 30,
    allowed_models: ["openai/gpt-5.4-mini"],
    allowed_endpoints: ["/api/v1/chat/completions"],
  }),
})
const { key } = await resp.json() // shown only once
```
:::

::::

:::warning
The plaintext `key` is returned **once** on creation and never again. Store it in a secret manager immediately.
:::

## Errors

Key endpoints use the standard error envelope:

```json
{
  "error": {
    "code": "authentication_required",
    "message": "Authentication required",
    "metadata": {
      "type": "authentication_error"
    }
  }
}
```

| Status | Meaning | Fix |
|---|---|---|
| 401 | Missing or invalid credential | Send a valid Management key (`ak_…`) or dashboard session. |
| 403 | Key lacks the required scope | Grant `read:llm-keys` / `write:llm-keys`. |
| 403 `free_key_restricted` | Free-plan account on a disposable or duplicate email inbox, or on an email domain with a burst of new accounts in the last 24 hours | Try again tomorrow, top up at least $2 in total or upgrade to a paid plan. See [Free Tier](/features/free-tier#creating-api-keys-on-the-free-plan). |
| 429 `free_key_ip_limit` | 3 Free-plan keys already created from your network in 24 hours | Try again tomorrow, top up at least $2 in total, or upgrade to a paid plan. |
| 403 `free_key_cap` | A Free-plan account already has 5 active keys | Delete a key you no longer use, upgrade to Go, or buy credits. Keys you already have keep working. |
| 403 `account_banned` | The account is blocked and cannot create keys | Contact support if you think this is a mistake. |
| 404 | No key with that hash | Check the `hash` from a `GET /api/v1/keys` listing. |


## Related

- [Authentication](/docs/getting-started/authentication.md)
- [OAuth](/docs/api-reference/oauth.md)
