Skip to main content

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

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:
string
required
Human-readable name for the API key (e.g., “Production CLI Tool”)
integer
default:365
Days until expiration (default: 365 days)
Request Example:
Response:
Save the api_key securely - it will not be shown again! Store it in a password manager or environment variable. The plain-text key is hashed with bcrypt before storage.
Status Codes:
Created
API key created successfully
Bad Request
Invalid input or exceeded key limit
Unauthorized
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:
Response:
Status Codes:
Success
API keys retrieved successfully
Unauthorized
Not authenticated

POST //rotate

Rotate an API key. Generates a new API key while keeping the same key_id. The old key is invalidated immediately. Path Parameters:
string
required
Unique identifier of the API key to rotate
Request Example:
Response:
Update your client configuration immediately! The old API key is invalidated and will no longer work. Save the new new_api_key securely.
Status Codes:
Success
API key rotated successfully
Unauthorized
Not authenticated
Not Found
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:
string
required
Unique identifier of the API key to revoke
Request Example:
Response: No content (HTTP 204) Status Codes:
No Content
API key revoked successfully
Unauthorized
Not authenticated
Not Found
API key not found

POST /validate

Validate API key and return JWT (internal endpoint for Kong plugin).
Internal Endpoint: This endpoint is called by the Kong API gateway plugin and is not intended for direct client use. It is excluded from the public API schema.
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:
string
required
API key to validate
Request Example:
Response:
Status Codes:
Success
API key validated, JWT issued
Bad Request
Missing X-API-Key header
Unauthorized
Invalid or expired API key
Internal Server Error
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

Never hardcode API keys in code
  • Store in environment variables: export API_KEY=ak_live_...
  • Use .env files (add to .gitignore)
  • Use secret managers in production: HashiCorp Vault, AWS Secrets Manager
  • Password managers for personal keys: 1Password, LastPass
Never:
  • Commit API keys to Git
  • Store in plain text files
  • Share via email or chat
  • Embed in client-side code
Rotate keys regularly
  • Quarterly rotation for production keys
  • Immediately after security incidents
  • When team members leave
  • If key may have been exposed
Zero-downtime rotation:
  1. Create new API key
  2. Update client configuration
  3. Test with new key
  4. Revoke old key
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)
Monitor expiration and renew before expiry:
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
5 key limit per user
  • Encourages key hygiene and rotation
  • Prevents accumulation of unused keys
  • Revoke unused keys regularly
Monitor usage:
  • Track last_used timestamp
  • 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 gateway
2

Kong validates API key

Kong calls /api/v1/api-keys/validate (internal endpoint) with the API key
3

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 backend
5

Backend processes request

Backend receives JWT-authenticated request and processes normally
This pattern provides:
  • ✅ Long-lived credentials (API keys) for clients
  • ✅ Short-lived tokens (JWT) for internal services
  • ✅ Centralized API key validation
  • ✅ Standard JWT-based authorization throughout system
See ADR-0034: API Key JWT Exchange for design details.

Comparison with Other Auth Methods


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!