API Key Management
API Keys are straightforward access credentials, but they’re only secure when generated correctly, stored safely, used responsibly, and rotated on schedule.
Overview
API Key management covers the entire lifecycle of API Keys, from generation through storage, usage, rotation, and revocation. API Keys are secret strings that clients use to identify themselves and, in some cases, to authorize their actions with an API. They’re simple to implement but carry risks if exposed, copied, or left unchanged for extended periods. Solid key management generates strong, unique Keys, stores them only as hashes, transmits them over TLS, limits their scope and validity, enables effortless rotation, and supports immediate revocation. Additionally, Keys should operate under the principle of least privilege, and their usage should be tracked through logging and monitoring.
Key Components
API Key Generation
API Keys must be generated using cryptographically secure random methods. They need to be long and unique enough to resist brute-force attacks. A minimum length of 32 bytes, combined with a mix of letters, numbers, and special characters, strengthens security considerably. Keys should never be predictable or based on user data.
API Key Storage
On the server side, API Keys must never be stored in plain text. Instead, store a hash of the Key—using SHA-256 or bcrypt—alongside a reference that maps the Key to the user. The plain-text Key is shown to the user only once, during creation. On the client side, Keys should never end up in code repositories, logs, or publicly accessible configuration files.
Transport and Transmission
API Keys must always travel over HTTPS. Avoid passing them in URLs, since URLs can appear in browser history, server logs, and Referer headers. Use the Authorization header or a dedicated header like X-API-Key instead.
Scopes and Permissions
API Keys should be issued with minimal permissions. A Key for read-only access must not permit write operations. Scopes like read:users or write:orders help restrict permissions and reduce the blast radius if a Key is compromised.
API Key Rotation
Rotation means regularly—or when compromise is suspected—issuing new Keys and invalidating old ones. Supporting multiple active Keys per client streamlines rotation: the old Key remains functional while the new one is distributed. After a transition period, the old Key is disabled.
Revocation and Deactivation
API Keys must be revocable at any time without restarting the entire service. A revoked Key is rejected on the next request. Dashboards or admin APIs should allow quick blocking if a Key is exposed.
Logging and Monitoring
Usage of API Keys should be logged. Key details include timestamps, IP addresses, which Key was used, the endpoint accessed, and the result. Monitoring detects unusual patterns: sudden traffic spikes, geographic anomalies, or access to unexpected endpoints.
Secret Management
Applications that consume API Keys should use Secret Management solutions like HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault. These tools store secrets encrypted, support rotation, and prevent Keys from leaking into code or configuration.
Incident Response for Leaks
If an API Key is exposed, it must be revoked immediately. Investigate whether unauthorized actions occurred. Notify customers and internal teams, and provision a replacement Key.
Documentation and Policies
Clear guidelines for generating, using, rotating, and handling API Keys are essential. Developers and customers should understand how to store Keys safely, report breaches, and know the consequences of policy violations.
Practical Example
A SaaS provider issues API Keys to customers who want to access the Orders API.
Creating a new Key:
POST /api/v1/api-keys
Authorization: Bearer USER_TOKEN
Content-Type: application/json
{
"name": "Integration Warehouse",
"scopes": ["read:orders", "write:shipments"]
}
Response:
HTTP/1.1 201 Created
Content-Type: application/json
{
"keyId": "key-abc-123",
"key": "sk_live_51H8x...9zA2",
"scopes": ["read:orders", "write:shipments"],
"createdAt": "2026-07-01T10:00:00Z",
"expiresAt": "2027-07-01T10:00:00Z"
}
The plain-text Key is displayed only once. The server stores only a hash. The customer uses the Key:
GET /api/v1/orders
Authorization: Bearer sk_live_51H8x...9zA2
If the Key leaks, the customer can revoke it instantly from the dashboard and create a replacement. The old Key’s permissions are immediately invalid.
FAQ: API Key Management
1. What is an API Key?
2. How should an API Key be generated?
3. Should an API Key be stored in plain text?
4. What is API Key rotation?
5. Why should API Keys be revocable?
6. What are scopes in the context of API Keys?
7. Where should API Keys be stored on the client side?
8. What is a Secret Management tool?
9. How do you detect unusual API Key usage?
10. What should you do if an API Key leaks?
11. Should API Keys have an expiration date?
12. What’s the difference between an API Key and an OAuth2 Token?
13. What is a Key prefix?
sk_live_ or sk_test_. It helps distinguish Keys visually and prevents accidental use in the wrong environment.14. What is a hash in the context of API Keys?
15. Why shouldn’t API Keys be passed in URLs?
Sources
- https://owasp.org/API-Security/editions/2023/en/0x11-t10/
- https://cheatsheetseries.owasp.org/cheatsheets/Key_Management_Cheat_Sheet.html
- https://www.vaultproject.io/
Recommended books on API security
If you want to dive deeper into API key management, secret management, and API security, we recommend the following books:
IT CyberSecurity
Books about IT security, authentication, encryption and security best practices
The Web Application Hacker's Handbook von Dafydd Stuttard, Marcus Pinto
Bei Amazon ansehenAffiliate-Link: Bei einem Kauf erhalten wir möglicherweise eine Provision.




