API key rotation — changing the active credential for a service while maintaining continuity of access — is one of the most operationally painful security practices because most systems treat API keys as static configuration that's set once and never touched, yet key rotation is the primary defence against undetected credential compromise
The previous articles on this site covered random string generation basics, the birthday problem and token length, why Math.random() fails for security, UUID variants, the test data vs security token distinction, and hex vs Base64url format selection. This article addresses API key lifecycle management — how keys are provisioned, stored, distributed, rotated, and revoked, and the specific operational patterns that make key rotation achievable without service disruption.
The key lifecycle: from generation to retirement
An API key's lifecycle has five stages:
1. Generation: a cryptographically secure random string created by the issuing service. Quality: minimum 128 bits of entropy, ideally 256 bits. Format: typically hex or Base64url encoding, often with a service-specific prefix (sk_live_, ghp_, AKIA...).
2. Distribution: delivering the key to the consuming application. The distribution channel must be as secure as the key itself — a key generated securely but transmitted via unencrypted email is no more secure than an insecure key.
3. Storage: where the key lives in the consuming application. This is where most security failures occur — keys end up in source code, logs, environment variable dumps, and error messages.
4. Rotation: replacing the active key with a new one. The critical operational challenge: the new key must be active before the old key is revoked, or a service interruption occurs.
5. Revocation: permanently invalidating a key. Should be immediate and verifiable.
Secure distribution channels
How NOT to distribute API keys:
- Email (plaintext, sits in inboxes indefinitely)
- Slack or Teams messages (logged, searchable, retained)
- Jira tickets or GitHub issues
- Shared Google Docs or Notion pages
- Verbal communication (memorable but unauditabile)
How to distribute API keys:
- Secrets management platforms: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, Google Secret Manager. Keys are stored encrypted, accessed programmatically with audit logging, and never seen by humans after initial storage.
- Encrypted one-time link: tools like 1Password (share with link), Doppler, or purpose-built OTP link generators. Key is viewable once and then expires.
- CI/CD secret injection: GitHub Actions secrets, GitLab CI variables, CircleCI contexts. Keys are stored in the CI system and injected as environment variables at runtime — never appear in source code or logs.
The zero-downtime rotation pattern
The operational challenge of rotation: if you revoke key A and generate key B, any application using key A stops working the moment A is revoked.
The zero-downtime rotation sequence:
- Generate new key B in the issuing service (both A and B are now valid)
- Distribute key B to all consuming applications via secrets management
- Update applications to use key B (deploy configuration changes)
- Verify key B is working — monitor for authentication errors
- Revoke key A after a grace period (minutes to hours, depending on deployment rollout speed)
Why this requires multiple valid keys simultaneously: the issuing service must support having two (or more) active keys at the same time. Services that only allow one active key at a time make zero-downtime rotation impossible.
Services that handle this well: GitHub (multiple API tokens per account), Stripe (can have multiple API keys of each type), AWS (up to 2 access keys per IAM user).
The secret scanning problem: keys already in repositories
GitHub's secret scanning automatically scans all commits and issues to public repositories for patterns matching known API key formats. If a key is detected, GitHub notifies the owner and notifies the service (if the service has registered their key format with GitHub).
The problem: once a key has been committed to a Git repository, it's in the Git history — even if you delete it from the latest version. The commit history is permanent.
The common but wrong response: delete the file from the latest commit. The key is still in every previous commit.
The correct response when a key is committed:
- Immediately revoke the key — assume it's compromised from the moment it was committed
- Generate a new key and distribute it through proper channels
- Remove the key from Git history using
git filter-branchorgit filter-repo(rewrites all commits) — this is disruptive if others have cloned the repository - Audit access logs for unexpected use of the compromised key
Why immediate revocation is critical: secret scanning bots — both legitimate (GitHub's) and malicious — scan commits within seconds of push. A key committed at any point is potentially already compromised before you notice.
Environment variable patterns: what's safe and what isn't
Environment variables are the standard mechanism for injecting secrets into applications. But not all environment variable approaches are equally secure:
Safe: secrets manager reads the secret at application startup and stores it in an in-memory environment variable. Secret is never in source code or on disk.
Less safe: .env file containing API keys. If accidentally committed (or if .gitignore is misconfigured), the .env file exposes all secrets. If the .env file is left on servers, any process that can read the filesystem can access it.
Unsafe: hardcoding keys directly in source code (obvious).
Also unsafe but commonly done: storing keys in application configuration files (config.json, application.yml, settings.py) that are committed to version control.
The minimum safe practice: .env files must be in .gitignore before the first commit; use pre-commit hooks that scan for API key patterns and block commits containing them.
How to use the Random String Generator on sadiqbd.com
- For API key generation: the tool generates cryptographically secure random strings — use it to generate new keys during rotation, choosing the length that matches the entropy requirements of your application (minimum 32 characters hex = 128 bits; 43 characters Base64url = 256 bits)
- For rotation key pairs: generate two keys simultaneously (before you start rotation) — this gives you both the new key to distribute and a verified secure key ready before any revocation
- For non-production environments: generate separate API keys for development, staging, and production — never use production API keys in non-production environments, where they might be exposed in logs or debugging output
Frequently Asked Questions
How often should API keys be rotated? It depends on the key's scope, value, and exposure level. NIST guidelines suggest annual rotation for most credentials; more frequent rotation (quarterly or monthly) for high-privilege keys or keys that have broad access. The practical advice: implement rotation automation before worrying about frequency — if rotation is manual and painful, it won't happen at all. Once rotation is automated and zero-downtime, rotate as frequently as your risk tolerance requires. For keys that appear in many places (third-party integrations, long-running services), quarterly rotation with automated distribution is a reasonable target for most organisations.
Is the Random String Generator free? Yes — completely free, no sign-up required.
Try the Random String Generator free at sadiqbd.com — generate cryptographically secure random strings for API keys, tokens, and secrets.