How to Rotate API Keys Without Downtime: A Developer's Guide
Why Key Rotation Matters
API keys are credentials — and like passwords, they should be rotated regularly. Leaked keys are the #1 cause of unauthorized API usage and surprise bills. Regular rotation limits the blast radius if a key is ever compromised.
The Zero-Downtime Rotation Strategy
Most providers let you have multiple active keys simultaneously. Use this to your advantage:
Step 1: Generate a New Key
Create a new key while the old one is still active:
- OpenAI: API Keys page → "Create new secret key"
- Stripe: API Keys dashboard → "Create secret key"
- Anthropic: Console → API Keys → "Create Key"
Step 2: Test the New Key
Validate the new key before deploying:
- Use Api Key Chain for an instant health check
- Run your test suite against the new key
- Verify it has the correct permissions and rate limits
# Quick validation with curl
curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: Bearer NEW_KEY" \
https://api.openai.com/v1/models
# Should return 200
Step 3: Deploy with Both Keys (Overlap Period)
This is the critical step — deploy your app with both keys configured:
# .env — during rotation
OPENAI_API_KEY=old-key-here
OPENAI_API_KEY_NEW=new-key-here
Your app should try the new key first, falling back to the old one:
const API_KEYS = [
process.env.OPENAI_API_KEY_NEW,
process.env.OPENAI_API_KEY,
].filter(Boolean);
async function callWithFallback(endpoint, options) {
for (const key of API_KEYS) {
try {
const response = await fetch(endpoint, {
...options,
headers: { ...options.headers, Authorization: `Bearer ${key}` }
});
if (response.ok) return response;
if (response.status === 401) continue; // Try next key
return response;
} catch {
continue;
}
}
throw new Error('All API keys failed');
}
Step 4: Monitor and Verify
After deploying:
- Monitor error rates for 24-48 hours
- Check that traffic is flowing through the new key
- Confirm no 401 errors in logs
- Test the old key one final time to ensure you haven't missed any services
Step 5: Revoke the Old Key
Once traffic is stable on the new key:
- Go to the provider dashboard
- Revoke/delete the old key
- Remove it from environment variables
- Deploy the cleaned-up config
Automation with Api Key Chain
With Api Key Chain Premium, you can:
- Save all your keys encrypted in one place
- Schedule health checks to validate keys automatically
- Get alerts when a key is nearing expiration
- Bulk test multiple keys before and after rotation
Rotation Schedule by Risk Level
| Service Tier | Rotation Frequency | Reason |
|---|---|---|
| Production payment (Stripe) | Every 30 days | Financial risk |
| Production AI (OpenAI, etc.) | Every 90 days | Cost and data risk |
| Development/Testing | Every 180 days | Lower impact if leaked |
| Read-only keys | Every 365 days | Minimal blast radius |
Common Rotation Mistakes
- ❌ Revoking the old key before deploying the new one → downtime
- ❌ Not testing the new key before deploy → surprise failures
- ❌ Forgetting to update CI/CD environment variables → CI breaks
- ❌ Rotating during peak traffic hours → schedule for low-traffic windows
🔄 Pro tip: Set a recurring calendar event for key rotation. Use Api Key Chain's health checks the day before rotation day to ensure all current keys are still valid.
Related Posts
Get an Email When Your API Key Stops Working
API keys get revoked, expire or run out of credit without warning. Here is how to watch a key and get an email when it fails, and again when it works.
Never Miss an API Key Expiry Date Again
Many API keys and tokens expire on a fixed date. Record the date once and get reminder emails 30, 7 and 1 day before, and on the day it expires.
Check Your API Keys in CI With a GitHub Action
Fail the build when an API key in your secrets or .env file has stopped working. Use the open-source Api Key Chain CLI or GitHub Action.