Authentication methods and security configuration
This document covers authentication methods and security configuration for the API.
For API routes, request/response schemas, and the job queue system, see API Operations.
The application supports two authentication methods:
Dashboard users don't need to manage API keys for using the dashboard - they're authenticated via their login session. API keys are only needed for external programmatic access.
The recommended way to manage API keys is through the web dashboard:
Dashboard-generated keys are stored securely in workspaces/api-keys.json and are automatically validated by the API.
For security, the application stores only a hash of dashboard-generated keys (the plaintext key is shown only once when generated).
For backwards compatibility or CI/CD environments, you can also configure API keys via environment variables in your .env.local file:
# Optional - only needed if not using dashboard-generated keys ADMIN_API_KEY=your-secure-admin-api-key-here VALID_API_KEYS=client-key-1,client-key-2,client-key-3 # Rate limiting and IP restrictions API_RATE_LIMIT=100 ALLOWED_IPS=192.168.1.100,10.0.0.50
Variable descriptions:
ADMIN_API_KEY - Admin API key (32+ characters recommended)VALID_API_KEYS - Comma-separated list of client keysAPI_RATE_LIMIT - Requests per hour per clientALLOWED_IPS - IP whitelist (use * to allow all, or comma-separated list)The API validates authentication in the following order:
workspaces/api-keys.json)ADMIN_API_KEY environment variable)VALID_API_KEYS environment variable)External clients must include an API key in their requests using either:
Option A: x-api-key header
curl -X POST http://localhost:3000/api/ask \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key-here" \
-d '{
"workspaceId": "workspace-123",
"question": "How does this code work?"
}'
Option B: Authorization Bearer token
curl -X POST http://localhost:3000/api/ask \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key-here" \
-d '{
"workspaceId": "workspace-123",
"question": "How does this code work?"
}'
const response = await fetch('http://localhost:3000/api/ask', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': 'your-api-key-here',
},
body: JSON.stringify({
workspaceId: 'workspace-123',
question: 'How does this code work?',
}),
});
const data = await response.json();
console.log(data);
import requests
url = 'http://localhost:3000/api/ask'
headers = {
'Content-Type': 'application/json',
'x-api-key': 'your-api-key-here'
}
data = {
'workspaceId': 'workspace-123',
'question': 'How does this code work?'
}
response = requests.post(url, json=data, headers=headers)
result = response.json()
print(result)
| Status | Error | Description |
|---|---|---|
| 401 | Invalid API key | The provided API key is not valid |
| 403 | Access denied from this IP address | Client IP is not whitelisted |
| 429 | Rate limit exceeded | Client has exceeded their request quota |
ALLOWED_IPS, deploy behind a trusted reverse proxy (Traefik/Caddy) that overwrites X-Forwarded-For / X-Real-IP.The easiest way to generate secure API keys is through the dashboard:
The dashboard automatically generates cryptographically secure keys.
If you need to generate keys for environment variable configuration:
Using Node.js crypto:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"Using OpenSSL:
openssl rand -hex 32
Using Python:
python3 -c "import secrets; print(secrets.token_hex(32))"