Automate the busywork

How to Keep Your API Keys Safe

Keep API keys out of your code and out of shared documents. Store them in environment variables or a secrets manager, give each key the narrowest permissions possible, and replace any key that has been exposed. These three habits prevent most key-related incidents, and none of them takes long to set up.

An API key works like a password for a program. Anyone who holds it can act as you, within the key’s permissions. Leaked keys have run up large bills and exposed customer data, usually because someone pasted a key where it did not belong.

Why keys leak

Most leaks are accidents, not attacks. Common routes include:

Automated scanners crawl public repositories constantly, looking for strings that resemble keys. A leaked key can be found within minutes of being published, so assume that anything posted publicly is compromised, even if you remove it quickly.

Store keys outside your code

The first rule is to keep the secret and the code apart. In a script, read the key from an environment variable, which is a setting that lives outside the file. The code refers to the variable’s name, and the actual value is set separately on your machine or server.

If you use a no-code platform, use its built-in connection or credentials area instead of typing a key into a text field inside a step. Those areas store the secret more carefully and hide it from casual view.

For teams, a secrets manager is the next step. These services keep keys in an encrypted store, control who can read them and record each access. Many hosting providers and automation tools include a simple version.

Keep keys out of version control

If you use Git or a similar tool, add the file that holds your local settings to the ignore list before the first commit. Deleting a key from a later commit does not help, since the history still contains it.

Some people keep a template file with placeholder values, committed to the repository, and a real file with actual values that stays on their machine. New teammates copy the template and fill in their own.

Before you publish any project, search it for strings that look like keys. Free scanning tools can do this for you and are worth running.

Limit what each key can do

Most services let you create several keys with different permissions. Use that. A workflow that only reads data should have a read-only key. A workflow that posts to one channel should not hold a key that can delete a whole account.

Where possible, restrict keys further by allowed address or by the kinds of requests they may make. If a restricted key leaks, the damage is much smaller.

Create a separate key for each automation, and name each clearly. When one key is exposed, you can replace it without breaking everything else. It also makes the access logs easy to read.

Watch usage and set limits

Check the usage dashboard that most providers offer. A sudden jump in requests, or requests at odd hours, can signal that someone else is using your key.

If the service lets you set a spending cap or alerts, turn them on. A low cap on an experimental key turns a potential disaster into an annoyance.

Review your list of keys every few months. Delete the ones you no longer use. An abandoned key that still works is an open door nobody is watching.

What to do after a leak

If you suspect a key has been exposed, act quickly and in this order.

  1. Revoke or rotate the key in the provider’s dashboard, so the old one stops working.
  2. Create a new key and update your automations to use it.
  3. Check the usage logs for activity you did not cause.
  4. Find how the leak happened and fix that route, or it will happen again.

Do not wait to confirm that the key was actually abused. Replacing a key takes a few minutes, and waiting costs far more if it turns out to be misused. If the key protected customer data, you may also have legal duties to report a breach; check with a legal professional in your area.

Sharing access with other people

Sometimes a colleague or contractor needs to run your automation. Avoid sending them your key. Use the service’s own sharing options, such as inviting them as a user with limited rights, or give them a separate key that you can revoke later.

If you must share a secret, use a password manager’s sharing feature instead of email or chat. Messages stay in archives long after you forget them.

Finally, build the habit of asking where a secret will end up before you handle it. If a key will pass through a chat tool, a spreadsheet or a screen share, it will probably linger there. Choose a path that leaves no copies behind, and tell collaborators the same rule, so one careless paste does not undo all your care.

Your next step

Make a list of every API key you currently use, and note where each one is stored. Look for any in scripts, documents or chat history. Move those to environment variables or a credentials area.

Then open each service’s dashboard and confirm that each key has only the permissions it needs. Replace any key that has ever sat in a shared place, and set a calendar reminder to repeat this check in three months.