Skip to main content

Rotate API Key

Rotates an existing API key by generating a new key value while preserving all configuration settings (name, scopes, IP whitelist, etc.). The old key is invalidated and a new key is issued. This is the recommended approach for periodic key rotation as part of security best practices.


Endpoint

Method: POST URL: {{base_url}}/api/v1/api-keys/{keyId}/rotate


Authentication

This endpoint uses Bearer Token authentication via a JWT (JSON Web Token). The token must be included in the Authorization header. Requires admin-level permissions.


Headers

HeaderDescriptionRequired
AuthorizationBearer token (JWT) for authenticationYes
acceptSpecifies acceptable response formats (application/json)Yes
x-tenantidUUID identifying the tenant/organizationYes
realmnameThe authentication realm nameNo

Path Parameters

ParameterTypeDescriptionRequired
keyIdstring (UUID)The unique identifier of the API key to rotateYes

Query Parameters

None.


Request Body Schema

Not applicable — this endpoint typically requires no request body. Optional parameters may be supported:

{
"gracePeriodMinutes": 60,
"expiresIn": 365
}
FieldTypeDescriptionRequired
gracePeriodMinutesintegerMinutes during which both old and new keys workNo
expiresInintegerNew expiration time in days (resets expiration)No

Example cURL

curl --request POST \
--url '{{base_url}}/api/v1/api-keys/{{key_id}}/rotate' \
--header 'accept: application/json' \
--header 'authorization: Bearer {{access_token}}' \
--header 'x-tenantid: {{tenant_id}}'

With Grace Period:

curl --request POST \
--url '{{base_url}}/api/v1/api-keys/{{key_id}}/rotate' \
--header 'accept: application/json' \
--header 'authorization: Bearer {{access_token}}' \
--header 'content-type: application/json' \
--header 'x-tenantid: {{tenant_id}}' \
--data '{
"gracePeriodMinutes": 60
}'

Success Response Example

Status Code: 200 OK

{
"success": true,
"message": "API key rotated successfully",
"data": {
"id": "{{key_id}}",
"name": "Production Integration Key",
"description": "API key for ServiceNow integration",
"newApiKey": "{{new_generated_api_key}}",
"newPrefix": "ak_live_xxxx",
"previousPrefix": "ak_live_yyyy",
"scopes": [
"ticketing:read",
"ticketing:write",
"users:read"
],
"protocol": "rest",
"ipWhitelist": [
"{{allowed_ip_range_1}}",
"{{allowed_ip_range_2}}"
],
"environment": "live",
"status": "active",
"expiresAt": "2026-07-10T13:10:00.000Z",
"rotation": {
"rotatedAt": "2025-07-10T13:10:00.000Z",
"rotatedBy": {
"id": "{{user_id}}",
"name": "{{user_name}}",
"email": "{{user_email}}"
},
"rotationCount": 3,
"previousKeyInvalidatedAt": "2025-07-10T13:10:00.000Z",
"gracePeriod": null
}
},
"timestamp": "2025-07-10T13:10:00.000Z"
}

Success Response with Grace Period

Status Code: 200 OK

{
"success": true,
"message": "API key rotated successfully with grace period",
"data": {
"id": "{{key_id}}",
"name": "Production Integration Key",
"newApiKey": "{{new_generated_api_key}}",
"newPrefix": "ak_live_xxxx",
"previousPrefix": "ak_live_yyyy",
"scopes": [
"ticketing:read",
"ticketing:write",
"users:read"
],
"environment": "live",
"status": "active",
"rotation": {
"rotatedAt": "2025-07-10T13:10:00.000Z",
"rotatedBy": {
"id": "{{user_id}}",
"name": "{{user_name}}",
"email": "{{user_email}}"
},
"rotationCount": 3,
"gracePeriod": {
"enabled": true,
"minutes": 60,
"previousKeyValidUntil": "2025-07-10T14:10:00.000Z"
}
}
},
"timestamp": "2025-07-10T13:10:00.000Z"
}
Important

The newApiKey value is only returned once during rotation. Store it securely as it cannot be retrieved again.


What Gets Preserved

SettingPreservedNotes
idYesKey ID remains the same
nameYesName unchanged
descriptionYesDescription unchanged
scopesYesAll permissions preserved
protocolYesProtocol setting preserved
ipWhitelistYesIP restrictions preserved
environmentYesEnvironment unchanged
apiKeyNoNew key value generated
prefixNoNew prefix from new key
expiresAtOptionalCan be reset with expiresIn

Error Responses

Status CodeErrorDescription
400 Bad RequestInvalid key ID formatThe keyId is not a valid UUID
401 UnauthorizedAuthentication failedBearer token is missing, expired, or invalid
403 ForbiddenInsufficient permissionsUser lacks permission to rotate API keys
404 Not FoundAPI key not foundNo API key exists with the specified ID
409 ConflictKey is inactiveCannot rotate an inactive, expired, or revoked key
429 Too Many RequestsRate limit exceededToo many rotation requests (security protection)
500 Internal Server ErrorServer errorUnexpected server-side error

Example Error Response:

{
"success": false,
"error": {
"code": "KEY_INACTIVE",
"message": "Cannot rotate an inactive API key",
"details": "The API key is currently in 'revoked' status. Create a new key instead."
},
"timestamp": "2025-07-10T13:10:00.000Z"
}

Notes

  1. Security Best Practice: Rotate API keys periodically (e.g., every 90 days) as part of your security policy.

  2. New Key Only Shown Once: The new API key value is only returned in the rotation response. Store it immediately in a secure location.

  3. Preserved Settings: All configuration (name, scopes, IP whitelist, etc.) is preserved. Only the key value changes.

  4. Grace Period: If supported, use gracePeriodMinutes to allow both old and new keys to work during a transition period. This prevents service disruption during key update in integrations.

  5. Immediate Invalidation: Without a grace period, the old key is immediately invalidated. Ensure all integrations are ready to switch.

  6. Rotation Count: The rotationCount tracks how many times the key has been rotated, useful for auditing.

  7. ID Preservation: The key ID (keyId) remains unchanged, making it easy to track the same logical key across rotations.

  8. Preferred Over Delete: Rotation is preferred over delete + create because:

    • Preserves all configuration
    • Maintains audit history
    • Keeps the same key ID for tracking
  9. Required Roles: Typically requires tenant_admin or api_admin role to rotate API keys.

  10. Rate Limiting: Rotation may be rate-limited to prevent abuse (e.g., max 10 rotations per hour per key).

  11. Notifications: Key owners may receive notifications about the rotation depending on tenant settings.

  12. Related Endpoints:

    • GET /api-keys — List all API keys
    • GET /api-keys/{keyId} — Get API key details
    • POST /api-keys — Create new API key
    • PUT /api-keys/{keyId} — Update API key settings
    • DELETE /api-keys/{keyId} — Revoke/delete API key
    • POST /api-key/validate — Validate an API key