swiftapplications

Apple security for developers

How to store API keys securely on macOS

Keep API keys out of .env files on your Mac: store them in the macOS Keychain, read them only when needed, and keep them away from AI agents.

Updated 8 October 2026 3 min read

The short answer

On a Mac, store API keys in the Keychain rather than in .env files or shell profiles. The Keychain encrypts them at rest, locks them with your login, and asks before another app reads them. Read a key only at the moment a program needs it, and give it to that one process, not your whole shell.

Why .env files are the wrong place

A .env file is plain text. Any program running as you can read it: your editor’s extensions, a build script you pulled from GitHub, and now the coding agent working in that folder. It also travels: into Time Machine, into iCloud Drive if the project sits in Documents, and into Git history the one time .gitignore misses it.

Exporting keys in ~/.zshrc is worse: every program you start from a terminal inherits them, whether it needs them or not.

Put the key in the Keychain

macOS ships with the security command for the login keychain. Store a key once; ending with -w and no value makes it prompt, so the key never lands in your shell history:

Store a key (prompts for the value)
security add-generic-password -a "$USER" -s "openai-api-key" -w

Read it back only when you need it:

bash
security find-generic-password -a "$USER" -s "openai-api-key" -w

Give the key to one process, not your shell

Set the variable for a single command instead of exporting it. Only that process, and the processes it starts, can see it:

bash
OPENAI_API_KEY="$(security find-generic-password -s openai-api-key -w)" python3 summarise.py

In your own Swift apps, read the item with Keychain Services at the moment of the request and keep it out of logs, crash reports and UserDefaults. The Keychain in Swift guide has the code.

AI agents change the threat

A coding agent runs as you, reads your project, and runs commands. If a key is in a file it can read, assume it can end up in a prompt, a log or a commit. Two habits help: keep keys out of the agent’s working folder entirely, and give agents a scoped, revocable way to use a key instead of the key itself.

That second habit is what a secrets vault for agents is for: the agent asks, you decide, and the key is used without ever being written to a file the agent can read.

The safe way to do this

MoltenRock

The encrypted vault for your API keys and AI agents

Your keys sealed by your Mac’s Secure Enclave, handed only to the local agents you approve, never written to a .env file. Free for Mac.

Get MoltenRock, free

Questions developers ask

Is the macOS Keychain encrypted?

Yes. Keychain items are encrypted at rest and the login keychain is unlocked with your Mac login. Apps must be allowed before they can read an item another app created.

Should I use 1Password or the Keychain for API keys?

Both keep keys out of plain text. The Keychain is built in and scriptable with security; a password manager adds sharing and sync. What matters most is that programs read keys at run time instead of from files.

Can an AI agent read my Keychain?

Not silently. macOS asks before a program reads an item it didn’t create, unless you choose Always Allow. That prompt is worth keeping.