Environment Variables
Environments are .env files in the workspace root. Use them for base URLs, tokens and IDs that change between setups, and to keep secrets out of request files.
Environment filesβ
Every file in the workspace root named .env or .env.<something> is an environment: .env, .env.dev, .env.production, .env.local and so on. .env files in subfolders, links to files elsewhere, and look-alikes such as direnv's .envrc are ignored.
# .env.dev
BASE_URL=http://localhost:3000
API_TOKEN=dev-token
# .env.production
BASE_URL=https://api.example.com
API_TOKEN="prod token"
Shared variables in .envβ
.env itself (no suffix) holds what every environment shares, as in dotenv and Vite: with .env.dev selected, Mercury reads .env first and .env.dev on top, so a variable in both takes .env.dev's value. Selecting .env uses it alone.
# .env: shared by every environment
API_VERSION=v2
USER_AGENT=mercury
Choosing an environmentβ
One environment is selected at a time, with the shared .env underneath it (see above). If the selected file is deleted or renamed outside Mercury, it says so and switches to No environment.
- Pick the environment in the menu at the top right of the window. No environment turns variables off.
β Eswitches to the next environment, in file-name order. After the last one it goes to None, then starts over. Every switch, from the menu orβ E, shows a notification naming the environment you landed on.- When you open a workspace, Mercury selects the first file (alphabetically) whose name contains
.dev, or else the first file. When Mercury reopens the workspace at launch, it restores the environment you last had selected. - Environment names that contain
prodare shown in red, and names that containstagin amber (in any case:.env.PRODtoo), so it's harder to send to production by mistake. With aprodenvironment selected, the Send button turns red and reads Send Β· prod (orproduction, and so on).
File formatβ
# Full-line comments start with #
BASE_URL=https://api.example.com
USER_ID = 12345
QUOTED="two words"
SINGLE='also fine'
EQUALS=a=b
- One
KEY=VALUEper line. The line is split at the first=. - Spaces around the key and value are trimmed.
- A value wrapped in matching
"or'has the quotes removed. Inside double quotes,\nis a line break,\"a quote and\\a backslash; any other backslash is kept as it is. - Blank lines and lines starting with
#are ignored. A line with only a name (KEY, no=) defines it as empty. - A shell-style
exportbefore the name is ignored, so a file you alsosourceworks as is. So is a byte-order mark at the start of the file. - Keys are case-sensitive.
- Values are used as they are. A value that contains
{{OTHER}}is not expanded.
Using variablesβ
Write {{NAME}} in the URL, a header (including the Auth tab) or the body:
GET {{BASE_URL}}/users/{{USER_ID}}
Authorization: Bearer {{API_TOKEN}}
- Variables are substituted when you send a request or copy it as cURL. Request files always keep the
{{NAME}}placeholders. - Spaces inside the braces are allowed:
{{ NAME }}is the same as{{NAME}}. - A variable the active environment doesn't define is left as-is in headers and the body, and sent as the literal text
{{NAME}}. In the URL it stops the send instead: the response panel names the variable and where to define it, because a URL with{{NAME}}in it can only fail, usually with an error about something else (an "invalid domain name").
Seeing what's missingβ
- Postman's built-ins
{{$guid}},{{$timestamp}},{{$isoTimestamp}}and{{$randomInt}}get a fresh value at every send, so imported collections that use them work. A variable of the same name in your.envwins. - In the URL bar,
{{variables}}are coloured: violet when the selected environment defines them, amber when it doesn't. - If any variable in the request is undefined, a line under the URL bar names it and the environment it's missing from, and the tab that holds it shows a warning.
- The Params and Headers tabs show a chip for each variable they use: β if it's defined, β if it isn't. Hover a chip to see its value.
- Hover a
{{variable}}in the URL bar to see its value and which file it comes from (the selected one, or the shared.env). Hover anywhere else in the URL to see it with every variable filled in.
Creating and editing environmentsβ
From the environment menu at the top right:
- New environment⦠asks for a name (
stagingmakes.env.staging), selects it, and opens it in the editor, already listing the variables the open request uses, so you only fill in values. - Edit .env.dev⦠opens the selected environment in the same editor: a table of variables, or Bulk edit for the file as text. Save writes the file; the next request uses the new values.
Saving a value from a responseβ
Right-click a field in a JSON response (a login's access_token, the id of something you just created) and choose Save as {{access_token}} in .env.dev. Mercury writes that line into the selected environment file, replacing the variable if it's already there and leaving the other lines and comments alone, and the next request uses it. Copy value is in the same menu.
It's the same plain file either way, and you can also edit .env files in any editor. Mercury picks up changes right away: new or removed env files update the picker (your selection is kept), and edited values apply to the next request.
Keeping secrets out of Gitβ
.env*
!.env.example
Commit a .env.example with placeholder values so that others can copy it:
cp .env.example .env.dev
Troubleshootingβ
If a variable isn't substituted:
- Check that the right environment is selected at the top right.
- Check that the file is in the workspace root and its name starts with
.env. - Check the spelling. Names are case-sensitive.
Relatedβ
- Requests
- Authentication
- Import & Export: Postman and Insomnia variables become
.env.<name>files