Overview
The API Keys API provides endpoints for creating and managing API keys - long-lived authentication credentials for programmatic access. API keys are ideal for scripts, automation, and integrations that need persistent authentication without interactive login.v2.8.0 adds API key management with secure bcrypt hashing and Kong gateway integration for API key→JWT exchange.
Use Cases
- CLI Tools: Command-line interfaces and developer tools
- Scripts & Automation: Cron jobs, shell scripts, automated tasks
- CI/CD Integration: Build pipelines, deployment automation
- Third-Party Integrations: External systems that need API access
- Development & Testing: Local development without SSO
API Key vs Service Principal
- API Keys
- Service Principals
User-scoped credentials
- Tied to a specific user account
- Inherit user’s permissions
- Limited to 5 keys per user
- Expire after configurable period (default: 365 days)
- Ideal for: CLI tools, scripts, personal automation
Base URL
Authentication
All endpoints require user authentication:Authorization: Bearer {token}- Users can only manage their own API keys
Endpoints
POST /
Create a new API key for the current user. Creates a cryptographically secure API key with bcrypt hashing. Maximum 5 keys per user - revoke an existing key before creating more. Request Body:Human-readable name for the API key (e.g., “Production CLI Tool”)
Days until expiration (default: 365 days)
API key created successfully
Invalid input or exceeded key limit
Not authenticated
GET /
List all API keys for the current user. Returns metadata for all keys (name, created, expires, last_used). Does not include the actual API keys. Request Example:API keys retrieved successfully
Not authenticated
POST //rotate
Rotate an API key. Generates a new API key while keeping the samekey_id. The old key is invalidated immediately.
Path Parameters:
Unique identifier of the API key to rotate
API key rotated successfully
Not authenticated
API key not found or not owned by current user
DELETE /
Revoke an API key. Permanently deletes the API key. This action cannot be undone. Any clients using this key will immediately lose access. Path Parameters:Unique identifier of the API key to revoke
API key revoked successfully
Not authenticated
API key not found
POST /validate
Validate API key and return JWT (internal endpoint for Kong plugin). This endpoint validates an API key and issues a JWT for the associated user. It implements the API key→JWT exchange pattern described in ADR-0034. Headers:API key to validate
API key validated, JWT issued
Missing X-API-Key header
Invalid or expired API key
Failed to issue JWT
Using API Keys
1. Create API Key
2. Use API Key (Direct)
3. Use API Key (via Kong Gateway)
When Kong gateway is configured with the API key plugin:4. Environment-Based Configuration
Security Best Practices
Key Storage
Key Storage
Never hardcode API keys in code
- Store in environment variables:
export API_KEY=ak_live_... - Use
.envfiles (add to.gitignore) - Use secret managers in production: HashiCorp Vault, AWS Secrets Manager
- Password managers for personal keys: 1Password, LastPass
- Commit API keys to Git
- Store in plain text files
- Share via email or chat
- Embed in client-side code
Key Rotation
Key Rotation
Rotate keys regularly
- Quarterly rotation for production keys
- Immediately after security incidents
- When team members leave
- If key may have been exposed
- Create new API key
- Update client configuration
- Test with new key
- Revoke old key
Expiration Strategy
Expiration Strategy
Set appropriate expiration periods
- Short-lived (30-90 days): Development, testing
- Medium-lived (180-365 days): Production tools, CLI
- Long-lived (1-2 years): Stable integrations (with rotation)
Least Privilege
Least Privilege
API keys inherit user permissions
- Create dedicated user accounts for automation
- Grant minimum required permissions
- Use service principals for broader access
- Monitor API key usage in audit logs
Key Limits & Monitoring
Key Limits & Monitoring
5 key limit per user
- Encourages key hygiene and rotation
- Prevents accumulation of unused keys
- Revoke unused keys regularly
- Track
last_usedtimestamp - Alert on dormant keys (unused > 30 days)
- Review keys quarterly
- Revoke keys for decommissioned tools
Kong Gateway Integration
When using Kong API Gateway with the custom API key plugin:1
Kong receives request with X-API-Key
Client sends request with
X-API-Key header to Kong gateway2
Kong validates API key
Kong calls
/api/v1/api-keys/validate (internal endpoint) with the API key3
Backend issues JWT
Backend validates API key and returns JWT token if valid
4
Kong forwards with JWT
Kong replaces
X-API-Key header with Authorization: Bearer <JWT> and forwards to backend5
Backend processes request
Backend receives JWT-authenticated request and processes normally
- ✅ Long-lived credentials (API keys) for clients
- ✅ Short-lived tokens (JWT) for internal services
- ✅ Centralized API key validation
- ✅ Standard JWT-based authorization throughout system
Comparison with Other Auth Methods
| Feature | API Keys | Service Principals | JWT Tokens | Sessions |
|---|---|---|---|---|
| Lifespan | Days-Years | Permanent | Minutes-Hours | Hours-Days |
| Use Case | Scripts, CLI | Services | User auth | Web apps |
| Identity | User | Machine | User | User |
| Rotation | Manual | Manual | Automatic | N/A |
| Limit | 5 per user | Unlimited | N/A | Configurable |
| Revocation | Immediate | Immediate | Wait for expiry | Immediate |
Related Documentation
API Key Management Guide
Complete API key setup and usage guide
Service Principals API
Machine-to-machine authentication
Authentication API
User authentication endpoints
Kong Integration
Kong API gateway configuration
Secure & Scalable: API keys use bcrypt hashing and integrate seamlessly with Kong gateway!