# Rotate an API key (/docs/api/access/keys_rotate)

Mint a replacement key with the same scopes and limits; the old key keeps working for grace_seconds (default 24 h), then expires.

`POST /v1/keys/{id}/rotate`

```json
{
  "operationId": "keys_rotate",
  "summary": "Rotate an API key",
  "description": "Mint a replacement key with the same scopes and limits; the old key keeps working for grace_seconds (default 24 h), then expires.",
  "tags": [
    "keys"
  ],
  "security": [
    {
      "bearer": []
    }
  ],
  "parameters": [
    {
      "schema": {
        "type": "string",
        "pattern": "^[0-9A-Za-z]{12}$"
      },
      "required": true,
      "name": "id",
      "in": "path"
    }
  ],
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "grace_seconds": {
              "type": "integer",
              "minimum": 0,
              "maximum": 604800,
              "default": 86400,
              "description": "How long the old key keeps working after rotation"
            }
          }
        }
      }
    }
  },
  "responses": {
    "200": {
      "description": "OK",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "key": {
                "type": "object",
                "properties": {
                  "object": {
                    "type": "string",
                    "enum": [
                      "api_key"
                    ]
                  },
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "prefix": {
                    "type": "string"
                  },
                  "scopes": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "*",
                        "call",
                        "run",
                        "jobs",
                        "lists",
                        "catalog:read",
                        "balance:read",
                        "logs:read",
                        "billing:write",
                        "keys:write",
                        "agents:write",
                        "credentials:write",
                        "presets:write",
                        "pipelines:read",
                        "pipelines:write",
                        "pipelines:run",
                        "cache:write"
                      ]
                    }
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "active",
                      "revoked"
                    ]
                  },
                  "label": {
                    "type": "string"
                  },
                  "agent_id": {
                    "type": "string"
                  },
                  "created_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "last_used_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "expires_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "allowed_ips": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "limit_micro": {
                    "type": [
                      "integer",
                      "null"
                    ],
                    "minimum": 0
                  },
                  "limit_reset": {
                    "type": "string",
                    "enum": [
                      "day",
                      "week",
                      "month",
                      "never"
                    ]
                  },
                  "usage_micro": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "limit_remaining_micro": {
                    "type": [
                      "integer",
                      "null"
                    ],
                    "minimum": 0
                  },
                  "secret": {
                    "type": "string"
                  }
                },
                "required": [
                  "object",
                  "id",
                  "name",
                  "prefix",
                  "scopes",
                  "status",
                  "created_at"
                ]
              },
              "previous": {
                "type": "object",
                "properties": {
                  "object": {
                    "type": "string",
                    "enum": [
                      "api_key"
                    ]
                  },
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "prefix": {
                    "type": "string"
                  },
                  "scopes": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "enum": [
                        "*",
                        "call",
                        "run",
                        "jobs",
                        "lists",
                        "catalog:read",
                        "balance:read",
                        "logs:read",
                        "billing:write",
                        "keys:write",
                        "agents:write",
                        "credentials:write",
                        "presets:write",
                        "pipelines:read",
                        "pipelines:write",
                        "pipelines:run",
                        "cache:write"
                      ]
                    }
                  },
                  "status": {
                    "type": "string",
                    "enum": [
                      "active",
                      "revoked"
                    ]
                  },
                  "label": {
                    "type": "string"
                  },
                  "agent_id": {
                    "type": "string"
                  },
                  "created_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "last_used_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "expires_at": {
                    "type": "string",
                    "format": "date-time"
                  },
                  "allowed_ips": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  },
                  "limit_micro": {
                    "type": [
                      "integer",
                      "null"
                    ],
                    "minimum": 0
                  },
                  "limit_reset": {
                    "type": "string",
                    "enum": [
                      "day",
                      "week",
                      "month",
                      "never"
                    ]
                  },
                  "usage_micro": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "limit_remaining_micro": {
                    "type": [
                      "integer",
                      "null"
                    ],
                    "minimum": 0
                  },
                  "secret": {
                    "type": "string"
                  }
                },
                "required": [
                  "object",
                  "id",
                  "name",
                  "prefix",
                  "scopes",
                  "status",
                  "created_at"
                ]
              }
            },
            "required": [
              "key",
              "previous"
            ]
          }
        }
      }
    },
    "default": {
      "description": "Error",
      "content": {
        "application/json": {
          "schema": {
            "type": "object",
            "properties": {
              "error": {
                "type": "object",
                "properties": {
                  "code": {
                    "type": "string",
                    "enum": [
                      "invalid_request",
                      "unauthorized",
                      "insufficient_balance",
                      "max_cost_exceeded",
                      "scope_denied",
                      "budget_blocked",
                      "not_found",
                      "idempotency_mismatch",
                      "invalid_meta",
                      "rate_limited",
                      "upstream_error",
                      "response_buffer_limit",
                      "provider_capacity_unavailable",
                      "grid_saturated",
                      "upstream_timeout",
                      "conflict",
                      "internal_error",
                      "account_frozen",
                      "strict_filters",
                      "plan_limit_reached",
                      "validation_failed",
                      "deadline_exceeded",
                      "cancelled"
                    ]
                  },
                  "message": {
                    "type": "string"
                  },
                  "request_id": {
                    "type": "string"
                  },
                  "call_id": {
                    "type": "string"
                  },
                  "retry_after_ms": {
                    "type": "integer",
                    "minimum": 0
                  },
                  "details": {
                    "type": "object",
                    "additionalProperties": {}
                  }
                },
                "required": [
                  "code",
                  "message"
                ]
              }
            },
            "required": [
              "error"
            ]
          }
        }
      }
    }
  }
}
```
