Rotate Third-Party API Keys on Railway
How to rotate third-party API keys on Railway, including which providers allow two active keys and what to do when yours allows only one.
TL;DR
- Rotate third-party API keys with each provider's steps.
- If your provider lets two keys be valid at once, issue the new key, update the variable on Railway, deploy, verify that it works, and then revoke the old key.
- Some providers invalidate the old key the moment you generate a new one. In that case, keep the gap short by deploying right away.
A third-party API key is a credential that proves who you are to an outside service. Rotating it means replacing it with a new key and revoking the old one, so the old one is no longer valid. This article covers how providers handle that overlap and what to do when they don't. For how variable changes, deploys, and healthchecks work on Railway, see Rotate environment variables on Railway without downtime.
How to rotate third-party API keys
A third-party API key comes from a provider, such as Stripe or AWS. Check whether your provider lets two keys be valid at once. If it does, follow the ordered steps in the environment variable rotation guide:
- Issue the new key.
- Set the variable.
- Deploy.
- Verify.
- Revoke the old key.
If several services use the same key, rotate it once as a shared variable, and add a healthcheck so Railway only switches traffic to a deployment that works. Both are covered in that article. The table shows how each provider retires the old key:
| Credential | Overlap supported | Retiring the old key | Reference |
|---|---|---|---|
| AWS access keys | Yes (up to 2 keys per user) | Set the old key to inactive, then delete it once nothing breaks. | Update access keys |
| Google Cloud service account keys | Yes (up to 10 keys per service account) | Disable the old key, then delete it once your applications use the new key. | Key rotation |
| GitHub Apps private keys | Yes (up to 25 private keys) | Delete the old key after your applications switch to the new key. | Private keys for GitHub Apps |
| GitHub personal access tokens | Yes | Delete the old token once you’ve verified that the new token works. | Personal access tokens |
| OpenAI API keys | Yes | Revoke the old key once you’ve verified that the replacement works. | Best practices for API key safety |
| Anthropic API keys | Yes | Disable the old key first, since that’s reversible, then delete it once requests succeed with the new key. | Claude Platform authentication docs |
| Stripe API keys | Yes (for up to 7 days) | When you rotate the key in the Dashboard, choose a later expiry, not Now, which deletes the old key immediately. Both keys work until the expiry. | API keys |
Providers that only allow one active key
Some providers invalidate the old key the moment you generate a new one, so no overlap is possible. This means that downtime is practically unavoidable. To keep the gap short, generate the new key, set the variable, and deploy right away, and rotate when traffic is low.
Secrets other than API keys
Webhook secrets, OAuth client secrets, and similar secrets follow the same rules as API keys. If the provider lets two be valid at once, use the same steps. If not, change both sides together and rotate when traffic is low.
FAQ
How do I know the old credential is no longer in use?
Check the credential provider’s dashboard for a “last used” date. Then check your deploy logs for authentication errors.
Summary
To avoid downtime, keep the old key valid until every service is running on the new one. Downtime can’t always be avoided: providers that allow only one active key have no overlap period. In these cases, rotate during low-traffic periods and deploy right after changing the credential to keep the gap as short as possible.
See also: