# Keys API

## Read managed credential cache policy and local counters

`GET /api/v1/admin/security/managed-key-cache` · operation `getManagedKeyCachePolicy`

Safe global admin/viewer/operator projection. Available for staging even when managed_key_cache is off. Returns reviewed YAML/console policy, eligibility and process-local accounted counters, never credential hashes, private proof or tokens. Named administrative sessions remain live. Active means policy eligibility, not database health, current credential validity or inference success. Private/no-store.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Effective policy, source and serving-replica cache counters | `application/json`: [ManagedKeyCacheState](/docs/api/schemas/m/#managedkeycachestate) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | Policy storage or current control refresh unavailable | — |


## Replace the complete managed credential cache policy

`PUT /api/v1/admin/security/managed-key-cache` · operation `putManagedKeyCachePolicy`

Global administrator features:manage plus same-origin session/CSRF. Requires current global version and semantic YAML base fingerprint; policy may be staged while the operational flag is off. Complete four-field replacement, strict nonnull/canonical/unique fields, maximum4KiB body. Publication flushes local cache, including identical PUT; replicas adopt the control version through normal refresh. No restart or plaintext credential is involved. A committed-but-unsynced outcome is503 configuration_sync_pending; GET current state before retrying.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer, required, min 0)
  - `expected_base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `managed_key_cache` ([ManagedKeyCachePolicy](/docs/api/schemas/m/#managedkeycachepolicy), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Policy committed and locally refreshed; GET the new state/version | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | Current version or YAML base fingerprint changed; explicit review required | — |
| `413` | Policy request exceeds4KiB | — |
| `422` | Invalid policy domain, precision or capacity | — |
| `503` | Storage unavailable or committed policy synchronization pending | — |


## Revert managed credential cache policy to YAML/defaults

`DELETE /api/v1/admin/security/managed-key-cache` · operation `revertManagedKeyCachePolicy`

Global administrator features:manage plus same-origin session/CSRF. Both current guards required; does not change the operational feature flag. Revert invalidates local positive state and is audited without credential content.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer, required, min 0)
  - `expected_base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Override removed and locally refreshed; GET the new state/version | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | No console override exists | — |
| `409` | Version or YAML base changed | — |
| `413` | Request exceeds4KiB | — |
| `503` | Storage unavailable or synchronization pending | — |


## GET /api/v1/admin/security/key-policy

`GET /api/v1/admin/security/key-policy` · operation `getKeySecurityPolicy`

