# API authentication

Use an API key for the REST API and MCP. A cabinet session is for a person in a browser or the app, not for scripts.

## The key

Shape: `pb_<mode>_<prefix>_<secret>`. Mode is `test` or `live`. The 12-character prefix is visible in the cabinet and logs. The secret is shown once, at creation.

| How | Header |
| --- | --- |
| Recommended | `Authorization: Bearer <key>` |
| REST alternative | `X-API-Key: <key>` |
| MCP | `Authorization: Bearer <key>` only |

```bash
curl -sS "https://paybot.kz/v1/payments?limit=1" -H "Authorization: Bearer $PAYBOT_API_KEY"
```

A key is bound to one business and one mode. `X-Merchant-Id` is not needed.

## Modes

| Mode | Behaviour |
| --- | --- |
| `test` | Sandbox. Payments are simulated, no Kaspi account is used, no money moves. Sees test data only. |
| `live` | Real payments to your Kaspi account. |

Some setup needs a live key even to configure it (subscription plans, publishing templates): the error is `live_key_required` or `live_only`.

## Scopes

Scopes do not imply each other. Grant only what is needed.

| Scope | Allows |
| --- | --- |
| `read` | Read data. |
| `write` | Create and change, except money. |
| `publish` | Ask to publish something public. A person approves unless the key has `auto_publish`. |
| `broadcast` | Ask to message customers. Always waits for a person. |
| `money` | Refunds and anything that moves money. Needs `allow_money: true` at creation. |

A missing scope returns `403 insufficient_scope`.

## Creating a key

Cabinet only (`POST /v1/api-keys` with a session). An API key cannot create keys.

| Field | Description |
| --- | --- |
| `mode` | `live` or `test`. Required. |
| `scopes` | 1 to 5 scopes. Required. |
| `name` | A label for the cabinet. |
| `expires_in_days` | Lifetime, 1 to 3650. Omit for a key that never expires. |
| `allowed_ips` | Up to 32 addresses or CIDRs. Omit to allow any. |
| `allow_money` | `true` when scopes contain `money`. |
| `grants.auto_publish` | Lets the key publish without approval. Needs the `publish` scope. |

## Rotation

`POST /v1/api-keys/{id}/rotate` (cabinet) issues a successor with the same mode, scopes and IP list. The old key keeps working for `grace_hours` (default 24, at most 168). The new key does not inherit the old expiry.

1. Issue the new key.
2. Deploy it.
3. Verify a call with it.
4. Let the old key lapse, or revoke it with `DELETE /v1/api-keys/{id}`.

Revoked, expired and unknown keys all return the same `401`.

## OAuth

For connectors that cannot send a header (Claude.ai, ChatGPT web), see [OAuth for MCP connectors](https://paybot.kz/docs/oauth-mcp.md) (Russian).
