Guide
How to store .env secrets in the macOS Keychain instead of plain files
A `.env.production` on disk is readable by every program you run, swept into backups and one `git add .` away from a leak. Your Mac already has an encrypted place for secrets.
By Anmol Sethi · Updated
01
Why not plain files
- Any process running as you can read them.
- They end up in backups, sync folders and zip files you send around.
- They’re one mistyped
.gitignoreaway from a public repo.
The login Keychain is encrypted, locked when you log out, and every item can require your approval before an app reads it.
02
Save a secret
security add-generic-password -a "$USER" -s web-app/STRIPE_SECRET_KEY -wWith -w last and no value, security asks for the secret, so it never lands in your shell history. Add -U to update an existing item.
03
Read it back
security find-generic-password -a "$USER" -s web-app/STRIPE_SECRET_KEY -w-w prints only the secret. The first time, macOS asks whether Terminal may use the item; Always Allow skips the prompt next time.
04
Use it when you run your app
Load the value into the environment for one command:
STRIPE_SECRET_KEY="$(security find-generic-password -a "$USER" -s web-app/STRIPE_SECRET_KEY -w)" npm startDelete it when you’re done with the project:
security delete-generic-password -a "$USER" -s web-app/STRIPE_SECRET_KEY05
Where this gets painful
One Keychain item per variable works for two or three secrets. With twenty variables across three environments, you end up writing scripts to save, load and switch them, and nothing tells you which environment you’re on.
06
The one-click way: Dotvane
Dotvane keeps each whole profile (every variable of local, staging or production) as one Keychain item. Switching writes the active profile to .env, readable by you alone, and dotvane run staging -- npm start injects it without writing anything.
Profiles named “prod” also ask for Touch ID before they’re shown, switched to or run. See how it works.
Try Dotvane free for 7 days. Every feature, no account.
Download for Mac