Skip to main content
Availability: API key rotation is available on all plans (Free, Growth, and Enterprise) on Portkey Cloud.For self-hosted deployments:
  • Airgapped: Backend v1.14.0+
  • Hybrid: Gateway Enterprise v2.5.0+
Most older versions are compatible, but there are known edge cases around cache invalidation during the transition window. We recommend upgrading to the versions above for full support.

Overview

API Key Rotation enables you to replace API key secrets without causing service disruption. When a key is rotated:
  • A new secret is generated immediately.
  • The previous secret remains valid for a configurable transition period, allowing time to update existing integrations.
  • After the transition period ends, the old secret is automatically revoked.
You can initiate rotation:
  • Manually, on demand
  • Automatically, using a defined schedule
Key Characteristics
  • The API key ID remains unchanged during rotation.
  • Only the secret value is updated.
  • Existing usage data, such as budget tracking, cost attribution, and analytics - continues to be associated with the same key.

Key Concepts

How Rotation Works

  1. A new API key secret is generated.
  2. The current secret is retained as a previous version, with a key_transition_expires_at timestamp.
  3. The API key is updated with the new secret, and last_rotated_at is recorded.
  4. The new secret becomes immediately valid for authentication.
  5. If a rotation_period is configured, next_rotation_at is recalculated for the next cycle.
During the transition window
  • Both the previous and current secrets are accepted.
  • Requests authenticated with either secret resolve to the same API key.

Transition Window Lifecycle

Rotation Modes

Manual Rotation

Triggered via API call. The caller receives the new key in the response. Endpoint: POST /v2/api-keys/:apiKeyId/rotate Request body (optional):
  • key_transition_period_ms — overrides the policy/default transition period for this rotation. Minimum: 1800000 (30 min).
Response:
Constraints:
  • Cannot rotate if a previous version is still in transition. Wait for the transition to expire first.
  • If a rotation policy with rotation_period exists, the transition period must be shorter than the rotation period.
Required scope: *.rotate (e.g., organisation-service-api-keys.rotate, workspace-user-api-keys.rotate)

Automatic Rotation

Automatic rotation is handled by a background worker that runs on a recurring schedule. On each run, the worker executes the following phases in order:

Rotation Policy Configuration

A rotation policy can be attached to any API key at creation or via update.
  • key_transition_period_ms must be strictly less than the rotation period duration.
  • If both rotation_period and next_rotation_at are provided, next_rotation_at takes precedence for the upcoming rotation.
  • Subsequent rotations follow the rotation_period.
Setting a policy on create:
Updating a policy:
Removing a policy: Set rotation_policy to null in the update request.

Organisation-Enforced Rotation Period

Organisation owners/admins can enforce a rotation cadence on workspace-user API keys via the user_api_key_rotation_period setting (with optional per-workspace override). The setting accepts an integer between 1 and 365 (days). (Backend v1.17.0+ stores and returns days only; weekly/monthly input is normalized to 7/30.) When set, newly-created workspace-user keys without a rotation policy automatically get the enforced cadence, and any explicit rotation_period / rotation_period_days that differs from the enforced value is rejected at create/update time. next_rotation_at cannot be manually changed on API key update while enforcement is active.

Email Notifications

These notifications are triggered during automatic rotation runs.: Recipients: Organisation admins and owners, the key’s owning user (for workspace-user keys), and any custom addresses in alert_emails.

Reading Rotation State

GET /v2/api-keys/:apiKeyId returns the rotation policy alongside the key details:

Permissions & Authorization

Admins (org admin/owner, workspace admin/manager) can rotate any key within their scope. Non-admins can only rotate their own keys.

Constraints & Limits

  • A maximum of two active secrets can exist per API key at any time:
    • The current secret
    • One previous secret within its transition window
  • Rotation is blocked while a transition is active (i.e., until the previous secret expires)
  • key_transition_period_ms:
    • Minimum: 30 minutes (1800000 ms)
    • Must be strictly less than the configured rotation period
  • Supported rotation_period values:
    • weekly
    • monthly
  • rotation_period_days (Backend v1.16.0+) accepts an integer between 1 and 365. Mutually exclusive with rotation_period.
  • next_rotation_at is always normalized to UTC midnight for consistency across schedules

Audit Logging

Every rotation (manual and automatic) produces an audit log entry containing:

Notes

  • Automatic rotation phases are executed sequentially within a single run.
  • A single run may process multiple keys across different phases.
  • Notifications are advisory and do not affect key validity.

Rotate API Key

Create API Key

Update API Key

Retrieve API Key

API Keys (AuthN & AuthZ)

User Roles & Permissions

Last modified on May 26, 2026