Overview
API keys are:- Stored in Keycloak (user attributes with bcrypt hashing)
- Exchanged for JWTs (standardized authentication)
- User-attributed (linked to user or service principal identity)
- Rotatable (without changing client code)
- Expirable (default 365 days)
- Rate-limited (5 keys per user)
Creating API Keys
Via API
- ⚠️ Save the
api_keyvalue immediately - it cannot be retrieved later.
Via CLI
Using API Keys
Direct API Calls
Python Client
JavaScript Client
How API Keys Work
Exchange Flow
Storage Format
API keys are stored in Keycloak user attributes as bcrypt hashes:Managing API Keys
List Keys
Rotate Key
Revoke Key
Security Best Practices
Storage
- ✅ DO: Store in secrets manager (Vault, AWS Secrets Manager, GCP Secret Manager)
- ✅ DO: Use environment variables in production
- ❌ DON’T: Commit to version control
- ❌ DON’T: Log API keys
- ❌ DON’T: Include in error messages
Rotation
- ✅ DO: Rotate keys every 90-180 days
- ✅ DO: Rotate immediately if compromised
- ✅ DO: Test new key before revoking old key
- ❌ DON’T: Share keys across multiple systems
- ❌ DON’T: Reuse revoked keys
Usage
- ✅ DO: Use HTTPS only
- ✅ DO: Monitor key usage (last_used timestamp)
- ✅ DO: Set appropriate expiration dates
- ✅ DO: Use different keys for dev/staging/production
- ❌ DON’T: Use in client-side code (browsers)
- ❌ DON’T: Share keys in public repositories
Rate Limiting
API keys are subject to rate limits based on user tier:| Tier | Rate Limit |
|---|---|
| Free | 60 req/min, 1,000 req/hour |
| Premium | 300 req/min, 10,000 req/hour |
| Enterprise | 1,000 req/min, 100,000 req/hour |
Monitoring
Key Usage
Check last_used timestamps to identify unused keys:Prometheus Metrics
Troubleshooting
Invalid API Key
Symptom:401 Unauthorized - Invalid or expired API key
Solutions:
- Verify key is copied correctly (no whitespace)
- Check expiration date:
GET /api/v1/api-keys - Ensure user account is enabled
- Verify key hasn’t been revoked
Kong Validation Timeout
Symptom:504 Gateway Timeout
Solutions:
- Check MCP server health
- Verify Kong→MCP connectivity
- Increase timeout in Kong plugin config
- Check Keycloak availability
Cache Issues
Symptom: Old key still works after revocation Solutions:- Wait for cache TTL (5 minutes by default)
- Clear Kong cache:
kong cache:purge apikey:* - Reduce cache TTL if faster revocation needed
Migration from Kong-Stored Keys
Step 1: Export Existing Keys
Step 2: Recreate in Keycloak
Step 3: Update Clients
Provide new API keys to clients (gradual rollout).Step 4: Sunset Old Keys
After migration period, remove Kong key-auth plugin.Examples
CLI Tool Authentication
Webhook Integration
References
Architecture & API Docs
- ADR: ADR-0034: API Key to JWT Exchange
- API Reference: API Keys API
- Kong Plugin: kong-apikey-jwt-exchange
- Security: See “Security Best Practices” section above
Related Guides
- Service Principals - Machine-to-machine authentication for batch jobs and CI/CD
- SCIM Provisioning - Automated provisioning of API keys for users
- Identity Federation - External IdP integration
- Authentication Migration - Migrating to v2.8.0 authentication