API Key Security Best Practices
Learn how to securely store, manage, and share API keys and secrets. Protect your applications from credential exposure and security breaches.
Why API Key Security Matters
Exposed API keys are one of the leading causes of security breaches. Attackers use automated tools to scan code repositories, public pastes, and chat logs for credentials. A single exposed key can lead to data breaches, unauthorized charges, and service abuse.
Common Mistakes to Avoid
Hardcoding in Source Code
Never embed API keys directly in your code. Keys in source files end up in version control, build artifacts, and can be extracted from compiled applications.
Committing to Git
Even if you delete a key from your code, it remains in git history forever. Attackers actively scan public repositories for exposed credentials.
Sharing via Chat or Email
Credentials shared in Slack, Teams, or email remain searchable indefinitely. These platforms aren't designed for secure secret transmission.
Using the Same Key Everywhere
Reusing API keys across environments (dev, staging, prod) or services increases blast radius if one key is compromised.
Security Best Practices
Use Environment Variables
Store API keys in environment variables that are injected at runtime. This keeps secrets out of your codebase and allows different values per environment.
- Use .env files for local development (add to .gitignore)
- Use platform secrets management for production
- Never log environment variable values
Implement Secret Rotation
Regularly rotate API keys to limit the impact of potential exposure. Automated rotation reduces the window of vulnerability if a key is compromised.
- Set up automatic key rotation schedules
- Ensure your app can handle key updates gracefully
- Keep old keys active briefly during rotation
Apply Least Privilege
Create API keys with only the permissions needed for their specific use case. Avoid using admin or full-access keys when limited permissions suffice.
- Create separate keys for different services
- Use read-only keys when writes aren't needed
- Restrict keys to specific IP ranges or domains
Use a Secrets Manager
Enterprise teams should consider dedicated secrets management solutions that provide encryption, access control, audit logging, and rotation.
- HashiCorp Vault, AWS Secrets Manager, Azure Key Vault
- Centralized access control and audit trails
- Automatic secret injection into applications
Monitor and Audit Access
Enable logging for API key usage to detect unusual patterns. Set up alerts for suspicious activity like requests from unexpected locations.
- Review API usage logs regularly
- Set up anomaly detection alerts
- Track which team members access which secrets
API Key Exposure Prevention
API key exposure prevention starts before a key is created. Use scoped credentials, keep keys out of source code, avoid persistent chat history, and rotate keys as soon as exposure is suspected. Treat Slack, email, CI logs, browser screenshots, and public issue trackers as places where secrets should never appear.
Prevent Leaks
Add secret scanning, keep `.env` files in `.gitignore`, and block keys from entering repos, tickets, and logs.
Limit Blast Radius
Use separate API keys for each environment and permission scope, then restrict IPs, domains, or resources when available.
Respond Quickly
Revoke leaked keys, deploy replacements, audit recent usage, and notify affected owners before restoring normal access.
How to Store API Keys Securely
The safest API key storage option depends on who needs the key. Applications should load keys from a secrets manager or platform secret store. Developers should keep local keys in ignored `.env` files or a password manager, then use a secure .env file sharing workflow when a teammate needs local setup values.
Environment Variables
Local development and simple runtime configuration
Use `.env` files only for local development, keep them out of git, and use separate values for development, staging, and production.
Platform Secrets
Hosted apps on Vercel, Netlify, Heroku, or cloud platforms
Store production API keys in the platform's encrypted secrets or environment variable store instead of checking files into the repo.
Secrets Manager
Teams that need access control, audit logs, and rotation
Use tools like AWS Secrets Manager, Google Secret Manager, Azure Key Vault, Doppler, Infisical, or Vault for centralized key management.
Password Manager
Human access to shared non-runtime credentials
Store keys people need to retrieve manually in a team password manager, then deliver initial access through a one-time link.
API Key Management Best Practices for Teams
API key management is an ownership problem as much as a storage problem. Teams need a repeatable process for creating, storing, sharing, rotating, and revoking every credential.
- Assign every API key an owner, purpose, environment, and rotation date.
- Create separate keys per app, service, environment, and teammate when the provider supports it.
- Prefer scoped keys over broad admin keys, especially for CI and contractor workflows.
- Keep an inventory of active keys so offboarding and incident response are not guesswork.
- Document the secure handoff workflow for local `.env` setup and temporary access.
API Key Rotation and Leak Response
If an API key appears in git, chat, email, logs, or a public paste, assume it is compromised. Create a replacement key, update the consuming services, revoke the exposed key, and review provider logs for unexpected usage.
OWASP-style API key storage best practices come down to least privilege, encrypted storage, auditability, and quick revocation. Never rely on deleting a message or rewriting git history as your only cleanup step.
Quick Reference: Do's and Don'ts
Do
- Store keys in environment variables or secrets managers
- Use different keys for development, staging, and production
- Rotate keys regularly and after any suspected exposure
- Restrict key permissions to only what's needed
- Use self-destructing links when sharing keys with teammates
- Revoke keys immediately when team members leave
- Enable API key usage monitoring and alerts
Don't
- Hardcode API keys in source code
- Commit keys to version control (even private repos)
- Share keys via Slack, email, or chat platforms
- Use production keys in development environments
- Share keys with more people than necessary
- Ignore key expiration warnings
- Store keys in plain text files on servers
Secure Sharing When You Must
Sometimes you need to share an API key with a teammate, contractor, or collaborator. When that happens, avoid chat and email. Use a secure, self-destructing link that ensures the key is viewed once and then permanently deleted.
- One-time access ensures the key isn't left in message history
- End-to-end encryption protects the key during transmission
- Automatic destruction means no cleanup needed
Need to Share a Secret Securely?
When you need to share an API key, password, or credential with someone, use SnapPwd to create a secure, self-destructing link. No signup required.
Share a Secret Securely