On a Mac, the safest everyday place for an API key your code uses is the
macOS Keychain: store it once with the security command and read it
into an environment variable only when a script runs. Keep the master copy,
with notes on where each key is used, in an encrypted vault that syncs to
your other devices. Avoid committing keys, pasting them into ~/.zshrc, or
leaving them in .env files that every tool in the folder can read.
A disclaimer: I make Fydra, which is one option for that vault. It has no command-line tool, so for scripts this guide uses the Keychain.
Why not .env files?
.env files are convenient, which is why they’re everywhere. The problems:
- They’re plain text. Anything running as you can read them: editor
extensions,
npminstall scripts, and AI coding assistants that index the project folder. - They travel. Into a commit when
.gitignoremisses one, into Time Machine and cloud backups, into a zip you send a colleague, into a Docker image. - They multiply. One project per key becomes ten copies of the same token, and when you rotate it you miss two.
.env files for non-secret settings (ports, feature flags) are fine. The
goal is to keep real secrets out of them, or at least out of the long-lived
copies.
Store a key in the macOS Keychain
The Keychain is encrypted, unlocked when you log in, and protected per app.
The security command, built into macOS, adds and reads entries.
Add a key (leave -w last so it asks for the value instead of saving it in
your shell history):
security add-generic-password -a "$USER" -s OPENAI_API_KEY -w
Paste the key when asked, then press Return. Read it back:
security find-generic-password -a "$USER" -s OPENAI_API_KEY -w
To change it, add -U to the first command to update the existing entry.
To delete it:
security delete-generic-password -a "$USER" -s OPENAI_API_KEY
You can also see and edit these entries in Keychain Access (in
/System/Library/CoreServices/Applications on recent macOS) under login >
Passwords.
Use the key without writing it down
For one command
OPENAI_API_KEY="$(security find-generic-password -a "$USER" -s OPENAI_API_KEY -w)" python app.py
The key exists only in that process’s environment, and nothing is written to disk.
For every terminal
If a tool expects the variable in every shell, have ~/.zshrc read it from
the Keychain instead of storing it there:
export OPENAI_API_KEY="$(security find-generic-password -a "$USER" -s OPENAI_API_KEY -w 2>/dev/null)"
Your dotfiles stay safe to share. The tradeoff: every program you start from that shell inherits the key, so prefer the one-command form for keys that can spend money.
Per project, with direnv
direnv loads variables when you cd into a folder.
Instead of a .env file, put the lookup in .envrc:
export STRIPE_SECRET_KEY="$(security find-generic-password -a "$USER" -s STRIPE_SECRET_KEY_DEV -w)"
.envrc now contains no secret and can even be committed.
Keep a master copy in a vault
The Keychain is good at handing keys to scripts. It’s poor at remembering
which key is which: the dashboard it came from, the projects that use
it, when you created it, when it expires. And a login Keychain entry made
with security stays on that Mac.
That’s what a vault is for. In Fydra, an API Key item has API Key, Secret and Service fields, and notes for the rest. A typical entry:
- Title: Stripe, live secret key
- API Key:
sk_live_… - Service: Stripe
- Notes: used by billing-service and the admin dashboard; created 2026-06-01; restricted to charges and refunds; rotate in June.
- Folder: Work
Items are encrypted with AES-256-GCM on your device and sync through your own iCloud, so you can look a key up on your iPhone while you’re in a provider’s dashboard on someone else’s computer. On a Mac, the menu bar shows your favourites, and a value copied from there is cleared from the clipboard after 90 seconds.
When you set up a new Mac, copy the keys you use from the vault into the
Keychain with security add-generic-password, and you’re done.
Rotate, scope and expire
Wherever you store keys, three habits matter more than the storage:
- Scope keys. Most providers let you create restricted keys (read-only, one project, test mode). Use those for local development.
- Set expiry dates where the provider allows it, and note them in the vault.
- Rotate after an incident. If a key ever lived in a committed
.envfile, assume it’s leaked: revoke it in the provider’s dashboard and make a new one. Deleting the commit isn’t enough. GitHub’s secret scanning catches many leaked key formats, but not all.
Summary
| Where | Good for | Watch out for |
|---|---|---|
.env file |
Non-secret settings | Plain text, travels with the folder |
~/.zshrc |
Nothing secret | Shared in dotfiles, readable by every tool |
| macOS Keychain | Keys your scripts use on this Mac | Hard to browse, stays on one Mac |
| Vault (Fydra) | Master copy, notes, other devices | Copy to the Keychain for scripts |
SSH keys are a separate story, because an SSH agent can use them without ever handing them out. See where to store SSH keys on a Mac and how to use Touch ID for SSH keys.
Frequently asked questions
Is it safe to store API keys in a .env file?
It's common, not safe. A .env file is plain text: it ends up in commits when .gitignore is wrong, in backups and synced folders, and AI coding assistants and editor extensions can read it. Keep real keys elsewhere and load them when needed.
How do I store an API key in the macOS Keychain from Terminal?
Run security add-generic-password -a "$USER" -s OPENAI_API_KEY -w and paste the key when asked. Read it back with security find-generic-password -a "$USER" -s OPENAI_API_KEY -w.
Should I put export API_KEY=... in ~/.zshrc?
No. ~/.zshrc is a plain text file that many tools read and people share in dotfiles repos. If you need the key in every shell, have ~/.zshrc read it from the Keychain instead of writing it there.
Can Fydra give API keys to my scripts?
No. Fydra has no command-line tool. It's the encrypted place where you keep and find your keys on iPhone, iPad and Mac. For scripts, put the key you use in the macOS Keychain, as shown in this guide.