# Create an API key; the secret is shown once

POST /v1/api-keys: Create an API key; the secret is shown once

```http
POST /v1/api-keys
```

|  |  |
|---|---|
| Operation id | `createApiKey` |
| Key scope | `admin`: admin keys only |
| Section | [API keys](https://docs.kaab.ist/reference/api#api-keys) |

## Request body

| Field | Type | Required | Description |
|---|---|---|---|
| `name` | string | yes | min length 1 · max length 64 |
| `scopes` | `"run"`, `"read"`, `"admin"`[] | yes | min items 1 |
| `mode` | `"live"`, `"test"` |  | default `"live"` |
| `expires_at` | string (date-time) |  |  |

## Example request

```bash
curl -X POST "https://api.kaab.ist/v1/api-keys" \
  -H "Authorization: Bearer $KAAB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"ci","scopes":["run","read"],"mode":"live","expires_at":"2027-01-01T00:00:00Z"}'
```

## Responses

### 201

Key

| Field | Type | Required | Description |
|---|---|---|---|
| `id` | string | yes | pattern `^key_[0-9a-hjkmnp-tv-z]{26}$` |
| `object` | `"api_key"` | yes |  |
| `name` | string | yes |  |
| `prefix` | string | yes |  |
| `scopes` | `"run"`, `"read"`, `"admin"`[] | yes | min items 1 |
| `mode` | `"live"`, `"test"` | yes |  |
| `last_used_at` | string or null (date-time) | yes |  |
| `expires_at` | string or null (date-time) | yes |  |
| `created_at` | string (date-time) | yes |  |
| `key` | string | yes | pattern `^sk_(live\|test)_[A-Za-z0-9]{32,}$` |

## Errors

Every error answers with the [error envelope](https://docs.kaab.ist/reference/api/schemas/error-response); its `code` is one of the [error codes](https://docs.kaab.ist/reference/codes#errors).

| Status | When |
|---|---|
| 400 | `request_invalid`: request validation failed |
| 401 | Missing or invalid API key |
| 402 | `insufficient_credits`: top up to continue |
| 403 | The key's scope does not cover this operation |
| 409 | Conflicting state (see `error.code`) |
| 422 | Semantically invalid request (see `error.code`) |
| 429 | Rate limited; see `Retry-After` |
| 500 | `internal`: unexpected server error |