Global admin/viewer/operator metadata. Scoped consoles use only Config.key_policy_view; no host lifecycle policy or override is disclosed to them.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeySecurityPolicyState](/docs/api/schemas/k/#keysecuritypolicystate) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## PUT /api/v1/admin/security/key-policy

`PUT /api/v1/admin/security/key-policy` · operation `putKeySecurityPolicy`

Global administrator replaces complete prospective policy+lifecycle under reviewed global control version and YAML base fingerprint. May stage while flags are off. Validates the whole candidate and current license; Never remains available.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `expected_base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)
  - `key_policy` ([ManagedKeyPolicy](/docs/api/schemas/m/#managedkeypolicy), required)
  - `key_lifecycle` ([KeyLifecyclePolicy](/docs/api/schemas/k/#keylifecyclepolicy), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Committed; reload current policy before another edit. | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## DELETE /api/v1/admin/security/key-policy

`DELETE /api/v1/admin/security/key-policy` · operation `revertKeySecurityPolicy`

Global administrator removes the override and restores the immutable YAML policy/defaults under the same control CAS/base/license guards; no working credential is revoked.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `expected_base_fingerprint` (string, required, pattern `^[a-f0-9]{64}$`)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Committed; reload current policy before another edit. | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## GET /api/v1/admin/keys/{id}/tags

`GET /api/v1/admin/keys/{id}/tags` · operation `getManagedKeyTags`

Read selected managed key static metadata and advisory selected-key write capability under active team membership. Remains readable with new metadata flag/plan/license off.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeyTagsState](/docs/api/schemas/k/#keytagsstate) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## PUT /api/v1/admin/keys/{id}/tags

`PUT /api/v1/admin/keys/{id}/tags` · operation `putManagedKeyTags`

Replace the complete static metadata map, including explicit{} clear. Requires key_metadata_tags, live keys:rotate authority/delegation plus tenant/organization plan/license. tags_version is independent of credential/owner/control versions. Prospective required-tags validation applies to this repair only, preserving unrelated legacy owner drift.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `tags` ([StaticKeyTags](/docs/api/schemas/s/#statickeytags), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeyTagsState](/docs/api/schemas/k/#keytagsstate) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## GET /api/v1/admin/keys/{id}/rotation-schedule

`GET /api/v1/admin/keys/{id}/rotation-schedule` · operation `getManagedKeyRotationSchedule`

Read selected-key one-shot schedule metadata and separate prepare/confirm/cancel capabilities. No secret or nonce retrieval. Historical metadata remains readable under legacy key rights when new processing is disabled.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeyRotationScheduleState](/docs/api/schemas/k/#keyrotationschedulestate) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## POST /api/v1/admin/keys/{id}/rotation-schedule

`POST /api/v1/admin/keys/{id}/rotation-schedule` · operation `prepareManagedKeyRotationSchedule`

Prepare one inactive future credential now, return secret and independent confirmation nonce once with private no-store. Persist only hashes. Preparation is unarmed and does not change the current generation or retirement; lost handoff requires cancel/restage. Named actor is revalidated at activation; shared/bootstrap intent remains durable after token/login changes. No recurring secret generation/delivery.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: [KeyRotationSchedulePrepareInput](/docs/api/schemas/k/#keyrotationscheduleprepareinput)

**Responses**

| Status | Description | Body |
|---|---|---|
| `201` | Private no-store metadata or one-time handoff. | `application/json`: [KeyRotationSchedulePrepared](/docs/api/schemas/k/#keyrotationscheduleprepared) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## DELETE /api/v1/admin/keys/{id}/rotation-schedule

`DELETE /api/v1/admin/keys/{id}/rotation-schedule` · operation `cancelManagedKeyRotationSchedule`

Cancel only a pending prepared/armed/blocked schedule under live keys:rotate and schedule CAS. Recovery remains available if new flag, plan or license is off/expired. Does not revoke or replace the working credential; activated schedules cannot be cancelled to resurrect an old secret.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeyRotationSchedule](/docs/api/schemas/k/#keyrotationschedule) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## POST /api/v1/admin/keys/{id}/rotation-schedule/confirm

`POST /api/v1/admin/keys/{id}/rotation-schedule/confirm` · operation `confirmManagedKeyRotationSchedule`

Explicitly confirm saved secret possession with the separate one-time nonce and current schedule CAS, then arm this one rotation. Requires current scheduling flag/tenant/license plus live keys:rotate authority. Never automatically confirms preparation.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `confirmation_nonce` (string, required, min length 64, max length 64, pattern `^[a-f0-9]{64}$`)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store metadata or one-time handoff. | `application/json`: [KeyRotationSchedule](/docs/api/schemas/k/#keyrotationschedule) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature/managed key/override not found, or key outside current scope. | — |
| `409` | Control/base/generation/schedule CAS changed, pending rotation or schedule state/confirmation conflict. | — |
| `422` | Prospective key policy or whole configuration dependencies reject the mutation. | — |
| `503` | Storage or admitted configuration unavailable; unknown outcome requires reload, not blind retry. | — |


## GET /api/v1/admin/keys/{id}/detail

`GET /api/v1/admin/keys/{id}/detail` · operation `keysIdentityDetail`

Requires identity_details, identity_inventory, identity_management and keys:read. Reads exact identity metadata, configured access ceiling, persisted usage and bounded target audit in one read-only repeatable-read snapshot. Limits are a separate current observation. Global users:manage is required for console users. Other identities respect live session memberships; foreign or scoped-deleted IDs return 404. No tokens, hashes or credential material. Summary and recent requests require their respective usage_analytics/request_logs flags and actions. Audit requires global audit:read plus audit_explorer. RPM and direct key/team quota projections require global rates:read/quotas:read and the corresponding operational flags; other cumulative limits may still apply. Console user names/memberships never imply inference ownership. Both from and to must be provided together, or both omitted for the previous 24 hours. Unknown, duplicate and empty query parameters return 400. Range is positive and at most 90 days. Disabled, restricted and unavailable sections are explicit; empty usage does not prove no traffic. Configured policy and feature states are informational, never an authorization grant. Cache-Control no-store.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `from` | query | string (date-time) | Start of the usage window shown, RFC 3339. |
| `to` | query | string (date-time) | End of the usage window shown, RFC 3339. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Authorized identity detail with explicit section availability and bounded metadata | `application/json`: [IdentityDetail](/docs/api/schemas/i/#identitydetail) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Disabled feature or identity not found within authorized scope | — |
| `503` | Identity authority or requested detail section unavailable | — |


## GET /api/v1/admin/keys/{id}/savings

`GET /api/v1/admin/keys/{id}/savings` · operation `keySavings`

What the key saved per UTC day and in total (ADR 0132): the gateway cache (tokens of the stored answer at the model's price), the provider's prompt caching (cached input tokens times the difference between the input and the published cache-read price) and the auto router (cost at the most expensive candidate minus the cost of the chosen model). Dollars use today's automatic price of each model and are estimates; requests without a price count tokens and are reported as unpriced_requests. Same gates and scope as the key detail, plus usage_analytics and analytics:read. from and to together, at most 90 days.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `from` | query | string (date-time) | Start of the time window, an RFC 3339 timestamp. |
| `to` | query | string (date-time) | End of the time window, an RFC 3339 timestamp. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `403` | Missing permission | — |
| `404` | Feature disabled or key not visible | — |


## GET /api/v1/admin/keys/{id}/auto-route-usage

`GET /api/v1/admin/keys/{id}/auto-route-usage` · operation `keyAutoRouteUsage`

Auto-routed traffic of the key per routed alias (ADR 0132): turns, sessions (a turn without a session ID is a session of its own), averages per session, the models chosen, the cost against the most expensive candidate, and the provider prompt-cache hit rate in three buckets by what the router did: same model as the previous turn of the session, a model the session had not used, or a return to one it used. Reads at most 20000 turns (truncated says when more exist). Same gates as the savings report.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^[a-z][a-z0-9_-]{1,63}$` |  |
| `from` | query | string (date-time) | Start of the time window, an RFC 3339 timestamp. |
| `to` | query | string (date-time) | End of the time window, an RFC 3339 timestamp. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | OK | — |
| `400` | Invalid input | — |
| `403` | Missing permission | — |
| `404` | Feature disabled or key not visible | — |


## GET /api/v1/admin/keys/page

`GET /api/v1/admin/keys/page` · operation `listManagedKeyInventory`

Requires identity_inventory and identity_management. Managed PostgreSQL keys only; YAML bootstrap keys remain in the legacy keys directory. Requires keys:read. Scope is applied before SQL count and pagination. Count/page use one repeatable-read snapshot; default ordering is created_at DESC, id DESC; sort=name selects alias for keys or name for teams/users. Sort and order are closed enums applied to the full authorized set; ties use ID in the same direction. Duplicate or unknown query parameters are rejected. Omitted status/all includes active, revoked and expired keys but excludes deleted rows. Deleted is explicit and global-only. No bearer tokens or hashes. Total is the matching authorized inventory, not provider usage. Cache-Control no-store.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `limit` | query | integer, default `25`, min 1, max 100 | Most keys on this page, 1-100 (25 by default). |
| `offset` | query | integer, default `0`, min 0, max 10000 | Rows to skip before this page, for pagination. |
| `search` | query | string, max length 128 |  |
| `team_id` | query | string, max length 64, pattern `^([a-z][a-z0-9_-]{1,63})?$` |  |
| `status` | query | string, one of `"active"`, `"revoked"`, `"expired"`, `"deleted"`, `"all"` | Only keys in this state; all, the default, is active, revoked and expired; deleted is global-only. |
| `kind` | query | string, one of `"virtual"`, `"service_account"` | Only virtual keys or only service accounts. |
| `sort` | query | string, one of `"created_at"`, `"name"`, `"id"`, default `"created_at"` | Order by creation time (default), alias or ID. |
| `order` | query | string, one of `"asc"`, `"desc"`, default `"desc"` | Sort direction: asc or desc. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Bounded page; truncated means additional matches exist, not authorization bypass | `application/json`: object |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `503` | Identity authority or bounded inventory query unavailable | — |

Response `200` fields:

- `application/json`: object
  - `items` (array of [KeyInventoryItem](/docs/api/schemas/k/#keyinventoryitem), required, max items 100)
  - `total` (integer (int64), required, min 0)
  - `limit` (integer, required, min 1, max 100)
  - `offset` (integer, required, min 0, max 10000)
  - `truncated` (boolean, required)


## GET /api/v1/admin/keys

`GET /api/v1/admin/keys` · operation `listAPIKeys`

List managed and YAML key metadata. Scoped roles see only keys in active memberships. Never includes bearer secrets or hashes.

**Authentication:** `adminSession`

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Key metadata, wrapped in data | — |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |


## POST /api/v1/admin/keys

`POST /api/v1/admin/keys` · operation `createAPIKey`

Generate a random key. Global admin may select any active team; team_admin may select only an active membership and must specify routes/models without wildcards. With team_policy_enforcement, every role is held to the effective team policy; a team without one answers 403 team_policy_required, and a model or route the ceiling would remove answers 403 outside_team_policy, listing them; * and wildcard entries mean whatever the team allows. The bearer secret is returned exactly once; only SHA-256 is stored. Expiration is OPTIONAL: missing/sole null means Never, including rotation. Choose expires_at or expires_in exclusively. Explicit expiration is DB-clock checked; an optional prospective maximum applies only to that choice. Static metadata tags are separate from request tags and grants.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `alias` (string, required, min length 2, max length 128)
  - `kind` (string, required, one of `"virtual"`, `"service_account"`)
  - `principal` (string, required, min length 2, max length 128)
  - `team` (string, required) — Existing team ID
  - `allowed_models` (array of string, required, min items 1)
  - `scopes` (array of string, required, min items 1) — Explicit route:&lt;capability&gt;, route:models, route:* or mcp:read/mcp:execute scopes
  - `allowed_source_cidrs` ([SourceCIDRList](/docs/api/schemas/s/#sourcecidrlist)) — Optional independent source ceiling. Omitted or [] is unrestricted. May be staged while source_ip_controls is off; enforcement requires the boot flag.
  - `expires_at` ([ManagedKeyExpiryDate](/docs/api/schemas/m/#managedkeyexpirydate) or null) — Optional absolute RFC3339 date with explicit timezone. Missing or sole null means Never. Future and optional prospective maximum checked under the database clock. Cannot coexist with expires_in, even when null.
    - Any of (1): [ManagedKeyExpiryDate](/docs/api/schemas/m/#managedkeyexpirydate)
    - Any of (2): null
  - `owner` ([KeyOwner](/docs/api/schemas/k/#keyowner) or null) — Optional attribution owner bound in the same transaction. Requires identity_ownership and a global administrator; grants no model or route.
    - One of (1): [KeyOwner](/docs/api/schemas/k/#keyowner)
    - One of (2): null
  - `require` ([AttributionRequirement](/docs/api/schemas/a/#attributionrequirement)) — Optional attribution the key's requests must declare, on top of its team's (400 invalid_require when a flag it needs is off or a tag is outside the taxonomy)
  - `expires_in` ([ManagedKeyDuration](/docs/api/schemas/m/#managedkeyduration) or null) — Optional positive integer followed by s, m, h, d or w, at most 9223372036 seconds. Missing or sole null means Never. Database-clock issuance anchor; scheduled rotations instead anchor at actual activation. Cannot coexist with expires_at, even when null.
    - Any of (1): [ManagedKeyDuration](/docs/api/schemas/m/#managedkeyduration)
    - Any of (2): null
  - `tags` ([StaticKeyTags](/docs/api/schemas/s/#statickeytags))

**Responses**

| Status | Description | Body |
|---|---|---|
| `201` | Key metadata (with require and require_version) and one-time bearer secret; Cache-Control no-store | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | Generated key ID collision; retry | — |
| `422` | Prospective key policy rejects explicit expiry or required owner/static tags. | — |


## POST /api/v1/admin/keys/{id}/revoke

`POST /api/v1/admin/keys/{id}/revoke` · operation `revokeAPIKey`

Immediately reject future requests using a managed key. team_admin may revoke only keys in active memberships; foreign IDs return 404. YAML keys are changed by the operator.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Managed key revoked | — |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Active managed key not found or feature disabled | — |


## DELETE /api/v1/admin/keys/{id}

`DELETE /api/v1/admin/keys/{id}` · operation `deleteAPIKey`

Revoke and soft-delete a managed key; preserve usage and audit history. team_admin may delete only keys in active memberships; foreign IDs return 404. YAML keys are operator-owned.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Managed key deleted | — |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Managed key not found or feature disabled | — |


## PUT /api/v1/admin/keys/{id}/owner

`PUT /api/v1/admin/keys/{id}/owner` · operation `setAPIKeyOwner`

Global admin binds, changes or clears the attribution owner of a managed key. Requires identity_ownership and the current owner_version. Affects only requests accepted afterwards; persisted events and durable receipts keep the owner captured at acceptance. Ownership never grants models, routes, membership or console roles. YAML key owners are changed in configuration.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^(vk\|sa)_` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `owner` ([KeyOwner](/docs/api/schemas/k/#keyowner) or null, required)
    - One of (1): [KeyOwner](/docs/api/schemas/k/#keyowner)
    - One of (2): null
  - `version` (integer (int64), required, min 0) — owner_version from the key listing

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Managed key metadata with the new owner and owner_version | — |
| `400` | Invalid owner: unknown, disabled, cross-team workload or team-scoped user outside the key team | — |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Managed key not found or feature disabled | — |
| `409` | Stale owner_version | — |


## GET /api/v1/admin/keys/{id}/source-cidrs

`GET /api/v1/admin/keys/{id}/source-cidrs` · operation `getAPIKeySourceCIDRs`

Read the normalized key source policy and its independent version. Requires identity_management and keys:read with current team scope; YAML keys are read-only. Available for staging when source_ip_controls is off; enabled describes that operational flag, not permission or source health. No query parameters accepted.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Private no-store policy metadata; no bearer secret or hash | `application/json`: [KeySourceCIDRPolicy](/docs/api/schemas/k/#keysourcecidrpolicy) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Key not visible or identity management disabled | — |
| `503` | Authoritative policy storage unavailable | — |


## PUT /api/v1/admin/keys/{id}/source-cidrs

`PUT /api/v1/admin/keys/{id}/source-cidrs` · operation `replaceAPIKeySourceCIDRs`

Replace the complete managed-key source ceiling using its current policy version. Requires keys:manage and global administrator or current scoped team-administrator authority, revalidated transactionally. Plain-member key actions do not delegate this policy. [] explicitly clears only this ceiling; models, scopes and all other policy remain unchanged. Versioned and audited; credential rotation preserves the policy. YAML-owned policy returns409. No query parameters accepted; may stage while enforcement is off.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `allowed_source_cidrs` ([SourceCIDRList](/docs/api/schemas/s/#sourcecidrlist), required)

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Updated normalized private no-store policy | `application/json`: [KeySourceCIDRPolicy](/docs/api/schemas/k/#keysourcecidrpolicy) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Managed key not visible or identity management disabled | — |
| `409` | Stale policy version or YAML-owned policy | — |
| `413` | Request body exceeds 16 KiB | — |
| `503` | Policy storage unavailable; reload before retrying an uncertain write | — |


## PUT /api/v1/admin/keys/{id}/require

`PUT /api/v1/admin/keys/{id}/require` · operation `setAPIKeyRequire`

Replace a managed key's own attribution requirement (T140). Global admin, or team_admin for keys of the teams they administer (other keys answer 404). Requires identity_management and the current require_version. The team's requirement still applies on top. A non-empty requirement needs attribution_requirements and the flags it names; clearing is always accepted. Applies from the key's next request. YAML keys take require from configuration.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required, pattern `^(vk\|sa)_` |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `require` ([AttributionRequirement](/docs/api/schemas/a/#attributionrequirement), required)
  - `version` (integer (int64), required, min 0) — require_version from the key listing

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Managed key metadata with the new require and require_version | — |
| `400` | invalid_require: missing fields, unknown fields, a flag the requirement needs is off, or a tag outside usage_tags.allowed_values | — |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Managed key not found or feature disabled | — |
| `409` | Stale require_version | — |


## POST /api/v1/admin/keys/{id}/disable

`POST /api/v1/admin/keys/{id}/disable` · operation `disableManagedKey`

Disable a managed key - it stops authenticating at once and keeps its secret. Requires keys:manage, CSRF and reversible_disable. Audited.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |
| `id` | path | string, required, max length 128 |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Done | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled or not found (another organization's object is not found) | — |
| `409` | Already in that state, revoked, or YAML-owned | — |
| `503` | Storage unavailable | — |


## POST /api/v1/admin/keys/{id}/enable

`POST /api/v1/admin/keys/{id}/enable` · operation `enableManagedKey`

Enable a disabled key with the same secret. A revoked key cannot be enabled. Requires keys:manage, CSRF and reversible_disable. Audited.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |
| `id` | path | string, required, max length 128 |  |

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Done | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled or not found (another organization's object is not found) | — |
| `409` | Already in that state, revoked, or YAML-owned | — |
| `503` | Storage unavailable | — |


## POST /api/v1/admin/keys/{id}/rotate

`POST /api/v1/admin/keys/{id}/rotate` · operation `rotateManagedKeyCredential`

Requires key_rotation, keys:manage and CSRF. Stable key identity, scope and team remain. Maximum two active credentials, overlap 0–86400 seconds; PostgreSQL time enforces retirement. Team admin is revalidated transactionally. Secret returned once with no-store. Expiry is optional: omitted/sole null Never, or exclusive expires_in/absolute expires_at. No silent90-day default or hardcoded one-year ceiling. Active prospective policy and owner validity are checked before replacing either credential; a pending prepared/armed scheduled generation answers409 schedule_pending.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `X-CSRF-Token` | header | string, required | Token returned by admin login/session; required with the same-origin session cookie for writes. |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)
  - `overlap_seconds` (integer, required, min 0, max 86400)
  - `expires_at` ([ManagedKeyExpiryDate](/docs/api/schemas/m/#managedkeyexpirydate) or null) — Optional absolute RFC3339 date with explicit timezone. Missing or sole null means Never. Future and optional prospective maximum checked under the database clock. Cannot coexist with expires_in, even when null.
    - Any of (1): [ManagedKeyExpiryDate](/docs/api/schemas/m/#managedkeyexpirydate)
    - Any of (2): null
  - `expires_in` ([ManagedKeyDuration](/docs/api/schemas/m/#managedkeyduration) or null) — Optional positive integer followed by s, m, h, d or w, at most 9223372036 seconds. Missing or sole null means Never. Database-clock issuance anchor; scheduled rotations instead anchor at actual activation. Cannot coexist with expires_at, even when null.
    - Any of (1): [ManagedKeyDuration](/docs/api/schemas/m/#managedkeyduration)
    - Any of (2): null

**Responses**

| Status | Description | Body |
|---|---|---|
| `201` | Successful operation; metadata is private and not cached. | `application/json`: [KeyRotationResult](/docs/api/schemas/k/#keyrotationresult) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | Version changed or lifecycle conflict | — |
| `422` | Current prospective key policy rejects issuance. | — |
| `503` | Storage or configuration unavailable | — |


## GET /api/v1/admin/keys/{id}/credentials

`GET /api/v1/admin/keys/{id}/credentials` · operation `managedKeyCredentialHistory`

Requires key_rotation and keys:read. Scope is applied before pagination; foreign keys return 404. Hashes and secrets are never returned. Version is credential generation, not control version.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `limit` | query | integer, default `50`, min 1, max 100 |  |
| `before_version` | query | integer, min 0 | Only credential generations older than this one, to page back through the history. |

**Responses**

| Status | Description | Body |
|---|---|---|
| `200` | Successful operation; metadata is private and not cached. | `application/json`: [KeyCredentialHistory](/docs/api/schemas/k/#keycredentialhistory) |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | Version changed or lifecycle conflict | — |
| `503` | Storage or configuration unavailable | — |


## POST /api/v1/admin/keys/{id}/credentials/{credential_id}/retire

`POST /api/v1/admin/keys/{id}/credentials/{credential_id}/retire` · operation `retireManagedKeyCredential`

Requires key_rotation, keys:manage and CSRF. Retires an old credential immediately; retiring current credential requires revoking the stable key. Repeated retirement is idempotent; unrelated generations conflict.

**Authentication:** `adminSession`

**Parameters**

| Name | In | Type | Description |
|---|---|---|---|
| `id` | path | string, required |  |
| `credential_id` | path | string, required |  |

**Request body** (required)

- `application/json`: object
  - `version` (integer (int64), required, min 0)

**Responses**

| Status | Description | Body |
|---|---|---|
| `204` | Old credential retired or already retired | — |
| `400` | Invalid input | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `401` | Missing or invalid bearer key | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `403` | Model not allowed | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `404` | Feature disabled | `application/json`: [Error](/docs/api/schemas/e/#error) |
| `409` | Version changed or lifecycle conflict | — |
| `503` | Storage or configuration unavailable | — |

