Skip to main content

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.
  • ⌘ E switches 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 prod are shown in red, and names that contain stag in amber (in any case: .env.PROD too), so it's harder to send to production by mistake. With a prod environment selected, the Send button turns red and reads Send Β· prod (or production, 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=VALUE per 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, \n is 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 export before the name is ignored, so a file you also source works 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 .env wins.
  • 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 (staging makes .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:

  1. Check that the right environment is selected at the top right.
  2. Check that the file is in the workspace root and its name starts with .env.
  3. Check the spelling. Names are case-sensitive.