# API keys

Create, change, replace and revoke the team's API keys from code.

Web version: https://www.svanja.com/docs/api/api-keys/

These endpoints let you manage your team's API keys from code. They work with the same keys you see in [Settings](https://app.svanja.com/settings) under **API Keys**, which is handy if you want to rotate keys on a schedule or issue limited-access keys for your own tools.

To call these endpoints, your key needs access to the API keys area. Read-only keys don't have it, and only a Full access key can grant another key permission to manage keys. A key can never give another key more access than it has itself. For more on what keys are and how access works, see [The REST API](https://www.svanja.com/docs/api/).

`POST /api-keys` creates a new key, and `POST /api-keys/{apiKey}/rotate` replaces an existing one. Both return the new secret only once in `data.access_token`, so save it right away. When you rotate a key, the old one stops working immediately, and the new one keeps the same name, access, and expiration.

A key can update its own display name. To rotate it, revoke it, or change its access, you'll need to use the app or another key with the right permissions.

The CLI sign-in endpoints (`/cli-auth/...`) are what `svanja login` uses to fetch a key through your browser. They don't require an API key and aren't meant for your own code.

## Operations

| Operation | What it does | Effect | Endpoints |
| --- | --- | --- | --- |
| `api_key.read` | Read team API keys. | Reads only | `GET /api-keys`<br>`GET /api-keys/{apiKey}` |
| `api_key.write` | Create or manage API keys. | Changes access or automation | `POST /api-keys`<br>`PATCH /api-keys/{apiKey}`<br>`POST /api-keys/{apiKey}/rotate`<br>`POST /api-keys/{apiKey}/revoke` |

## Endpoints

### GET /api/v1/api-keys

List API keys

Lists non-secret metadata for API keys in the authenticated team. Requires API key read management capability.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `page` | query | integer | no | Page number. Defaults to 1. |
| `per_page` | query | integer | no | Page size. Defaults to 25 and is capped at 100. |

### POST /api/v1/api-keys

Create an API key

Creates an API key and returns the plaintext bearer value once. This secret-issuing endpoint rejects Idempotency-Key and is not replayable.

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `name` | string | yes |  |
| `preset` | one of full_access, read_only, custom | no |  |
| `group_selections` | array of object | no |  |
| `project_scope` | one of all_projects, selected_projects | no |  |
| `selected_project_public_ids` | array of string | no |  |
| `expires_at` | string or null | no |  |

### GET /api/v1/api-keys/{apiKey}

Show an API key

Shows non-secret metadata for one API key by display id. Missing, malformed, cross-team, deleted-token, and sidecar-missing targets return resource_not_found after the read capability check.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes | API key display id in ak_<ULID> form. This is not the internal numeric token row id. |

### PATCH /api/v1/api-keys/{apiKey}

Update an API key

Partially updates API key metadata from JSON body fields only. The final target state must fit within the managing key's capabilities, project scope, selected projects, and lifetime. This non-secret write supports idempotency replay.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes | API key display id in ak_<ULID> form. This is not the internal numeric token row id. |
| `Idempotency-Key` | header | string | no | Optional key for unsafe bearer writes. Reusing the same key and same canonical body replays the original response for 24 hours; reusing it with a different body returns idempotency_conflict. |

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `name` | string | no |  |
| `preset` | one of full_access, read_only, custom | no |  |
| `group_selections` | array of object | no |  |
| `project_scope` | one of all_projects, selected_projects | no |  |
| `selected_project_public_ids` | array of string | no |  |
| `expires_at` | string or null | no |  |

### POST /api/v1/api-keys/{apiKey}/rotate

Rotate an API key

Revokes the source key and creates a replacement with the same configuration. The plaintext replacement bearer value is returned once. This secret-issuing endpoint rejects Idempotency-Key.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes | API key display id in ak_<ULID> form. This is not the internal numeric token row id. |

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `expires_at` | string or null | no | Required only when rotating an expired source key. |

### POST /api/v1/api-keys/{apiKey}/revoke

Revoke an API key

Soft-revokes a same-team non-self API key. Repeated revoke calls are idempotent no-ops and this non-secret write supports idempotency replay.

| Parameter | In | Type | Required | Notes |
| --- | --- | --- | --- | --- |
| `apiKey` | path | string | yes | API key display id in ak_<ULID> form. This is not the internal numeric token row id. |
| `Idempotency-Key` | header | string | no | Optional key for unsafe bearer writes. Reusing the same key and same canonical body replays the original response for 24 hours; reusing it with a different body returns idempotency_conflict. |

### GET /api/v1/cli-auth/discovery

Discover CLI device authorization settings

Returns stable feature status and endpoint metadata for first-party CLI login. This endpoint intentionally runs before a bearer key exists.

Works without an API key.

### POST /api/v1/cli-auth/device

Create a CLI device authorization request

Creates a short-lived browser authorization request and returns the plaintext device code once. CLI metadata is optional and sanitized.

Works without an API key.

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `client_kind` | string | no | Forbidden in the legacy omitted-client-kind request arm. |
| `preset` | string | no | Forbidden unless client_kind is local_agent. |
| `group_selections` | array of object | no | Forbidden unless client_kind is local_agent. |
| `project_scope` | string | no | Forbidden unless client_kind is local_agent. |
| `selected_project_public_ids` | array of object | no | Forbidden unless client_kind is local_agent. |
| `expires_at` | string | no | Forbidden unless client_kind is local_agent. |
| `client_name` | string or null | no | Optional client-declared name. Control characters are sanitized. |
| `cli_version` | string or null | no | Optional client-declared version metadata. Control characters are sanitized. |
| `key_name` | string or null | no | Optional API key name. null is treated as omitted. A non-null value must be non-empty after sanitization. When omitted or null the key is named "Svanja CLI", or "Svanja CLI (<client_name>)" when client_name is present. |

The body takes one of several shapes; see the OpenAPI document for each.

### POST /api/v1/cli-auth/token

Poll for CLI device authorization completion

Polls a pending device authorization request. It returns an issued API key once after browser approval, or a public polling error while the request is pending, denied, expired, invalid, unsupported, or too fast.

Works without an API key.

| Body field | Type | Required | Notes |
| --- | --- | --- | --- |
| `grant_type` | always "urn:ietf:params:oauth:grant-type:device_code" | yes |  |
| `device_code` | string | yes |  |
