Skip to content

Use your vault's env in a project

Your project’s variables move from .env into the vault. The repository keeps only references, and keypaste run fills them in when your program starts.

From the project’s folder:

Terminal window
keypaste env pull shop
Master password:
shop/dev: 3 new, 0 updated, 0 unchanged
new DATABASE_URL, PORT, STRIPE_KEY on env/shop/.env
env/shop/.env will be created and tagged into shop/dev.
Import 3 variables into shop/dev? [y/N] y
Imported 3 variables into shop/dev.
Deleting removes the file from the directory. It does not overwrite the blocks it
used: on an SSD, on a copy-on-write filesystem, or on any volume with snapshots or
backups, the old contents can outlive the file. If these values may have leaked, rotate them.
Delete '/repo/shop/.env'? [y/N] y

shop names the environment, the vault’s set of this project’s variables, and dev is its stage. keypaste lists names, never values, and writes nothing if a line is malformed. Deleting doesn’t remove the file from git history either: if git log -- .env prints anything, treat those values as leaked.

Terminal window
keypaste env export shop > .env.keypaste
cat .env.keypaste
✓ wrote 3 references · 0 values · safe to commit
# keypaste references: safe to commit, no value is stored here.
# `keypaste run --env-file .env.keypaste -- <command>` resolves them.
DATABASE_URL=kp://shop/dev/DATABASE_URL
PORT=kp://shop/dev/PORT
STRIPE_KEY=kp://shop/dev/STRIPE_KEY

Commit .env.keypaste, and keep .env in .gitignore.

Terminal window
keypaste run --env-file .env.keypaste -- npm start
keypaste run: resolving .env.keypaste → environment shop stage dev

Everything after -- is your command. It gets the variables in its environment, and no file holds them. While the desktop app or keypaste approve holds the vault, it asks there instead of for your master password, every time.

Terminal window
keypaste env set shop SENTRY_DSN
Value for SENTRY_DSN:
Set SENTRY_DSN on env/shop/.env

It asks for the value, so it stays out of your shell history. Export the reference file again to add it there.

In the app, the stage’s last row adds one: a Name and a Value. The Name turns into capitals as you type, so sentry dsn becomes SENTRY_DSN. In the app, a lower-case name such as api_url comes in by pasting NAME=value lines or importing a .env.

Environments does steps 1 and 2 in one. Its + › Import .env… brings a .env from any folder into a new environment, and an environment’s ⋯ › Import .env… into that one, with a Stage menu. Then Import and keep the file saves the values and leaves the file as it is, or Import and convert to references saves them, writes .env.keypaste, adds .env to .gitignore and removes .env if it hasn’t changed since keypaste read it (desktop guide).

Instead of keypaste run for each command, the shell hook loads the stage into your terminal when you cd into the folder and unloads it when you leave. Choose the environment’s folder in the app, add the line keypaste hook zsh (or bash, fish, pwsh) prints to your shell’s startup file, and allow each terminal once (keypaste hook).