API Key Management
API keys are simple access credentials, but they’re only secure when generated, stored, used, and rotated properly on a regular basis.
Overview
API key management covers the entire lifecycle of an API key—from generation through storage, usage, rotation, and revocation. API keys are secret strings that clients use to identify themselves and gain authorization to an API. They’re straightforward to implement but introduce risks if exposed, copied, or left unchanged for too long. Solid key management generates strong, unique keys, stores them only as hashes, transmits them over TLS, restricts their validity and permissions, enables simple rotation, and supports immediate revocation. Keys should also follow the principle of least privilege and their usage tracked through logging and monitoring.
Key Components
API Key Generation
API keys must be generated using cryptographically secure randomness. They need to be long and unique enough to resist brute-force attacks. A minimum of 32 bytes combined with letters, numbers, and special characters strengthens security considerably. Keys should never be predictable or derived from user data.
API Key Storage
Never store API keys in plaintext on the server. Instead, hash the key using SHA-256 or bcrypt and maintain a reference linking it to the user. Show the plaintext key to the user only once at creation. On the client side, keep keys out of repositories, logs, and public configuration files.
Transport and Transmission
Always transmit API keys over HTTPS. Avoid putting them in URLs, where they can leak into browser history, server logs, and referrer headers. Use the Authorization header or a custom header like X-API-Key instead.
Scopes and Permissions
Assign API keys the minimum permissions necessary for their purpose. A read-only key should never enable write operations. Scopes such as read:users or write:orders help limit privileges and reduce damage if a key is compromised.
API Key Rotation
Rotation involves regularly issuing new keys and invalidating old ones, or doing so immediately if compromise is suspected. Supporting multiple active keys per client simplifies rotation—the old key continues working while the new one is distributed. After a grace period, the old key is disabled.
Revocation and Deactivation
API keys must be revocable at any time without restarting the entire service. A disabled key is rejected on the next request. Dashboards or management APIs should make it quick to revoke keys if they’re exposed.
Logging and Monitoring
Log all API key usage. Capture timestamps, IP addresses, which key was used, the endpoint accessed, and the result. Monitoring detects unusual patterns: sudden traffic spikes, geographic anomalies, access to unexpected endpoints, or requests outside normal hours.
Secret Management
Applications consuming API keys should use Secret Management tools like HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault. These platforms store secrets encrypted, automate rotation, and prevent keys from ending up in code or configuration files.
Handling Breaches
If an API key is exposed, revoke it immediately. Investigate whether any unauthorized activity occurred. Notify affected customers and internal teams, then issue a replacement key.
Documentation and Policy
Clear guidelines on generating, using, rotating, and handling API keys matter. Developers and customers need to know how to store keys securely, report leaks, and understand the consequences of violations.
Practical Example
A SaaS provider issues API keys to customers who want access to an 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 plaintext key appears only once. On the server, only a hash is stored. The customer uses the key:
GET /api/v1/orders
Authorization: Bearer sk_live_51H8x...9zA2
If the key leaks, the customer revokes it instantly from the dashboard and generates a new one. The old credential becomes invalid right away.
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 plaintext?
4. What is API key rotation?
5. Why should API keys be revocable?
6. What are scopes in API keys?
7. Where should API keys be stored on the client?
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 is leaked?
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?
14. What is a hash in the context of API keys?
15. Why shouldn’t API keys appear 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 reading on API security
To deepen your knowledge of API key management, secret management, and API security, we recommend these 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.




