Rotate an API key
Goal
Replace a live sk_… key — scheduled rotation, suspected leak, departing integration — with zero downtime for its callers.
There is no update action on /api/api-keys. Rotation is: create a replacement, switch callers over, verify, revoke the old key.
Prerequisites
- An identity allowed to manage keys for the target account — see the authority model below.
- The list of places the old key is used (env vars, CI secrets, scripts).
Who may rotate — the real authority model
Key management is account-scoped self-service, not system_admin-only:
- Any account-bound principal (human via JWT, or a machine key holding an
account_id) canlist,get,create,revoke, and readusage-logswithin its own account. - A non-system caller cannot mint
is_internalkeys or*:*scope (403), and requestedscopesare clipped to what the caller holds — aclikey can rotate itself but can never widen. - Only
system_adminor an unbound internal machine principal can mint wildcard/internal keys or act across accounts. - Rotating someone else's key works only if it lives in your account; foreign-account keys are invisible to you.
Steps
Find the key.
GET /api/api-keyslists your account's keys (id,key_prefix,name,scopes,is_active,expires_at,last_used_at). Raw keys are never stored — identify bykey_prefixandname.curl -H "x-api-key: $SPINE_API_KEY" "$BASE_URL/api/api-keys"Create the replacement with the same (or narrower) scopes:
curl -X POST -H "x-api-key: $SPINE_API_KEY" -H "Content-Type: application/json" \ -d '{"name":"<same-purpose> v2","scopes":"<same scopes>","rate_limit":100}' \ "$BASE_URL/api/api-keys"The raw
sk_…is returned once — copy it now. Omittingscopesinherits the caller's scopes.- Switch callers. Update env vars/secrets (
SPINE_API_KEY,SPINE_CLI_API_KEY, integration configs, Netlify env, CI secrets) and redeploy/restart anything that caches the key. - Verify the switch.
GET /api/api-keys?action=usage-logs&api_key_id=<new-id>should show calls landing on the new key; the old key's usage should go quiet. For a leaked key, don't wait — go straight to step 5. Revoke the old key:
curl -X POST -H "x-api-key: $SPINE_API_KEY" -H "Content-Type: application/json" \ -d '{"id":"<old-key-id>"}' "$BASE_URL/api/api-keys?action=revoke"Revocation sets
is_active = false— soft, auditable, and it writes anapi_key.revokedaudit log entry. Keys are never hard-deleted through the API.
Verify
GET /api/api-keysshows old keyis_active: false, new key active.- Callers work on the new key; nothing still references the old prefix in configs.
Common mistakes
- Rotating the key you're calling with mid-flight — create and swap callers before revoking, or your own revoke call authenticates with a dying credential (it still completes — the revoke lands first).
- Expecting
system_adminto be required — it isn't; account members self-serve. If that surprises you, audit who in the account can reach key management, not the endpoint's gate. - Losing the raw key — only the hash and 8-char
key_prefixpersist; there is no "show key again." - Revoking before checking usage-logs — a silent caller (cron job, forgotten CI) dies at revocation with no warning.
Reference
- API keys entity page — fields, scopes, rate limits.
- api-keys function reference — exact request/response shapes and 401/403 semantics.