Skip to main content
coreydaley.dev

Where to store API keys on a Mac

An API key is a password that never expires on its own. Anyone who has it can spend your money or read your data until you rotate it. Yet keys usually live in the least protected places on a Mac: a .env file, a note, a spreadsheet, or a terminal’s history.

Where a key should live depends on who needs to read it: you, pasting it somewhere by hand, or a program running without you.

The usual places, and what they risk

  • .env files and config files. Plain text on disk. They get committed to Git by accident, copied into Time Machine and cloud-synced folders, and read by any process running as you. A .gitignore entry prevents one of those, not the rest.
  • Notes apps, text files, spreadsheets. Plain text again, usually synced, and easy to find by searching for words like “key” or “token”.
  • Shell history. Typing export API_KEY=sk-… writes the key into ~/.zsh_history.
  • Chat and email. Keys sent to yourself or a teammate stay in every copy of that message.

Better places

  • The macOS login keychain, through the built-in security command. Encrypted, free, already on every Mac, and readable from scripts. This is the right choice when a program needs the key. See Keep API keys out of .env files.
  • A password manager’s secure notes or custom fields. Encrypted and synced. Some, like 1Password, also have a command-line tool that can inject secrets into a program’s environment.
  • Secret Store, if you mostly copy and paste keys by hand. It keeps each key as a named secret in Apple’s end-to-end encrypted iCloud Keychain, masks it until you reveal it, and clears it from the clipboard after 90 seconds. It has no command-line tool, so scripts can’t read from it.

Keys your servers and CI use

Production and CI keys belong in the platform’s own secret store: GitHub Actions secrets, your cloud provider’s secrets manager, or a tool like HashiCorp Vault. A key on your laptop shouldn’t be the only copy, and a server shouldn’t read its keys from a file someone checked in.

If a key has already leaked

Rotate it first: create a new key, switch your code to it, then revoke the old one in the provider’s dashboard. Deleting the file or the Git commit doesn’t help once the key has been copied, and bots scan public repositories for keys within minutes of a push.

Download on the App Store

Free on Mac, iPhone and iPad. No in-app purchases, no limits, no account.