Source: https://keypaste.com/developers/replace-dotenv/
Last updated: 2026-12-01

# 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.

## Prerequisites

- The `keypaste` command and a vault chosen with `keypaste use` ([Give an AI agent its first key](https://keypaste.com/developers/start/) makes one).
- A project with a `.env`.

## 1\. Import the `.env`

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/.envenv/shop/.env will be created and tagged into shop/dev.Import 3 variables into shop/dev? [y/N] yImported 3 variables into shop/dev.Deleting removes the file from the directory. It does not overwrite the blocks itused: on an SSD, on a copy-on-write filesystem, or on any volume with snapshots orbackups, 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.

## 2\. Write the reference file

Terminal window

```
keypaste env export shop > .env.keypastecat .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_URLPORT=kp://shop/dev/PORTSTRIPE_KEY=kp://shop/dev/STRIPE_KEY
```

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

## 3\. Run

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.

## 4\. Add a variable

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`.

## From the desktop app

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](https://github.com/notinferred/keypaste/blob/main/docs/desktop.md#environments)).

## Load them in every terminal

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](https://keypaste.com/developers/hook/)).

## Next steps

- [Run in CI](https://keypaste.com/developers/run-in-ci/)
- [keypaste hook](https://keypaste.com/developers/hook/): every shell, what it never loads, and switching stages
- [keypaste env](https://keypaste.com/developers/env/): stages, comparing them, exporting a plain `.env`, and how keypaste reads one
- [keypaste run](https://keypaste.com/developers/run/) and [kp:// references](https://keypaste.com/developers/references/)
