feat: add support for custom user api keys #129
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
API Keys Setup Guide
This document explains how to configure custom API keys for AI providers in the application.
Overview
Users can configure their own API keys for the following AI providers:
System Requirements
Encryption Key
An encryption key is required for storing user API keys securely. Generate one with:
Add it to your
.envfile:Database Migration
Run the database migration to add the
apiKeyscolumn:cd db npx drizzle-kit pushOr if using migrations:
cd db npx drizzle-kit migrateEnvironment Variables
Required
Optional System Keys
These are used as fallback when users don't provide their own keys:
How It Works
For Users
For Developers
Key Storage
user.apiKeysJSON columnENCRYPTION_KEYProvider Selection
The system automatically selects a provider based on this priority:
API Endpoints
GET /api/user/api-keys- Check which providers are configuredPUT /api/user/api-keys- Add/update API keysDELETE /api/user/api-keys/:provider- Remove provider keysSecurity Considerations
ENCRYPTION_KEYis backed up securelyENCRYPTION_KEYrequires re-encrypting all stored keysGetting API Keys
Anthropic
OpenAI
OpenRouter
AWS Bedrock
Troubleshooting
"ENCRYPTION_KEY environment variable is not set"
Make sure
ENCRYPTION_KEYis set in your.envfile. Generate one with:"Failed to decrypt key"
This can happen if:
API Key Not Working
Development
Building the Packages
After making changes, rebuild the packages:
Testing API Keys
Test adding a key via the UI or API:
Check configured keys:
curl http://localhost:4000/api/user/api-keys \ -H "Authorization: Bearer <your-token>"Model Selection
Each provider now supports custom model selection. When configuring your API keys in the Settings UI, you'll see a "Model ID (Optional)" field where you can specify which model to use.
Default Models (if not specified)
claude-sonnet-4-20250514gpt-4oanthropic/claude-sonnet-4-20250514anthropic.claude-3-sonnet-20240229-v1:0Example Model IDs
OpenRouter:
anthropic/claude-sonnet-4-20250514openai/gpt-4google/gemini-prometa-llama/llama-2-70b-chatAnthropic:
claude-sonnet-4-20250514claude-opus-4-20250514claude-3-5-sonnet-20241022OpenAI:
gpt-4ogpt-4-turbogpt-3.5-turboAWS Bedrock:
anthropic.claude-3-sonnet-20240229-v1:0anthropic.claude-3-opus-20240229-v1:0anthropic.claude-3-haiku-20240307-v1:0