API Key Architecture (Explanation)
Last Updated: 2026-02-04
Content-Type: Explanation
Audience: Developers
Goals
- Compatibility: Support OpenAI-style clients and “standard” API key headers.
- Security: Never store plaintext secrets; allow only one-time reveal.
- Operability: Admin can revoke/rotate keys; keys have scopes and optional expiry.
How keys work
- Keys are issued as copy/paste-friendly strings (example prefix:
kira_sk_...). - The database stores:
keyPrefix(first 12 chars) for lookupkeyHash(bcrypt) for validationsecretCiphertext/iv/tag(AES-256-GCM) until the user reveals once
After reveal:
- The encrypted secret fields are cleared
secretRevealedAtis set
Authentication flow
- Request arrives with either
X-API-KeyorAuthorization: Bearer ... - If the Bearer token looks like a JWT, JWT auth handles it
- Otherwise, API key auth:
- loads by
keyPrefix - bcrypt-compares full secret to
keyHash - attaches
req.apiKeyandreq.user
- loads by
OpenAI-compatible routing
Kirakira mounts OpenAI-compatible routes at /v1/* and reuses the same internal interaction pipeline as the Portal.