# Key rotation

**Last updated:** 2026-11-21T09:54:10-03:00 (UTC-3)

Change a virtual key's secret without taking applications down. The gateway issues a new secret, and the old one keeps working during an overlap window you choose, until the applications migrate. The key stays the same: ID, team, models, scopes, RPM and usage history do not change. The replacement may have no expiry; Never is the default unless you explicitly choose a duration or date. Flag: `key_rotation`, which requires `identity_management`.

For operator encryption/session keys, use the separate [keyring maintenance guide](/docs/security/keyring-rotation/). The workflows below rotate an inference client credential.

## How to use it

**In the console:** **Virtual keys → Credential rotation** shows the key's secret history, generates a new one with overlap and retires the old one before its deadline (**Retire now**).

**Through the API:**

| Endpoint | What it does |
| --- | --- |
| `GET /api/v1/admin/keys/{id}/credentials` | History and the current `version` |
| `POST /api/v1/admin/keys/{id}/rotate` | Generates the new secret |
| `POST /api/v1/admin/keys/{id}/credentials/{credential_id}/retire` | Retires an old secret now |

`rotate` takes:

| Field | Values |
| --- | --- |
| `version` | The version read from the history |
| `overlap_seconds` | 0 to 86,400 (up to 24 h) of coexistence between the old and the new |
| `expires_at` | Optional absolute UTC deadline; omit/null for Never |
| `expires_in` | Optional positive integer duration with s/m/h/d/w; exclusive with expires_at, resolved from database time |

The `201` response carries the new secret **only once**, with `Cache-Control: no-store`. Distribute it to the applications during the overlap.

## How it works

- At the end of the overlap, the old secret stops authenticating on every replica. By default, the database checks the deadlines on each authentication, without a scheduled job. The optional [managed credential cache](/docs/features/managed-key-cache/) preserves the credential expiry/retirement deadline captured by that same SQL lookup; it never extends the overlap. Local successful rotation/revocation invalidates the serving replica, while earlier changes on another replica have the explicitly configured bounded cache visibility window.
- The overlap never extends the old secret: it goes away at whichever comes first, the end of the overlap or its original expiration.
- **Up to two secrets** are valid at the same time. A new rotation while an old one is still active returns `409 rotation_pending`: retire the old one or wait.
- Two simultaneous rotations of the same version: one succeeds, the other gets `409`.
- Only active, unexpired keys can be rotated.
- A managed key's [source CIDR ceiling](/docs/security/source-ip-controls/) and independent source-policy version remain on the parent key; credential rotation does not clear or broaden them.
- **Retire now** retires an old secret, idempotently. To invalidate the current one, revoke the key.
- Revoking the key or deleting the team invalidates every secret. Requests already authenticated may finish.
- Only the SHA-256 hashes of the secrets are stored.

The history shows, with no secret or hash: ID, generation, predecessor (`replaces_id`), creation, expiration, scheduled or completed retirement and the state (`active`, `retiring`, `retired`, `expired`, `key_revoked`). Each generation keeps the issuance `request_id`, which links to the `key.rotate` event in the audit trail. Pagination by `limit` (1 to 100) and `next_before_version`.

## Who can do it

- Reading with `keys:read` (viewer reads the metadata); rotation with `keys:manage`, a same-origin session and CSRF. Operator has no access to keys.
- Team roles see only their own teams' keys; IDs from other teams return 404.
- A `team_admin` does not rotate keys with `*`, `route:*` or `mcp:read`, and the key must stay within the team policy: rotation does not broaden access.

## Limits

- YAML keys are not rotated from the console: change the hash in the Secret and roll out. Do not put a managed key's secret in the YAML.
- Scheduled activation is the explicit prepared/confirmed profile below. There is no blind recurring key generation or secret delivery to Vault/KMS, email or generic webhooks. Expiry notices are metadata-only.
- Analytics keeps using the key's stable ID, not the ID of each secret.
- Turning off the flag hides management and history, but the deadlines and revocations still apply.
- Going back to a gateway version without rotation ends the overlap: old secrets may stop before their deadline. Do not restore an old backup to recover a secret, because that can reactivate revoked keys.

