Blog · Security

Where to store API keys on a Mac (and why .env files leak)

Most API keys on a developer's Mac live in plain-text files that several other programs can read. Here is where they usually end up, how each place leaks, and a setup that keeps them locked until the moment a command needs one.

Short answer: keep API keys in the macOS Keychain, which encrypts them and can ask for Touch ID before releasing one, and pass a key to a program only for the one command that needs it. Don't keep them in ~/.zshrc, a committed .env file, notes apps or chat messages.

Where API keys usually end up

Nobody decides to store a secret badly. A key arrives in a dashboard, you need it working in the next five minutes, and it lands in whichever place is closest. These are the usual ones.

1. Your shell profile

Adding export OPENAI_API_KEY=sk-… to ~/.zshrc is the most common shortcut. It works everywhere, which is the problem: every process you start inherits it. That includes your editor, its extensions, a dev server, and any package install script that runs during npm install. Profiles also tend to end up in public dotfiles repositories.

2. A .env file in the project

A .env file is better than a profile because it's scoped to one project, but it is still a plain-text file on disk, and it leaks in quiet ways:

3. Shell history

Typing export STRIPE_KEY=sk_live_… or passing a key as a command argument saves it to ~/.zsh_history in plain text. Arguments are also visible to other processes through ps while the command runs.

4. Notes apps and chat

Notes, Slack DMs and "send to myself" emails are synced to servers, indexed by search, and kept for years. They feel private, but they're one of the most common places for keys to leak.

The better place: the macOS Keychain

Every Mac already ships with a secrets store. The Keychain keeps items encrypted on disk and unlocks them with your login. Each item can control which apps may read it, and it can require Touch ID or your password before giving a secret back. Safari passwords and Wi-Fi passwords already live there.

You can use it from the terminal today with the built-in security command:

# Save a key. -w as the last option prompts for the value,
# so it never touches your shell history.
security add-generic-password -a "$USER" -s openai -w
# Hand it to one command only, not to your whole shell.
OPENAI_API_KEY="$(security find-generic-password -a "$USER" -s openai -w)" npm start

This already fixes the biggest leaks: the key is encrypted at rest, it isn't in a project folder or a dotfile, and only the command you ran receives it. Two things the raw security command won't do for you:

Doing the same with Keyholdr

I built Keyholdr because I kept doing the quick, unsafe thing. It's a small native Mac app (menu bar plus a keyholdr command) that stores each key as its own Keychain item and asks for Touch ID every time a secret is read, whether you copy it in the menu bar or use it in the terminal.

# Add a key: the value is read from a hidden prompt, never an argument.
keyholdr add openai --label work
# Run one command with keys injected. Touch ID, then the key
# exists only inside that process: not in a file, not in history.
keyholdr run -e OPENAI_API_KEY=openai -e GITHUB_TOKEN=github -- npm start
# Or load keys into the current shell session only.
eval "$(keyholdr env openai anthropic)"

keyholdr env picks conventional names for you (OPENAI_API_KEY, ANTHROPIC_API_KEY, GITHUB_TOKEN and so on). keyholdr list shows every key's age without reading a single secret, and flags old ones so you know what to rotate. There's also a full-screen terminal UI and a ⌃⌥⌘K shortcut that opens the vault from anywhere. Everything stays on your Mac: no account, no sync, no server.

How the options compare

WhereEncrypted at restAsks before readingSeen by every process
~/.zshrc exportNoNoYes
Project .envNoNoAny process that opens it
Notes app / chatVariesNoSynced to servers
Keychain via securityYesOnly for other appsOnly the command you run
Keychain via KeyholdrYesTouch ID on every readOnly the command you run

A five-minute checklist

  1. Search your dotfiles. Run grep -nE "KEY|TOKEN|SECRET" ~/.zshrc ~/.zprofile ~/.bash_profile and move anything you find into the Keychain.
  2. Check your history. grep -nE "sk-|ghp_|AKIA" ~/.zsh_history finds the usual suspects. Rotate any key that shows up, then remove the lines.
  3. Ignore .env before the first commit. Commit a .env.example with names only, and add .env to .dockerignore too.
  4. Never pass a secret as an argument. Use a prompt or standard input instead.
  5. Rotate anything that was ever committed. Rewriting history doesn't help once a key has been pushed; revoke it and issue a new one.

Try Keyholdr. A native API key vault in your menu bar and terminal, under 1 MB, with Touch ID on every read.

Want the backstory? I built a tool because I kept losing my API keys tells how Keyholdr started.