> ## Documentation Index
> Fetch the complete documentation index at: https://docs.barndoor.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Rotate an API key

> Replace the key's secret in place. The key id, name, bindings and usage history are kept. With `grace_period_secs` the previous secret keeps working for that long (at most 7 days); otherwise it stops working immediately. The new secret is returned once and never stored.



## OpenAPI

````yaml /api-reference/openapi.yaml post /api/llm-gateway/admin/api-keys/{id}/rotate
openapi: 3.1.0
info:
  title: Barndoor.ai Public API
  description: Unified public API specification for Barndoor.ai platform
  version: 2.18.0
servers:
  - url: https://{host}
    description: Barndoor public API
    variables:
      host:
        default: platform.barndoor.ai
        description: >-
          The host serving your Barndoor deployment. Defaults to production;
          override it for another environment or a dedicated deployment.
security:
  - ApiKey: []
  - OAuth2:
      - openid
      - profile
      - email
tags:
  - name: dlp
    x-group: Data Control Center
  - name: identity
    x-group: Identity
  - name: llm-gateway
    x-group: LLM Gateway
  - name: notification
    x-group: Notifications
  - name: policy
    x-group: Policies
  - name: registry
    x-group: MCP Gateway
  - name: system-management
    x-group: Log Exports
paths:
  /api/llm-gateway/admin/api-keys/{id}/rotate:
    post:
      tags:
        - llm-gateway
      summary: Rotate an API key
      description: >-
        Replace the key's secret in place. The key id, name, bindings and usage
        history are kept. With `grace_period_secs` the previous secret keeps
        working for that long (at most 7 days); otherwise it stops working
        immediately. The new secret is returned once and never stored.
      operationId: rotate_api_key
      parameters:
        - name: id
          in: path
          description: Identifier, a UUID
          required: true
          schema:
            type: string
            format: uuid
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RotateApiKeyRequest'
        required: true
      responses:
        '200':
          description: The rotated key, including its new secret
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RotateApiKeyResponse'
        '400':
          description: Malformed request
        '401':
          description: Missing or invalid credentials
        '403':
          description: Caller is not permitted to act on this resource in this organization
        '404':
          description: No such resource in this organization
components:
  schemas:
    RotateApiKeyRequest:
      type: object
      description: >-
        Body of `POST /admin/api-keys/{id}/rotate` and `POST
        /user/api-keys/{id}/rotate`

        (BCP-5195). An empty object rotates immediately and keeps the key's
        expiry.
      properties:
        expires_at:
          type:
            - string
            - 'null'
          format: date-time
          description: |-
            New expiry for the key. Omit to keep the current one; `null` removes
            the expiry. Must be in the future.
        grace_period_secs:
          type: integer
          format: int32
          description: |-
            How long the previous secret keeps authenticating after rotation, in
            seconds. `0` (the default) cuts it off immediately. At most 7 days.
          minimum: 0
    RotateApiKeyResponse:
      type: object
      required:
        - id
        - name
        - key_prefix
        - key
      properties:
        expires_at:
          type:
            - string
            - 'null'
          format: date-time
        id:
          type: string
          format: uuid
          description: Unchanged by rotation.
        key:
          type: string
          description: The new API key — only returned by this call, never stored.
        key_prefix:
          type: string
        name:
          type: string
        previous_key_expires_at:
          type:
            - string
            - 'null'
          format: date-time
          description: >-
            Until when the previous secret still authenticates; `None` when it
            was

            cut off immediately.
        rotated_at:
          type:
            - string
            - 'null'
          format: date-time
  securitySchemes:
    ApiKey:
      type: http
      scheme: bearer
      description: >-
        A Barndoor API key, sent as `Authorization: Bearer <key>`. Keys are
        prefixed `bdai_` and are opaque — do not attempt to decode one.
        Interchangeable with an OAuth 2.0 access token.
    OAuth2:
      type: oauth2
      x-issuer: https://auth.barndoor.ai/realms/barndoor
      description: >-
        An OAuth 2.0 access token from Barndoor's identity provider, sent as
        `Authorization: Bearer <token>`. Public clients must use PKCE.
        Interchangeable with an API key.
      flows:
        authorizationCode:
          authorizationUrl: >-
            https://auth.barndoor.ai/realms/barndoor/protocol/openid-connect/auth
          tokenUrl: >-
            https://auth.barndoor.ai/realms/barndoor/protocol/openid-connect/token
          scopes:
            openid: Issue an OIDC ID token identifying the caller.
            profile: Read the caller's basic profile claims.
            email: Read the caller's email address.
            offline_access: Issue a refresh token. Optional — no operation requires it.
        clientCredentials:
          tokenUrl: >-
            https://auth.barndoor.ai/realms/barndoor/protocol/openid-connect/token
          scopes:
            openid: Issue an OIDC ID token identifying the caller.
            profile: Read the caller's basic profile claims.
            email: Read the caller's email address.
            offline_access: Issue a refresh token. Optional — no operation requires it.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.