How to Give Coding Agents Environment Variables Without a .env File
Say you ask Claude Code, Codex, or Cursor to run npm test. The tests need a database URL and a Stripe test key. If those values are already in Buddy, you can run the tests without putting either value in a local .env file:
bdy var inject --environment dev-agent -p shop-api --var DATABASE_URL --var STRIPE_SECRET_KEY -- npm test
$Here, bdy var inject gives npm test and its child processes the two variables from dev-agent. If a test prints either encrypted value, the CLI masks it in the output by default. Use test credentials here. An agent with permission to read decrypted values can still access them.
What you need
Set up Buddy CLI for your workspace and check that you’re running version 1.26 or later.
1. Prepare an Environment for the agent
In Environments, create a dedicated dev-agent environment for the shop-api project. Add encrypted test values for DATABASE_URL and STRIPE_SECRET_KEY. Use --var to pass only the variables each command needs.
If dev-agent inherits from another Environment, check the list below before running your tests. When both Environments define the same variable, dev-agent wins.
bdy var list --environment dev-agent -p shop-api
$-pis optional if the directory is linked to a project orBUDDY_PROJECTis set.--scope PROJECTlimits the Environment lookup to the project.--varselects the variables to pass. Without this flag, the command gets all active VAR variables, including inherited ones.
2. Give the CLI access to the test credentials
In Environments, open dev-agent. In Settings, check Allow reading decrypted values of masked variables. This requires the Manage permission.
Image loading...
Create a Personal API access token (PAT) with these permissions:
- Environments: Read
- Variables: Read
- Decrypted environment variables: Allowed
Image loading...
Leave the other permissions at No access and set a short expiration date. The bdy process needs access to BUDDY_TOKEN. If you launch the agent from a terminal, set the token in that shell before starting the agent:
export BUDDY_TOKEN='<PAT>'
$For an IDE agent, configure the variable in the environment the IDE uses to launch the agent. Exporting it in a separate shell does not change the environment of a running agent. The wrapped command also inherits BUDDY_TOKEN. Output masking covers the encrypted values injected by Buddy.
If the command fails before the tests start, check the CLI error for a missing Environment setting or token scope. The Manage permission is required to enable decryption in the Environment settings, not to run bdy var inject. Plain variables selected with --var require neither the decryption setting nor the MASKED_VARIABLE_VALUE_DECRYPTED scope.
3. Tell the agent how to run tests and migrations
In CLAUDE.md or AGENTS.md, list the commands that should use the variables:
## Running commands that need configuration
Use Buddy environment variables for tests and migrations instead of a local .env:
- tests: `bdy var inject --environment dev-agent -p shop-api --var DATABASE_URL --var STRIPE_SECRET_KEY -- npm test`
- migrations: `bdy var inject --environment dev-agent -p shop-api --var DATABASE_URL -- npm run migrate`
Put bdy var inject on the test or migration command. If you wrap claude itself, the agent gets those variables for its entire session. Any command that receives a secret can save or send it. The agent can also still read an existing .env file, even if Git ignores it.
4. Check output masking
To check masking, print the two test values. The CLI filters stdout and stderr, so the lines below should come back with *** in place of each value.
bdy var inject --environment dev-agent -p shop-api --var DATABASE_URL --var STRIPE_SECRET_KEY -- sh -c 'echo "DATABASE_URL=$DATABASE_URL"; echo "STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY"'
$DATABASE_URL=***
STRIPE_SECRET_KEY=***
Image loading...
- Plain variables are not masked.
- Secrets shorter than 6 bytes are not masked. The CLI shows a warning with their names.
- Multiline secrets are masked as a whole, and each line of 8 bytes or more is also masked separately.
For interactive programs that need direct terminal access, use --no-masking from your terminal. Encrypted values can then appear in the output.
The CLI returns the command's exit code. With masking enabled, if a background process still holds the output stream open after the command ends, bdy returns code 1 after the default 300-second wait. --wait <seconds> changes the limit, and --wait 0 turns it off.
5. Use an SSH key stored in Buddy
For commands that need a file on disk, use --var-file KEY:PATH. For example, add an SSH_KEY variable named DEPLOY_KEY to dev-agent and use it with the command below. Using --var-file without --var also passes all active VAR variables. Add --var to select only the variables the command needs.
bdy var inject --environment dev-agent -p shop-api --var-file DEPLOY_KEY:./deploy_key -- ssh -i ./deploy_key deploy@staging.example.com
$The CLI creates a new file with 600 permissions and deletes it when the command exits, including on failure. It does not overwrite an existing file. The password and passphrase, if set, go to DEPLOY_KEY_PASSWORD and DEPLOY_KEY_PASSPHRASE and follow the same masking rules.
--var accepts only variables of type VAR. --var-file accepts only file types.
The file is on disk while the command runs.
The agent can read or copy it. If the bdy process is killed with kill -9, the file can stay on disk.
See also:
- CLI: bdy variable inject - flags and usage examples
- Create an Environment - creating an Environment and inheriting configuration
- Manage Environments - reading encrypted values
- CLI: getting started - installation and login
Jarek Dylewski
Customer Support
A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.