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:
- Committed by accident. One missing line in
.gitignore, or agit add -f, and the key is in history. Deleting it in a later commit doesn't remove it from history; anyone with the repo can still find it. - Copied into images. A
COPY . .in a Dockerfile copies.envinto the image unless it's listed in.dockerignore. - Synced and backed up. Projects in iCloud-synced Desktop or Documents folders, zip files sent to a colleague, and old backups all carry the file with them.
- Readable by anything running as you. Any tool, script or extension running under your user account can open it, without a prompt.
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:
- Ask for Touch ID. Items created this way have no Touch ID requirement, so any script running as you can read them through the same command.
- Make it pleasant. There's no list of what you've stored, no way to tell an old key from a new one, and long commands like the one above are easy to get wrong when you're in a hurry. That's how keys end up back in
.env.
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
| Where | Encrypted at rest | Asks before reading | Seen by every process |
|---|---|---|---|
~/.zshrc export | No | No | Yes |
Project .env | No | No | Any process that opens it |
| Notes app / chat | Varies | No | Synced to servers |
Keychain via security | Yes | Only for other apps | Only the command you run |
| Keychain via Keyholdr | Yes | Touch ID on every read | Only the command you run |
A five-minute checklist
- Search your dotfiles. Run
grep -nE "KEY|TOKEN|SECRET" ~/.zshrc ~/.zprofile ~/.bash_profileand move anything you find into the Keychain. - Check your history.
grep -nE "sk-|ghp_|AKIA" ~/.zsh_historyfinds the usual suspects. Rotate any key that shows up, then remove the lines. - Ignore
.envbefore the first commit. Commit a.env.examplewith names only, and add.envto.dockerignoretoo. - Never pass a secret as an argument. Use a prompt or standard input instead.
- 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.