# API Keys API

Create API keys for your own customers, list the keys of your account and delete them — without going through the Staan console. Then follow what each key consumes with the [Usage API](/docs/usage).

| Endpoint               | Purpose                                               |
| ---------------------- | ----------------------------------------------------- |
| `POST /v1/keys`        | [Create a key](#create-a-key)                         |
| `GET /v1/keys`         | [List the keys of your account](#list-keys)           |
| `DELETE /v1/keys/{id}` | [Delete a key created through the API](#delete-a-key) |

Base URL: `https://api.staan.ai`. All requests and responses are JSON.

## Authentication

Call these endpoints with an API key **created in the Staan console**:

```bash
Authorization: Bearer YOUR_CONSOLE_API_KEY
```

- **Console keys** can read usage and manage keys.
- **Keys created through `POST /v1/keys`** can only be used for search. On `/v1/keys` and `/v1/usage` they get `403 SCOPE_NOT_GRANTED`. This lets you hand them to your customers safely.

:::warning

Keep your console key on your servers only: anyone holding it can create keys on your account.

:::

## Create a key

```
POST https://api.staan.ai/v1/keys
```

| Field        | Required | Description                                               |
| ------------ | -------- | --------------------------------------------------------- |
| `name`       | yes      | 1 to 255 characters, e.g. your customer's name            |
| `expires_at` | no       | ISO 8601 date in the future; omit or `null` for no expiry |

```bash
curl -X POST https://api.staan.ai/v1/keys \
  -H "Authorization: Bearer YOUR_CONSOLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "Customer A", "expires_at": "2027-12-31T23:59:59Z"}'
```

Response `201`:

```json
{
  "id": "6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b",
  "key": "stn_...",
  "name": "Customer A",
  "expires_at": "2027-12-31T23:59:59.000Z",
  "created_at": "2026-11-02T08:22:46.594Z"
}
```

:::caution{title="The secret is returned only once"}

The `key` field is returned only in this response. Store it right away: it cannot be retrieved later.

:::

- The new key gets the default settings: web search with extra snippets ([Web Search for AI](/docs/web-for-ai)) and the default rate limits.
- An account holds up to **100 keys**.

## List keys

```
GET https://api.staan.ai/v1/keys
```

```bash
curl https://api.staan.ai/v1/keys \
  -H "Authorization: Bearer YOUR_CONSOLE_API_KEY"
```

Returns every key of the account, newest first, without secrets:

```json
[
  {
    "id": "6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b",
    "name": "Customer A",
    "origin": "api",
    "enabled": true,
    "expires_at": "2027-12-31T23:59:59.000Z",
    "created_at": "2026-11-02T08:22:46.594Z"
  }
]
```

`origin` is `console` for keys created in the console and `api` for keys created through `POST /v1/keys`.

## Delete a key

```
DELETE https://api.staan.ai/v1/keys/{id}
```

```bash
curl -X DELETE https://api.staan.ai/v1/keys/6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b \
  -H "Authorization: Bearer YOUR_CONSOLE_API_KEY"
```

Response `200`:

```json
{ "deleted": true, "id": "6f1c2a9e-3b4d-4e5f-8a7b-1c2d3e4f5a6b" }
```

- The key stops working immediately. Its past usage stays in [`GET /v1/usage`](/docs/usage).
- Only keys created through the API can be deleted this way; console keys are managed in the console (`403`).

## Rate limits and errors

These endpoints accept **1 request per second, per key and per endpoint**. Above that they return `429`: wait a second and retry.

| HTTP  | `code` / message        | Cause                                             |
| ----- | ----------------------- | ------------------------------------------------- |
| `400` | invalid field           | Invalid `name`, `expires_at` or `{id}`            |
| `401` | `INVALID_KEY_FORMAT`    | Missing or malformed `Authorization` header       |
| `401` | `INVALID_CREDENTIALS`   | Unknown key or wrong secret                       |
| `403` | `KEY_DISABLED`          | The key is disabled                               |
| `403` | `SCOPE_NOT_GRANTED`     | Key created through the API: use a console key    |
| `403` | `API key limit reached` | 100 keys already on the account (`POST /v1/keys`) |
| `403` | Console key on `DELETE` | Console keys are deleted from the console         |
| `404` | not found               | `{id}` does not belong to your account            |
| `410` | `KEY_EXPIRED`           | The key has expired                               |
| `429` | too many requests       | Rate limit exceeded                               |
