API Key Management
Best practices for managing AI Gateway API keys securely.
Overview
API keys are the primary authentication method for AI Gateway. Proper key management is critical for security.
Key Types
Gateway API Keys
- Format:
gw_sk_[random-string] - Purpose: Authenticate requests to AI Gateway
- Created in: AI Gateway Settings
Provider API Keys
- Format: Varies by provider (
sk-...for OpenAI,sk-ant-...for Anthropic) - Purpose: Authenticate with upstream providers
- Stored in: AI Gateway Settings (encrypted)
Creating Gateway Keys
- Navigate to AI Gateway Settings
- Under "Gateway API Keys", click Create API Key
- Enter a descriptive name:
production-apidevelopmentci-testingmobile-app
- Click Create
- Immediately copy the key - it won't be shown again
Key Storage
Environment Variables (Recommended)
# .env (local development)
GATEWAY_API_KEY=gw_sk_your_key_here
# Never commit .env files!Secret Managers
For production, use a secret manager:
AWS Secrets Manager:
aws secretsmanager create-secret \
--name gateway-api-key \
--secret-string "gw_sk_your_key_here"HashiCorp Vault:
vault kv put secret/gateway api_key="gw_sk_your_key_here"Vercel:
vercel env add GATEWAY_API_KEY productionKubernetes Secrets
apiVersion: v1
kind: Secret
metadata:
name: gateway-secrets
type: Opaque
stringData:
GATEWAY_API_KEY: gw_sk_your_key_hereKey Rotation
Rotate keys regularly (recommended every 90 days):
Rotation Process
- Create new key in the dashboard
- Update applications to use new key
- Deploy the changes
- Monitor for any errors using old key
- Revoke old key after confirming all traffic uses new key
Zero-Downtime Rotation
// Support multiple keys during transition
const apiKeys = [
process.env.GATEWAY_API_KEY_NEW, // New key
process.env.GATEWAY_API_KEY_OLD, // Old key (fallback)
].filter(Boolean);
let currentKeyIndex = 0;
async function callWithKeyRotation(fn) {
for (let i = 0; i < apiKeys.length; i++) {
try {
return await fn(apiKeys[currentKeyIndex]);
} catch (error) {
if (error.status === 401) {
currentKeyIndex = (currentKeyIndex + 1) % apiKeys.length;
continue;
}
throw error;
}
}
throw new Error('All keys failed');
}Key Scopes (Coming Soon)
Future release will support scoped keys:
| Scope | Permissions |
|---|---|
read | List models only |
chat | Chat completions |
full | All operations |
Security Best Practices
Do
- Use different keys for different environments
- Store keys in environment variables or secret managers
- Rotate keys regularly (every 90 days)
- Monitor key usage in the dashboard
- Revoke keys immediately when compromised
- Use descriptive names for keys
Don't
- Commit keys to version control
- Share keys between applications
- Log keys in application output
- Expose keys in client-side code
- Use production keys in development
- Keep unused keys active
Monitoring Key Usage
Dashboard Metrics
View per-key metrics:
- Go to AI Gateway > Settings
- Click on a key to see:
- Request count
- Last used timestamp
- Error rate
Audit Logs
Track key usage:
- Go to Settings > Audit Logs
- Filter by "API Key" events
- View key creation, usage, and revocation
Responding to Compromised Keys
If a key is exposed:
Immediate Actions
- Revoke the key immediately in Settings
- Create a new key
- Update applications with new key
- Deploy changes
Investigation
- Check Audit Logs for unauthorized usage
- Review Request Logs for suspicious patterns
- Assess Cost Impact of any misuse
Prevention
- Review how the key was exposed
- Improve secret management practices
- Consider using IP allowlists
Key Naming Conventions
Use consistent naming for easy management:
{environment}-{application}-{purpose}
Examples:
production-api-primaryproduction-api-backupstaging-api-testingdevelopment-localci-github-actions
Next Steps
- IP Allowlist - Restrict by IP
- Authentication - API auth details
- Rate Limiting - Usage limits
On This Page
- Overview
- Key Types
- Gateway API Keys
- Provider API Keys
- Creating Gateway Keys
- Key Storage
- Environment Variables (Recommended)
- Secret Managers
- Kubernetes Secrets
- Key Rotation
- Rotation Process
- Zero-Downtime Rotation
- Key Scopes (Coming Soon)
- Security Best Practices
- Do
- Don't
- Monitoring Key Usage
- Dashboard Metrics
- Audit Logs
- Responding to Compromised Keys
- Immediate Actions
- Investigation
- Prevention
- Key Naming Conventions
- Next Steps