Prompt
What is the best way to handle API keys and passwords securely?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
The best way to handle API keys and passwords securely is to never hardcode them, and instead use a combination of secure storage, least privilege, and rotation.
Best practices
1. Store secrets outside your code
- Use environment variables for local development.
- Use a secrets manager in production, such as:
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- HashiCorp Vault
- Avoid putting secrets in:
- source code
- config files committed to git
- logs
- client-side code
2. Use least privilege
- Give each API key or account only the permissions it needs.
- Prefer separate keys for separate services or environments.
- Limit access by IP, scope, or role when possible.
3. Rotate secrets regularly
- Change API keys and passwords periodically.
- Rotate immediately if a secret is exposed or suspected compromised.
- Use automation to make rotation easier.
4. Protect secrets in transit and at rest
- Always use TLS/HTTPS when sending secrets.
- Encrypt secrets stored on disk or in databases.
- Restrict who and what can read them.
5. Never expose secrets to users
- Don’t embed API keys in frontend apps, mobile apps, or public repos.
- If a client must access a service, use a backend proxy or short-lived tokens.
6. Audit and monitor
- Log secret access carefully, but never log the secret value itself.
- Monitor for unusual usage, leakage, or unauthorized access.
- Use secret scanning in CI/CD and git repositories.
7. Use short-lived credentials when possible
- Prefer temporary tokens over long-lived static credentials.
- Examples: OAuth access tokens, IAM roles, signed session tokens.
Practical rule of thumb
- Development: environment variables or local secret files excluded from git.
- Production: a managed secrets service + role-based access + rotation.
If you want, I can also show you a secure setup for a specific stack like Node.js, Python, Docker, or AWS.