# Rotate a service key

> POST /v1/services/{service_id}/keys/{key_id}/rotate. Creates a new key and makes the old one expire after overlap_hours (default 24, 0 to 168), so callers can switch without downtime.

Source: https://legion.ambient.xyz/docs/api/services/rotate-service-key

`POST https://api.legion.ambient.xyz/v1/services/{service_id}/keys/{key_id}/rotate`

Authenticate with an API key: `Authorization: Bearer $LEGION_API_KEY`.

Creates a new key and makes the old one expire after `overlap_hours` (default 24, 0 to 168), so callers
can switch without downtime. The new secret is shown once (`Cache-Control: no-store`). On a session it
needs a fresh sign-in (403 `step_up_required`). Needs `services.manage`.

## Path parameters

- `service_id`, string (uuid), required
- `key_id`, string (uuid), required

## Request body

- `name`, string: The new key's name; default the old one's.
- `overlap_hours`, integer: How long the old key keeps working. Default 24.
- `expires_in_days`, integer: The new key's expiry, as on create.
- `no_expiry`, boolean: The new key never expires, as on create.

Example:

```json
{
  "name": "string",
  "overlap_hours": 0,
  "expires_in_days": 7,
  "no_expiry": false
}
```

## Responses

### 201

The new key with its secret, and the old key with its new expiry.

- `new_key`, ServiceKeyCreated, required
  - `key`, ServiceKey, required
    - `id`, string (uuid), required
    - `kind`, ServiceKeyKind, required: `service`, an `lek_` key. `legacy_direct`, the `lsk_` key of a public model rental from before services; revoking it switches the service's access to keys.
    - `name`, string, required
    - `prefix`, string, required: The first 12 characters, `lek_` and 8 more (`lsk_` for a legacy direct key). Display only.
    - `status`, ServiceKeyStatus, required
    - `created_at`, string (date-time), required
    - `created_by`, string (uuid), required
    - `expires_at`, string (date-time), required
    - `last_used_at`, string (date-time), required: Updated at most once a minute.
    - `revoked_at`, string (date-time), required
    - `rate_limit_per_minute`, integer, required
    - `direct_url`, string, required: The direct address a legacy direct key opens.
  - `secret`, string, required: The whole key, `lek_` and 43 URL-safe characters. Shown once.
- `old_key`, ServiceKey, required
  - `id`, string (uuid), required
  - `kind`, ServiceKeyKind, required: `service`, an `lek_` key. `legacy_direct`, the `lsk_` key of a public model rental from before services; revoking it switches the service's access to keys.
  - `name`, string, required
  - `prefix`, string, required: The first 12 characters, `lek_` and 8 more (`lsk_` for a legacy direct key). Display only.
  - `status`, ServiceKeyStatus, required
  - `created_at`, string (date-time), required
  - `created_by`, string (uuid), required
  - `expires_at`, string (date-time), required
  - `last_used_at`, string (date-time), required: Updated at most once a minute.
  - `revoked_at`, string (date-time), required
  - `rate_limit_per_minute`, integer, required
  - `direct_url`, string, required: The direct address a legacy direct key opens.

Example:

```json
{
  "new_key": {
    "key": {
      "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "kind": "service",
      "name": "string",
      "prefix": "string",
      "status": "active",
      "created_at": "2026-10-15T12:00:00Z",
      "created_by": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
      "expires_at": "2026-10-15T12:00:00Z",
      "last_used_at": "2026-10-15T12:00:00Z",
      "revoked_at": "2026-10-15T12:00:00Z",
      "rate_limit_per_minute": 0,
      "direct_url": "string"
    },
    "secret": "string"
  },
  "old_key": {
    "id": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "kind": "service",
    "name": "string",
    "prefix": "string",
    "status": "active",
    "created_at": "2026-10-15T12:00:00Z",
    "created_by": "0192f1a2-7c3d-7e4f-8a9b-0c1d2e3f4a5b",
    "expires_at": "2026-10-15T12:00:00Z",
    "last_used_at": "2026-10-15T12:00:00Z",
    "revoked_at": "2026-10-15T12:00:00Z",
    "rate_limit_per_minute": 0,
    "direct_url": "string"
  }
}
```

### 400

The request is malformed or fails validation. An image reference that
is not a container image reference answers `invalid_image_reference`.

### 401

Missing, unknown, expired or revoked credentials.

### 403

The caller lacks the permission this operation requires (`forbidden`),
or signed in without having joined an organization yet
(`no_organization`; `getOnboarding` and the invitation and
organization operations under it work), or could not sign in
(`email_unverified`, `account_conflict`, and `domain_not_allowed`). `not_invited` is no longer returned to
session tokens.

`access_not_approved`: a signed-in person who is not approved
for the beta yet. Every operation that takes a credential answers it to
them, except `getMe` and `joinMyWaitlist`; the problem carries
`access` (`waitlisted` or `not_requested`, and the email). Approved
means a member of an organization, holding a pending invitation, or Legion; an API key always belongs to a member.

`oauth_token_mcp_only`: the caller sent an OAuth access token (`lgo_`), which is accepted
only by `mcpMessage` and the tool calls it dispatches. Every other operation answers it, whatever
the token's scope. Use an API key or the Legion web app.

`org_suspended`: the caller's organization is suspended. Every operation that takes a
credential answers it, by session or API key, except `getMe` (which shows `org.suspended_at` and
`org.suspension_reason`) and the support operations under `/v1/support/`.

`terms_not_accepted`: an approved caller who has not accepted
the current version of the Terms of Use and the Acceptable Use Policy. Every operation that takes a credential answers it, except `getMe`,
`acceptLegalDocuments`, `joinMyWaitlist` and the wind-down operations
(`listRentals`, `stopRental`, `deleteRental`, `listHosts`,
`updateListing` with `listed` false, `removeHost`); the problem
carries `legal`. An API key acts for the person who created it, so it
is refused until that person accepts. Terminal
operations also return
`session_required` (called with an API key) and `step_up_required`
(neither a sign-in nor a step-up challenge within the step-up window).

### 404

The resource does not exist or belongs to another organization.

### 409

The resource's current state does not allow the operation. `legal_versions_stale`: a legal document version sent is not the current one; the
problem's `legal` holds the current documents.

### 429

Rate limited (`code` is `rate_limited`), or the organization asked the
pull-through mirror to copy in too many different tags or digests of one
repository today (`mirror_quota_exceeded`). Retry after the `Retry-After`
seconds.

### 500

An unexpected server error. Safe to retry.

## Errors

- 403 [`no_organization`](https://legion.ambient.xyz/docs/errors#no_organization)
- 403 [`step_up_required`](https://legion.ambient.xyz/docs/errors#step_up_required)
- 403 [`web_app_required`](https://legion.ambient.xyz/docs/errors#web_app_required)
- 409 [`service_deleting`](https://legion.ambient.xyz/docs/errors#service_deleting)
- 409 [`too_many_keys`](https://legion.ambient.xyz/docs/errors#too_many_keys)
- 409 [`key_revoked`](https://legion.ambient.xyz/docs/errors#key_revoked)
- 429 [`rate_limited`](https://legion.ambient.xyz/docs/errors#rate_limited)

## Example request

```sh
curl -s -X POST "https://api.legion.ambient.xyz/v1/services/$SERVICE_ID/keys/$KEY_ID/rotate" \
  -H "Authorization: Bearer $LEGION_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "string",
  "overlap_hours": 0,
  "expires_in_days": 7,
  "no_expiry": false
}'
```
