Inject API keys into any command with keyfold run

Any POSIX shell can give one command its own environment variable: write the assignment in front of it, as in STRIPE_KEY=sk_test_… npm start, and only that process and its children get it. The catch is where the value sat before you typed it: in your shell history, in a .env file, or in an export line that every other program also sees. keyfold run does the same job without the value ever touching your shell. It asks the Keyfold app for one environment's keys, waits for you to approve, and starts your command with those keys set.

This tutorial covers both: the plain shell methods and what each costs, then keyfold run step by step, with the approval sheet, one-hour grants, error messages and exit codes. The terminal output below is from the real keyfold binary.

How do you set an environment variable for one command?

There are three common ways, and they all work without any extra tool.

A prefix assignment. The Bash manual puts it precisely: assignments before a simple command "are part of that command's environment for as long as it executes", and "affect only the environment seen by that command." zsh behaves the same way.

Terminal
$ STRIPE_KEY=sk_test_example npm start

env. The same idea as a separate program. env -i starts the command with only the variables you list, which is handy for testing what a program needs.

Terminal
$ env STRIPE_KEY=sk_test_example npm start
$ env -i PATH="$PATH" STRIPE_KEY=sk_test_example node server.js

A file the runtime reads. Node.js 20.6 and later can load a file itself with --env-file. The Node docs note that "If the same variable is defined in the environment and in the file, the value from the environment takes precedence."

Terminal
$ node --env-file=.env server.js

Each of these scopes the key to one command, which is the right instinct. What they don't fix is where the value lives before and after:

Method Where the value sits Who else can reach it
export in ~/.zshrc Plain text in your shell profile Every program you start from any shell, including editors and AI agents
KEY=… command Your shell history, unless you prevent it Anyone or anything that reads the history file
.env plus --env-file A plain file in the project Anything that can read the folder, and git if the ignore rule slips
keyfold run The encrypted vault Only the command you approved, while it runs
A table comparing export in your profile, a prefix assignment and keyfold run: which keeps the key to one command, out of shell history and off the disk, which asks you first, and which works with nobody at the screen
They differ in where the key waits, and in who has to be there.

How does keyfold run work?

The keyfold command is inside the Mac app, at /Applications/Keyfold.app/Contents/MacOS/keyfold. To use it by name, open Settings › General › Command line and MCP in Keyfold and click Install, which links /usr/local/bin/keyfold to it (macOS may ask for an administrator password). The setup notes cover the CLI and the MCP server. The command's help says what it needs:

Terminal
$ keyfold run --help
Run a command with the environment's secrets injected as variables
 
Usage: keyfold run --project <PROJECT> --environment <ENVIRONMENT> -- <COMMAND>...
 
Arguments:
  <COMMAND>...  The command and its arguments, after `--`
 
Options:
  -p, --project <PROJECT>          
  -e, --environment <ENVIRONMENT>  
  -h, --help                       Print help

A typical run:

Terminal
$ keyfold run -p acme-api -e development -- npm start

Here is what happens, in order:

  1. keyfold asks the running Keyfold app for every key in acme-api / development, over a local socket that only your user account can open. Project and environment names are matched without regard to case.
  2. Keyfold shows an approval sheet and waits. Nothing is sent until you click.
  3. When you allow it, keyfold starts npm start directly, not through a shell. The command inherits your normal environment, plus one variable per key, named as the key is named in Keyfold. A key with the same name as a variable you already have replaces it for this command.
  4. Standard input, output and error stay connected to your terminal, so dev servers, prompts and colours work as usual.
  5. When the command exits, keyfold exits with the same code.
You type keyfold run in a terminal; the Keyfold window asks whether keyfold-cli may read every secret in acme-api / development to run npm start; after Allow once, npm start runs with STRIPE_KEY set, and its exit code comes back to your shell
The key goes from the vault to npm start, and nowhere else.

What does the approval sheet show?

For a CLI request, the sheet is titled "The Keyfold CLI is asking". It names the client and its process id, says it wants to "read every secret in acme-api / development", and shows the path of the keyfold binary. Then it says what the keys are for: "It will run this command with them:", followed by npm start.

A warning follows: "This hands every value in acme-api / development to that process. Approve only if you started this command." A last line spells out the grant: "Allow for 1 hour also lets keyfold-cli read acme-api / development for any other command until the hour is up or you lock Keyfold." Then you choose Deny, Allow for 1 hour or Allow once.

The command comes from the client, like its name. Keyfold's own CLI runs exactly the command it reports, so check that it's the one you typed. The process id is your other cue: if a sheet appears when you didn't just press Enter on a keyfold command, deny it.

See the approval sheet in the live demoOpens the live demo, with made-up keys

The demo shows an AI agent's request rather than the CLI's, but the sheet and its three answers are the same.

When should you choose Allow for 1 hour?

A grant saves clicks in a loop. If you're running keyfold run -p acme-api -e development -- npm test every few minutes, "Allow for 1 hour" means Keyfold answers the next requests itself until the hour is up.

Know exactly what it covers. A grant is tied to the client name, the project and the environment. Every keyfold command reports itself as keyfold-cli, so the grant covers any CLI request for acme-api / development from any program running as you: a different command, keyfold env --stdout, or an AI agent that types keyfold run into its own terminal. None of those later requests shows a sheet. Locking Keyfold ends the grant: while the vault is locked every request is refused, and after you unlock, the next request asks again.

