This guide walks you through rotating exposed API keys and revoking leaked credentials without breaking your applications. You’ll learn how to identify which keys are compromised, generate replacements, update your systems, and remove the old keys — all while minimizing downtime and security risk.
What You’ll Need
- Access to the account or dashboard where the API keys were issued (e.g., cloud provider, SaaS platform, or your own server).
- Administrator or owner permissions on that account — most platforms restrict key rotation to users with write/delete privileges.
- A list of all services, applications, or scripts that use the affected key. This can come from your codebase, configuration management, or a secrets scanner.
- A way to update configurations quickly: CI/CD pipeline, configuration management tool, or direct access to servers.
- A secrets manager (optional but recommended) to store the new key securely and avoid hardcoding.
- Access to your team’s communication channel (e.g., Slack, email) to coordinate the change and avoid breaking production.
Step-by-Step
- Assess the exposure. Determine where the key was leaked — a public GitHub repo, a client-side JavaScript bundle, a log file, or a third-party breach. Check the platform’s audit logs to see if the key was used after the leak. If you see unusual activity, treat it as actively compromised.
- Create a replacement key. In the issuing platform’s dashboard, navigate to API keys or credentials. Generate a new key with the same permissions as the old one. Do not delete the old key yet — you’ll need it for comparison and to avoid breaking services before they’re updated.
- Update all configurations and code. Replace the old key with the new one in every application, environment variable, configuration file, or secrets manager entry. Search your entire codebase for the old key string (or its prefix) to catch hardcoded instances. Use a secrets scanner if available.
- Test the new key. Run a quick API call or a full integration test to confirm the new key works. Check that all services that depend on the API can authenticate and perform their expected actions.
- Revoke the old key. Once all systems are confirmed working with the new key, go back to the dashboard and delete or revoke the old key. Some platforms allow you to set a “grace period” or disable rather than delete — use that if you’re worried about missing a service.
- Monitor for residual use. After revocation, watch the platform’s logs for any requests still using the old key. If you see any, that means something was missed — update it immediately and consider a more thorough audit.
- Document the incident. Record what happened, which key was affected, when it was rotated, and any services that were updated. This helps with future audits and prevents repeated mistakes.
Common Mistakes
- Deleting the old key before updating services — this causes immediate downtime. Always rotate first, then revoke.
- Only updating the primary environment — forgetting staging, development, or CI/CD pipelines that also use the key.
- Hardcoding the new key in the same place — if the original leak was from a repo, the new key will be exposed again. Use environment variables or a secrets manager.
- Not checking for key reuse — some platforms allow the same key to be used across multiple services; make sure you update all of them.
- Ignoring rate limits — rotating many keys at once can trigger rate limits or lockouts. Do it in batches if you have many keys.
Tips
- Use a secrets manager (like HashiCorp Vault, AWS Secrets Manager, or a similar tool) to store keys and automatically rotate them on a schedule.
- Set up alerts for when a key is used in a way that suggests it’s been leaked (e.g., from an unexpected IP or region).
- When generating a new key, consider using a different naming convention or adding a prefix to distinguish it from the old one.
- If your platform supports it, use short-lived keys or tokens that expire automatically — this limits the damage if they leak.
- Keep a changelog of key rotations to help with debugging and compliance.
Troubleshooting
Services fail after updating the key. Check that the new key is correctly formatted (no extra spaces, quotes, or line breaks). Verify that the key has the right permissions — sometimes a new key is created with different scopes than the old one. Also, ensure that the key is being read from the correct environment variable or config file.
Old key still works after revocation. Some platforms have a propagation delay — wait a few minutes and try again. If it still works, you may have deleted the wrong key or there’s a cached copy in a service. Double-check the key ID and revoke again.
You can’t find all instances of the old key. Search for the key’s prefix (e.g., “sk_live_”) in your codebase, logs, and configuration files. Use a secrets scanner like TruffleHog or git-secrets to scan your entire git history. If you’re still missing something, consider rotating again after a more thorough sweep.
New key is immediately rejected. Ensure the key is active (some platforms require you to activate it after creation). Check that you’re using the correct authentication method (e.g., Bearer token vs. query parameter). If the platform has IP allowlisting, add the IP addresses of your services.
Rate limit errors after rotation. If you rotate many keys at once, you might hit rate limits. Wait a few minutes and retry. If the issue persists, contact the platform’s support.
Frequently Asked Questions
Q: How do I know if my API key was actually leaked?
A: Check the platform’s audit logs for unusual usage (e.g., requests from unexpected IPs, spikes in traffic). If you found the key in a public repo or a client-side app, assume it’s compromised.
Q: Can I just disable the key instead of deleting it?
A: Yes, disabling is safer if you’re unsure whether all services have been updated. Disable the key, then delete it after a week or so of no usage.
Q: Should I rotate all my keys or just the leaked one?
A: Only the leaked one is necessary, but if you suspect other keys were exposed (e.g., a shared credentials file), rotate all of them.
Q: How often should I rotate API keys proactively?
A: It depends on your security policy. Many organizations rotate keys every 90 days or when an employee with access leaves. For high-risk keys, consider more frequent rotation.
Q: What if the leaked key is in a public GitHub repo?
A: Immediately revoke that key, then remove it from the repo history (using tools like git filter-repo) to prevent further exposure. Even after removal, the key is still compromised — rotate it.
Q: Can I automate the rotation process?
A: Yes, many platforms offer APIs to create and revoke keys, and secrets managers can automate rotation on a schedule. This reduces manual error and response time.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Hide PHP Version HTTP Header: How To
- CentOS Stream for Developers: A Practical Guide
- How to Replace a Game Controller Battery Safely
- Man-Sized 3D Printer? Why You Probably Don’t Need One100.0 cm³/h
- Scroll Horizontally with Mouse Wheel by Holding Shift5.00 min/km
- iPadOS 16 Supported Devices: Full List