API Key Security: Issuance, Scope, Rotation, and Revocation

API keys are bearer credentials: whoever possesses a valid key can often use it as the identity represented by that key. They can be practical for identifying applications, applying quotas, or authenticating low-risk integrations, but they should not automatically be treated as proof of an individual user's identity or as the sole protection for sensitive data. Their security depends on how they are issued, scoped, stored, monitored, and revoked.

Begin by choosing the right authentication mechanism for the client. A browser or mobile application cannot keep a shared secret confidential from its users. For user access, use an appropriate user authentication and authorization flow. For service-to-service access, prefer short-lived workload credentials or federated identity when available. API keys can remain useful for public API identification or partner integrations, but the design should reflect what happens if the key is copied.

Issue keys with identity and limited authority

Figure: API Key Security: Issuance, Scope, Rotation, and Revocation
Figure: API Key Security: Issuance, Scope, Rotation, and Revocation

Create a distinct key for each application, environment, and owner where feasible. Avoid a single organization-wide key embedded in multiple services; it prevents attribution and turns one leak into a broad incident. Generate keys with a cryptographically secure random source and sufficient entropy. Use a non-secret prefix or identifier that helps operators locate the record without exposing the credential itself.

Assign only the scopes, resources, and operations required. A key that only needs read access to one API should not be able to change account settings or access unrelated tenants. Separate production and development credentials, and avoid wildcard scopes by default. Enforce authorization server-side on each request; possession of a key should not bypass object-level checks or business rules.

If users can create keys, show the secret only at creation and store a verifier rather than the raw value when the authentication design allows it. Use a one-way keyed or password-hashing strategy appropriate to the key format and lookup requirements. Ensure the verification process does not create a timing or enumeration leak. Keep key metadata such as owner, creation time, scope, environment, last-used time, and expiration separate from the secret value.

Protect storage and transmission

Transmit keys only over TLS and never place them in URLs, where they can appear in browser history, proxy logs, analytics, and referrer data. Use authorization headers or another documented mechanism that avoids logging the credential. Redact keys in request logs, exception reports, traces, and support bundles. Test redaction using a synthetic key and verify that each logging layer handles it.

Store service credentials in a secrets manager or protected workload configuration, not in source code, frontend bundles, public repositories, or container image layers. Restrict access to the secret store and audit retrieval. If the key is exposed to a client application, assume that users can extract it and enforce restrictions at the API provider: domain, app identity, quotas, allowed endpoints, or other constraints where available. Those restrictions reduce misuse but do not make a public key secret.

Rotate without breaking clients

Rotation should have an overlap plan. Issue a replacement key, distribute it through a protected channel, allow the client to switch, verify successful requests, then revoke the old key. Set a short and explicit overlap period. Track which client has migrated and notify an owner before expiry. For critical integrations, test the sequence in staging and document rollback conditions.

Automate rotation when the provider and consumers support it. Dynamic or short-lived credentials can reduce the window of exposure, but they require reliable renewal and clock handling. Stopping a workload does not necessarily revoke a stolen key; revoke or expire the credential at the issuing service. Avoid routine rotation schedules that cause needless operational churn for human passwords, and follow the credential provider's risk-based guidance.

Detect abuse and make revocation fast

Record the key identifier, owner, scope, source context, request outcome, and usage volume without logging the key itself. Alert on unusual geography or network source, sudden usage increases, unexpected operations, and use after a planned retirement date. A “last used” field can help find stale credentials, but low-frequency integrations need an owner review rather than automatic deletion based on a short inactivity window.

Provide an emergency revocation path that is available to the right responders and does not depend on the compromised key. Revoke a suspected credential at the issuer, identify affected services and data, rotate any related secrets, and preserve access logs for investigation. If a key was committed to a repository, deleting the visible line is not enough: revoke it because clones and history may retain the value.

Review key policy and client behavior

Return consistent authentication errors that do not reveal whether a particular key identifier exists. Rate-limit failed credential attempts and protect key-management endpoints with stronger user authentication. Require reauthentication or additional approval for creating high-privilege keys. Make key permissions visible to the owner, and provide a safe way to name, disable, rotate, and delete keys.

Test that an expired, revoked, malformed, or insufficiently scoped key is rejected. Test that one customer's key cannot access another tenant's resources, and that the same key does not work in the wrong environment. Verify that quotas apply across service replicas and that an emergency revocation takes effect within the documented time.

Operational checklist

Choose an identity mechanism appropriate to the client; issue distinct, high-entropy keys; grant narrow scopes; keep them out of URLs, source, and logs; protect key storage; define overlap-based rotation; track an accountable owner; monitor use; and rehearse revocation. Treat a key as a credential with a lifecycle, not as a permanent configuration string.

API keys can support controlled integrations, but they are not a substitute for user identity or authorization. Design the service so exposure has a limited blast radius, every key is attributable, and responders can revoke it quickly. Review the model whenever an integration gains new permissions or moves into a higher-risk environment.

References