Timeline: at 0:00 you choose Allow for 1 hour; at 0:10 another command runs with no new sheet; at 0:20 you lock Keyfold and the grant ends; at 0:25 you unlock and the next request asks again
Locking ends a grant before its hour is up.

A grant for names only (from keyfold names) doesn't cover values, so it never lets a later keyfold run through.

What happens when Keyfold is locked, closed or says no?

keyfold prints one line and exits with code 1 before your command starts. These are the real messages:

Terminal
$ keyfold run -p acme-api -e development -- npm start
Error: Keyfold is not running. Open the app and try again.
 
$ keyfold run -p acme-api -e development -- npm start
Error: Keyfold is locked. Unlock it in the app and try again.
 
$ keyfold run -p acme-api -e development -- npm start
Error: denied in Keyfold
 
$ keyfold run -p acme -e development -- npm start
Error: no project named 'acme'. Projects: acme-api, side-project

If nobody answers the sheet, the request times out after three minutes. When Keyfold is locked, it also brings its window forward so you can unlock it and run the command again.

Does keyfold run pass the exit code through?

Yes. Whatever code your command exits with, keyfold exits with too, so scripts and && chains behave as they would without it:

Terminal
$ keyfold run -p acme-api -e development -- sh -c 'exit 3'
$ echo $?
3

Two edge cases: a command killed by a signal makes keyfold exit with 1, and so does any error before the command starts. If you need to tell those apart in a script, the Error: line on standard error is from keyfold.

Why does $STRIPE_KEY come out empty?

Because your shell expands $STRIPE_KEY before keyfold runs, while the variable is still empty. keyfold run starts the command directly, without a shell, so it never sees the $. When the command line itself needs the injected values, or needs pipes, && or redirects, wrap it in sh -c with single quotes:

Terminal
$ keyfold run -p acme-api -e development -- sh -c 'npm run build && npm start'
$ keyfold run -p acme-api -e development -- sh -c 'curl -H "Authorization: Bearer $STRIPE_KEY" https://api.stripe.com/v1/balance'

The post on Claude Code and API keys shows the empty-variable trap in more detail.

How do you use it with Docker Compose, Python or make?

Anything that reads environment variables works. A few patterns:

Terminal
$ keyfold run -p acme-api -e development -- python manage.py runserver
$ keyfold run -p acme-api -e development -- make deploy-staging
$ keyfold run -p acme-api -e development -- docker compose up

For Compose, the containers only get variables you pass to them. Docker's docs show that naming a variable without a value takes it from the shell Compose runs in, which is the environment keyfold run set:

yaml
services:
  web:
    environment:
      - STRIPE_KEY
      - DATABASE_URL

What it isn't for is CI. keyfold run needs the Keyfold app open and unlocked and a person to click. On a build server, use your CI system's own secret store.

Why is this better than export in your shell profile?

An export STRIPE_KEY=… line in ~/.zshrc hands the key to every program you start from every terminal: your editor, its extensions, an AI coding agent, every postinstall script. 1Password's CLI docs make the same point about environment variables generally: "processes on your computer can access the environment of other processes run by the same user."

With keyfold run:

  • the key exists in one command's environment, only while it runs;
  • the value never appears in your shell history, because you typed a name, not a key;
  • a locked vault means no keys reach anything;
  • you rotate a key once in Keyfold, and every project that runs through it picks up the new value.

One limit to be honest about: the command you approve, and everything it starts, does get the keys. A malicious dependency inside npm start can read them like any other variable. Keyfold narrows who gets a key; it can't vouch for the code you run.

Can an AI agent use keyfold run?

It can, with two things to know. A coding agent that runs keyfold run in its own terminal shows up as keyfold-cli, exactly like you, so an open one-hour grant lets it through without a sheet. And if Claude Code's sandbox is on, macOS blocks the agent's commands from reaching Keyfold's socket until you allow it, as the post on Claude Code's permissions explains.

The other route is Keyfold's MCP server, whose keyfold_run tool runs a command through sh -c with the keys injected and returns the exit code and up to 20 KB of output to the agent. No Keyfold tool returns a secret value, but a command you let run can still print one. Why Keyfold's MCP server has no reveal tool explains that design.

FAQ

Does keyfold run write a .env file?

No. It passes keys to the command's environment and writes nothing to disk. If a tool insists on a file, keyfold env -p acme-api -e development writes ./.env (or --stdout prints it), and you delete the file afterwards.

Does the command still see my other environment variables?

Yes. It inherits your shell's environment, and the keys from the Keyfold environment are added on top. A key with the same name as an existing variable replaces it for that command only.

Does keyfold run work on Windows?

The CLI is part of the Keyfold app, so it arrives with the Windows app, which is coming through the Microsoft Store. Today it runs on macOS 12 or later: get the Mac app from the download page, then click Install in Settings › General › Command line and MCP to put keyfold on your PATH.

Can I use keyfold run in CI or on a server?

No. Each request needs the desktop app unlocked and a click, or an active grant. Use your CI provider's secret store for builds and deploys.

Can I try it before installing anything?

The live demo runs Keyfold's real interface with made-up keys, including the approval sheet, and the features page shows how projects and environments are organised.