Create API Key
Store an API key for an AI provider. Keys are encrypted using AES-256-GCM before storage when anENCRYPTION_KEY environment variable is configured.
Authentication
Requires authentication via session cookie or bearer token.Encryption
API keys are protected using multiple layers of security:- AES-256-GCM Encryption: When
ENCRYPTION_KEYis configured, keys are encrypted with a 256-bit key using Galois/Counter Mode, which provides authenticated encryption - SHA-256 Hash: A hash of the key is stored for validation without exposing the plaintext
- Fallback: If no encryption key is configured, keys are hex-encoded (not recommended for production)
base64(nonce || ciphertext || tag).
Request Body
AI provider name. Must be one of:
anthropic, openai, ollamaThe API key or access token for the provider. Must not be empty.
Optional human-readable label for the key (e.g., “Production Key”, “Development”)
Response
UUID of the created API key record
The provider name (anthropic, openai, or ollama)
The optional label provided during creation
Masked preview of the key for identification. Shows first 7 and last 3 characters for keys ≥12 characters, or first 3 and last 2 for shorter keys.
ISO 8601 timestamp of when the key was created
Example
Response Example
Error Responses
400 Bad Request:- Unsupported provider (must be anthropic, openai, or ollama)
- Key value is empty
Delete API Key
Soft-delete an API key. The key is marked as deleted in the database but not permanently removed.Authentication
Requires authentication via session cookie or bearer token.Path Parameters
UUID of the API key to delete
Response
Always
true on successful deletionUUID of the deleted API key
Example
Response Example
Error Responses
404 Not Found: API key not found or already deletedSecurity Notes
- Keys are soft-deleted (marked as deleted, not removed from database)
- Users can only delete their own API keys
- Deleted keys are no longer available for AI operations
Encryption Implementation Details
The encryption system is implemented insrc/crypto.rs and provides:
AES-256-GCM Encryption
- Algorithm: AES-256-GCM (Galois/Counter Mode)
- Key Size: 256 bits (32 bytes)
- Nonce: 12 bytes, randomly generated per encryption
- Format:
base64(nonce || ciphertext || tag) - Tag Size: 16 bytes (automatically appended by GCM)
Key Masking
Keys are masked for display using themask_key function (src/routes/settings.rs:105):
- Keys ≥12 characters: Shows first 7 +
***+ last 3 (e.g.,sk-ant-***440) - Keys 7-11 characters: Shows first 3 +
***+ last 2 - Keys ≤6 characters: Shows
***only
Key Hashing
A SHA-256 hash of each key is stored for validation purposes without requiring decryption. The hash is computed using thehash_key function (src/routes/settings.rs:120).
Decryption Fallback
Thedecode_stored_secret function (src/crypto.rs:53) supports multiple formats for backward compatibility:
- AES-256-GCM encrypted (preferred): Attempts decryption with configured key
- Hex-encoded: Falls back to hex decoding if decryption fails
- Plaintext: Returns as-is if no encryption key is configured (development only)
This multi-format support allows for gradual migration from hex-encoded keys to AES-256-GCM encryption without service disruption.
Implementation Reference
Seesrc/routes/settings.rs:128 (encrypt_key function), src/routes/settings.rs:239 (create endpoint), src/routes/settings.rs:314 (delete endpoint), and src/crypto.rs:16 (encryption implementation).