# Authentication

Every request carries an API key in a header:

```http
Authorization: Bearer ut_live_...
```

`x-api-key: ut_live_...` is accepted too. A key in the URL or the body is never read.

## Keys

- Create them in [Settings → API](/settings?p=api). API access opens after the first payment on the account; a [test key](#test-keys) can be created earlier.
- The secret is shown once. We keep only its fingerprint and cannot recover it: lost a key, revoke it and create a new one.
- All keys of an account spend one balance. Separate keys per project let you see spend separately and revoke them one by one.
- Revoking works at once: the next request with that key gets `401 api_key_revoked`.

## Per-key spend limit

In your account a key can have a limit in tokens and a reset period: never, daily, weekly, monthly. A request that does not fit into the rest of the limit gets `402 key_spend_limit_reached`. A limit and the `mcp` scope exclude each other: a key with a limit cannot connect to MCP, and a key with `mcp` cannot get a limit (the account answers with the `mcp_with_limit` error). The rest is in `GET /v1/key`:

```bash
curl https://api.unitool.ai/v1/key -H "Authorization: Bearer $UNITOOL_API_KEY"
```

```json
{
  "object": "api_key",
  "id": "Ab3dE5fG7hJ9",
  "name": "server",
  "prefix": "ut_live_Ab3dE5fG7hJ9",
  "mode": "live",
  "scopes": ["inference", "generations", "files", "read"],
  "allowed_ips": [],
  "limit_tokens": 5000,
  "limit_reset": "monthly",
  "usage_tokens": 1240.5,
  "usage_requests": 318,
  "limit_remaining_tokens": 3759.5,
  "rate_limit": { "requests_per_minute": 600, "concurrent_requests": 100 }
}
```

## Test keys

Keys come in two kinds. `ut_live_...` calls models and spends your balance. `ut_test_...` is a test key: the same scopes, rate limits, IP list and request validation, but it never calls a model and never charges. A test key can be created before the first payment.

- Chat, `/v1/messages` and `/v1/responses` validate the request as usual (unsupported parameters, switched-off search, an input longer than the context) and answer with a fixed text, streamed too. `cost_tokens: 0`, header `x-unitool-test: true`.
- `POST /v1/generations` checks the model, mode and input, then creates a `gen_test_...` that succeeds about 3 seconds later with a placeholder file. Canceling while it runs gives `canceled`. Webhooks are not sent for test generations.
- `POST /v1/quotes` answers with price 0 and `test: true`. `POST /v1/uploads` returns a `file_ref` for test generations and no upload link.
- A test key reads the model catalog, the key itself, its own test generations and test receipts (`GET /v1/requests` with `test: true`). Balance, usage, live generations, `/v1/images/*`, `/v1/audio/speech`, `DELETE /v1/generations/{id}` and everything else: `400 not_available_in_test_mode`.
- Test keys do not work with MCP (`403 test_key_not_supported`): MCP calls are real and billed.

`GET /v1/key` returns `mode: "live"` or `"test"`.

## IP allowlist

A key can be limited to up to 20 IP addresses or CIDR ranges (IPv4 and IPv6) in [Settings → API](/settings?p=api). A request from another address gets `403 ip_not_allowed`; `details.ip` shows the address we saw. An empty list means any address. The list applies to MCP too. `GET /v1/key` shows it in `allowed_ips`.

## Rotation

"Rotate" gives the key a new secret and keeps its id, name, scopes, limits and IP list. Choose how long the current secret keeps working: stop right away, 1 hour, 24 hours or 7 days. The new key is shown once. Rotating again during that time stops the oldest secret at once.

## Leaks

We take part in GitHub secret scanning: a key (live or test) pushed to a public repository is revoked automatically and we email you. Create a new key and remove the old one from the code and its history. If a key leaks during a rotation's grace period, only the leaked secret stops working.

## Scopes

A new key gets four scopes by default: `inference`, `generations`, `files`, `read`. The `mcp` scope is chosen separately at creation; a key's scopes can be changed in your account.

| Scope | Allows |
|---|---|
| `inference` | `POST /v1/chat/completions`, `POST /v1/messages`, `POST /v1/messages/count_tokens`, `POST /v1/responses` |
| `generations` | `POST /v1/generations`, `POST /v1/generations/{id}/cancel`, `DELETE /v1/generations/{id}`, `POST /v1/quotes`, `/v1/images/*`, `/v1/audio/speech` |
| `files` | `POST /v1/uploads` |
| `read` | Every `GET`: models, balance, key, usage, receipts, generations, `openapi.json`. `GET /v1/generations` and `GET /v1/generations/{id}` also work with `generations` alone |
| `mcp` | Connecting [Unitool MCP](/mcp) with this key, see below. Not among the defaults |

A key without the scope gets `403 insufficient_scope`; the message names the scope. `limit_reset: null` means the limit never resets.

## An API key in MCP

An agent that cannot sign in through a browser can use an API key with the `mcp` scope and no spend limit:

```http
Authorization: Bearer ut_live_...
```

The key gives the same tools and models as signing in and spends the same balance. MCP calls are billed at MCP prices, API prices do not apply to them, and they are not counted against the key's spend. That is why a key with a spend limit is not let into MCP.

| Answer | Reason |
|---|---|
| `403 insufficient_scope` | The key lacks the `mcp` scope |
| `403 key_limit_not_supported` | The key has a spend limit |
| `403 test_key_not_supported` | The key is a test key |
| `403 ip_not_allowed` | The request is not from an IP the key allows |
| `403 api_access_not_enabled` | API access of the account is closed |

An MCP refusal comes as `{"error": "...", "code": "..."}`; the codes are in [Errors](/developers/errors#key_limit_not_supported).

## Why a request is refused

| Answer | Reason |
|---|---|
| `401 invalid_api_key` | Mistyped, foreign or deleted key |
| `401 api_key_revoked` | The key was revoked |
| `403 payment_required` | The account has no payment yet |
| `403 api_access_not_enabled` | The API is not open for the account yet (closed beta) |
| `403 account_suspended` | API access is suspended, contact support |
| `403 ip_not_allowed` | The request is not from an IP the key allows |

The full list is in [Errors](/developers/errors).

## Security

- Never ship the key in browser or mobile code: it will be extracted and your balance spent. Call the API from your server.
- Put a spend limit on keys that live on machines others can reach, such as CI.
- A key ended up in a public repository: revoke it right away.
