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.

James MurdzaUpdated 3 min read

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:

  1. Issue the new key.
  2. Set the variable.
  3. Deploy.
  4. Verify.
  5. 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:

CredentialOverlap supportedRetiring the old keyReference
AWS access keysYes (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 keysYes (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 keysYes (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 tokensYesDelete the old token once you’ve verified that the new token works.Personal access tokens
OpenAI API keysYesRevoke the old key once you’ve verified that the replacement works.Best practices for API key safety
Anthropic API keysYesDisable the old key first, since that’s reversible, then delete it once requests succeed with the new key.Claude Platform authentication docs
Stripe API keysYes (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:

All 30 guides →

Ready to get started?

Join millions of developers deploying applications on Railway