Prompt
How do I store and rotate API keys without putting them in config files?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
The usual pattern is: don’t store API keys in app config files at all. Keep them in a secret store or environment variable system, and rotate them through the provider or an automation workflow.
Good options for storage
1) Environment variables
Best for simple apps and local development.
- Store the key in the runtime environment, not in the repo:
- Linux/macOS shell:
export API_KEY=... - Docker/Kubernetes: inject as env vars from secrets
- Linux/macOS shell:
- Access it in code via the environment.
- Keep
.envfiles out of version control.
Pros: simple, widely supported
Cons: not ideal for large-scale rotation/auditing by itself
2) Secret managers
Best for production.
Examples:
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- HashiCorp Vault
- Doppler, 1Password Secrets Automation, etc.
These provide:
- encrypted storage
- access control
- auditing
- versioning
- rotation hooks
Your app fetches the secret at startup or on demand using its identity/role.
3) Platform-native secret systems
If you run on a platform, use its built-in system:
- Kubernetes Secrets + External Secrets Operator or CSI driver
- AWS ECS task secrets / IAM roles
- GitHub Actions Secrets / GitLab CI Variables / CircleCI contexts
- Cloud Run / App Engine / Azure App Service app settings
How to rotate them safely
Rotation pattern
- Create a new key
- Deploy app to accept both old and new keys temporarily if needed
- Update secret store with the new key
- Restart or reload services if they cache secrets
- Revoke the old key after confirming the new one works
This avoids downtime.
Make rotation easier
- Use a secret manager that supports versions
- Load secrets at runtime instead of baking them into images
- Keep keys short-lived when possible
- Prefer OAuth tokens, service accounts, or workload identities over long-lived static API keys
Practical recommendations
If you’re starting out
- Use environment variables for local dev
- Use a managed secret store in production
- Never commit secrets to git
- Add secret scanning in CI
If you want best security
- Use a secret manager + workload identity
- Rotate automatically on a schedule
- Limit permissions of each key
- Log access to secrets, but never log the secret value itself
Example workflow
- App reads
API_KEYfrom environment - Production environment injects it from AWS Secrets Manager
- A scheduled job generates a new key monthly
- Secret Manager version is updated
- App instances refresh/restart
- Old key is revoked after a grace period
What to avoid
- Hardcoding keys in source code
- Putting them in checked-in config files
- Sending secrets through plain-text emails or chat
- Logging secrets in application logs
- Using one shared key for everything if you can avoid it
If you want, I can show:
- a Node/Python/Go example for reading secrets safely, or
- a rotation setup for AWS/GCP/Azure/Kubernetes.