API Key Storage and Rotation: A Practical Guide for Small Teams
Small teams usually get authentication right at the edges and wrong in the middle. The login page is careful, the database is encrypted, and then an API key for a paid service sits in a repository as a plain string with the comment that it is temporary. API key rotation and secure storage are not enterprise ceremony; they are the difference between a leaked key that costs you an afternoon and one that costs you a month of unexplained billing.
Direct answer: Store API keys in a secrets manager or an encrypted environment store, never in source control, scope each key to one service and one environment, and rotate on a schedule plus immediately after any suspected exposure. Rotation works cleanly only if the key can be swapped without a redeploy.
Where API Keys Should Live
There are only three acceptable places: a managed secrets service, an encrypted parameter store, or environment variables injected at deploy time from one of those. Everything else is a compromise you will regret.
- Never in Git. Once committed, a key is in the history forever and in every clone.
- Never in a mobile app or browser bundle. Anything shipped to a client is public.
- Never in a log line. Scrub authorization headers at the logging layer.
- Never in a shared document or chat message. Use a link to the secret store instead.
If a key has already been committed, treat it as compromised and rotate before removing it. Deleting the commit does not delete the copies.
Scope Every Key Narrowly
One key for everything is convenient until it leaks. Issue separate keys per environment and per service: staging, production, background jobs and reporting should not share credentials. If a reporting key leaks, you revoke it without touching the live verification flow.
Many platforms, including SMS Feel, support multiple keys per account so you can isolate workloads this way. Check the API documentation for how keys map to permissions before you design the split.
When to Rotate
- On a schedule. Ninety days is a reasonable default for most service keys.
- On staff change. Any time someone with access leaves the project.
- On suspected exposure. Immediately, without waiting to confirm.
- On vendor change. When a provider is retired, revoke rather than leaving dormant credentials.
- On architecture change. Splitting a service is a good moment to split its keys too.
How to Rotate Without Downtime
The safe pattern is overlap. Create the new key, deploy it alongside the old one, confirm traffic succeeds on the new key, then revoke the old one. The failure mode to avoid is the hard cutover that breaks production at the exact moment everyone is watching.
- Create a new key in the provider dashboard and store it in the secrets manager.
- Deploy configuration that reads the new key while the old key stays valid.
- Verify successful requests in logs for a full traffic cycle.
- Revoke the old key and record the rotation date.
- Remove the old value from every environment, including local developer machines.
Make Rotation Cheap
Rotation is painful when the key is hardcoded in twenty places. Make it a single configuration read at startup, or fetch it from the secrets manager at request time with a short cache. Add a startup check that fails fast if the key is missing or obviously malformed, so a configuration mistake surfaces at deploy rather than during a user signup.
Document the rotation procedure in one page. The person rotating the key during an incident is rarely the person who designed the system.
Detecting Leaks Before They Cost You
- Enable secret scanning on your repositories and on pull requests.
- Alert on unusual usage patterns such as a sudden spike in requests or new destination countries.
- Track last-used timestamps per key so you can retire idle credentials.
- Review key inventory quarterly and revoke anything nobody can explain.
Usage anomalies catch leaks that scanning misses, especially when the key was copied rather than committed.
Frequently Asked Questions
Is an environment variable safe enough?
It is acceptable when it is injected at deploy time from a secret store and not baked into an image. A hardcoded default value in the code is not acceptable.
How often should API keys be rotated?
Every ninety days is a sensible baseline. Rotate immediately after any exposure, staff departure or vendor change regardless of the schedule.
Can I use the same key for staging and production?
No. Separate keys per environment limit the blast radius of a leak and let you revoke staging credentials freely during testing.
What do I do if a key is committed to a public repository?
Revoke it first, then create a replacement, then clean the history. Removing the commit alone does not undo the exposure.
How do I rotate a key used by a mobile backend?
The mobile client should never hold the key. Route those calls through your backend, where the key can be rotated centrally without shipping an app update.
Conclusion
Good API key rotation is a habit, not a project. Store keys in one controlled place, scope them narrowly, rotate on a schedule and keep the procedure short enough that anyone can follow it. To put it into practice, create an SMS Feel account, issue separate keys for staging and production, and rehearse a rotation before you need one.
Related reading: more integration and security guides on the SMS Feel blog.