# Roll an API key

`POST /api/v1/api-keys/{id}/roll`

Mints a replacement with the same name, type, scopes and mode, and sets the old key to expire after the overlap (24 hours unless `overlap_hours` shortens it) so a deploy can swap them without a 401. End the overlap early with `DELETE /api/v1/api-keys/{old_key_id}?reason=rolled`. Signed-in session only: an API key cannot manage keys. Like create, this route does NOT honor Idempotency-Key, because the response holds a secret.

No API key can call this route: it needs a signed-in dashboard user with the right role. A key answers 403.

## Path parameters

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | required |  |

## Headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string | optional | A unique value for this operation, chosen by you. A retry with the same key and the same body returns the first response instead of running again. The same key with a different body answers 409 `idempotency_key_reused`; a key whose first request is still running answers 409 `idempotency_in_progress`. |

## Request body

JSON Schema:

```json
{
  "type": "object",
  "properties": {
    "overlap_hours": {
      "type": "integer",
      "minimum": 0,
      "maximum": 24,
      "description": "Hours the old key keeps working. 0 stops it at once. Default 24."
    }
  }
}
```

## Responses

### 201

Key rolled. Store `secret` now; it is unrecoverable.

JSON Schema:

```json
{
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "format": "uuid",
          "example": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
          "description": "The NEW key."
        },
        "name": {
          "type": "string"
        },
        "key_type": {
          "type": "string",
          "enum": [
            "developer",
            "agent",
            "publishable"
          ]
        },
        "mode": {
          "type": "string",
          "enum": [
            "test",
            "live"
          ]
        },
        "key_prefix": {
          "type": "string",
          "enum": [
            "gpy_",
            "agk_",
            "pk_"
          ]
        },
        "key_hint": {
          "type": "string"
        },
        "scopes": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "expires_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time",
          "description": "90 days from now, or null when the key being rolled never expired."
        },
        "old_key_id": {
          "type": "string",
          "format": "uuid",
          "example": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
        },
        "old_key_hint": {
          "type": "string"
        },
        "old_key_expires_at": {
          "type": [
            "string",
            "null"
          ],
          "format": "date-time",
          "description": "When the old key stops working. Never later than it would have expired anyway."
        },
        "secret": {
          "type": "string",
          "description": "The new plaintext key, returned by THIS response only.",
          "example": "gpy_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        }
      },
      "required": [
        "id",
        "name",
        "key_type",
        "mode",
        "key_prefix",
        "key_hint",
        "scopes",
        "expires_at",
        "old_key_id",
        "old_key_hint",
        "old_key_expires_at",
        "secret"
      ]
    },
    "_agent": {
      "type": "object",
      "properties": {
        "summary": {
          "type": "string"
        },
        "status": {
          "type": "string",
          "enum": [
            "ok"
          ]
        },
        "next_actions": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "warnings": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "summary",
        "status",
        "next_actions",
        "warnings"
      ]
    },
    "api_version": {
      "type": "string",
      "example": "2026-07-01"
    }
  },
  "required": [
    "data",
    "_agent",
    "api_version"
  ]
}
```

### 404

No such key in this workspace and mode.

### 409

The key was already rolled; roll its replacement (`replacedBy`) instead.

### 422

The key is revoked or expired; create a new one instead.