## Scheduled activation

Prepare the next credential in advance, distribute its secret, then explicitly confirm possession before it can activate at the chosen time. This is a one-shot schedule. It never generates an undistributable secret in a background job.

1. Read `GET /api/v1/admin/keys/{id}/rotation-schedule` and credential history. Keep the schedule `version` and active `credential_version`.
2. `POST` that endpoint with both versions, future `activate_at`, explicit `overlap_seconds` and an optional expiry choice. The201/no-store response shows the new secret and an independent confirmation nonce once. Save them securely. The staged secret does not authenticate and the old credential is unchanged.
3. After distributing the secret, `POST /api/v1/admin/keys/{id}/rotation-schedule/confirm` with the current schedule `version` and `confirmation_nonce`. Live permissions, plan/license, policy and nonce are checked again. Unconfirmed preparation cannot retire the current credential.
4. At or after the chosen time, the worker atomically activates the known hash using PostgreSQL time and rechecks current actor/team/parent/policy/entitlement state. The overlap starts at actual activation, so a delayed worker does not consume it before the new secret works.
5. `DELETE /rotation-schedule` with the current version cancels a pending schedule; it never retires the current credential or reactivates old material.

The console places these actions in the existing **Credential rotation** panel. Secret/nonce state is volatile: abandonment, logout and authority changes clear it. No read endpoint retrieves either value. A lost prepare response leaves an unarmed schedule: inspect its metadata, cancel it and prepare again. Do not confirm a schedule whose secret was not retained.

A scheduled `expires_in` means validity **after actual activation**. A fixed absolute deadline never moves; if it has passed when the worker executes, activation is blocked and the old credential is not retired. Never remains Never. The selected prospective [key policy](/docs/features/key-policy/) is revalidated; it never injects an expiry or silently clamps the user's choice.

Named-user demotion/disable, team deletion, parent revocation, stale parent versions or unavailable grants block activation. A schedule prepared with the shared/bootstrap administrator is durable operator intent: rotating/disabling that interactive console token does not cancel it. Use explicit cancellation or operator feature controls; named-user schedules retain their live identity check. Transient database/configuration-refresh faults leave an armed intent eligible for retry. An explicit authorization/policy/grant failure records a blocked intent: correct the cause, cancel it and prepare/confirm a fresh schedule. Restoring a permission alone never silently resumes a business-blocked rotation weeks later. A finite old deadline still applies if chosen earlier; preparation rejects a desired activation at or after that known deadline. Monitor blocked schedules rather than assuming a desired date proves execution.

Preparation, confirmation and execution under `scheduled_key_rotation` require `key_rotation`, a live scoped rotation permission and applicable team/organization plan rights. Historical scoped reads and cancellation of a pending schedule remain available under the existing live key authority when new processing/grants are off or unavailable; cancelling never changes the current credential. A flag or coarse console capability grants no selected team's access. Disabling processing leaves the current credential intact. The worker performs no secret delivery; existing [expiry notices](/docs/features/key-policy/#expiry-notifications) contain identifiers/deadlines only.

Prepared material is excluded from every authentication path and the optional managed credential cache. Only the hash and confirmation hash are stored. Credential generations remain unique across cancellation; the active parent version changes only on actual activation. Concurrent prepare/confirm/cancel/activation uses its own schedule CAS and the existing user→control→team→key lock order.

## Admin API routes

Every route below needs a console session or an admin Bearer token, the role's permission and, for writes, the `X-CSRF-Token` header; the full contract is in the [API reference](/docs/api/).

| Route | What it does |
| --- | --- |
| `POST /api/v1/admin/keys/{id}/rotation-schedule` | Prepares the next credential now and returns its secret and confirmation nonce once |
| `DELETE /api/v1/admin/keys/{id}/rotation-schedule` | Cancels a pending schedule; the working credential stays |

## See also

- [Key expiry, tags and policy](/docs/features/key-policy/).
- [Access control](/docs/features/access-control/).
- [Key owners](/docs/features/identity-ownership/).
