Environments
An environment is a .env file at the root of your workspace. You write {{VARIABLE_NAME}} anywhere in a request, and DotFetch fills in the value from the active environment at the moment you press Send.
Switch the active environment, and the same saved request now hits a different server with different credentials, without editing it.
Create environments
Add .env files to the root folder of your workspace (subfolders aren't scanned):
my-project/
βββ .env β default
βββ .env.local β local
βββ .env.development β development
βββ .env.staging β staging
βββ .env.production β productionThe Environments sidebar shows each file with its variable count, and updates within a moment when you save a change.
How file names become environment names
| File | Environment name |
|---|---|
.env | default |
.env.local | local |
.env.development or .env.dev | development |
.env.staging or .env.stage | staging |
.env.production or .env.prod | production |
.env.test or .env.testing | test |
.env.production.local | production-local |
.env.anything | anything |
These files are ignored: names ending in .example, .sample, .template, .schema, .lock, .bak, .swp, or ~. Keep a committed .env.example without it showing up as an environment.
File format
# Comments start with #
BASE_URL=https://api.example.com
API_KEY=sk_test_123
# Quotes are optional and removed
GREETING="Hello, world"
# Escapes work inside quotes: \n \t \" \\
MULTI="line one\nline two"
# A quoted value can span lines
PRIVATE_KEY="-----BEGIN KEY-----
abc123
-----END KEY-----"
# Variables can reference other variables
API_URL={{BASE_URL}}/v2Variable names must start with a letter or _ and contain only letters, digits, and _. A line like my-key=1 is skipped.
Activate an environment
In the Environments sidebar, hover an environment and click Set as Active Environment, or right-click it. The active one shows a β and Active, and its name appears as a badge in the Request Builder (default in the top right).
To stop using variables, click No Environment (Clear active environment).
Use variables
Write {{NAME}} in the URL, params, headers, body, or auth fields:
POST {{BASE_URL}}/users/{{USER_ID}}/notes
X-API-Key: {{API_KEY}}
{ "author": "{{USER_ID}}", "text": "hi" }With .env containing BASE_URL=https://api.example.com and USER_ID=42, this sends:
POST https://api.example.com/users/42/notes
X-API-Key: sk_test_123
{ "author": "42", "text": "hi" }Things to know:
- Spaces are allowed inside the braces.
{{ USER_ID }}works the same as{{USER_ID}}. - Unknown variables stay as written. If
TOKENisn't defined,{{TOKEN}}is sent literally, which usually means a401from the server. - Nested variables resolve.
API_URL={{BASE_URL}}/v2turns into the full URL. DotFetch resolves up to 10 levels and stops if two variables refer to each other in a loop. - No environment active? If you use
{{...}}while No Environment is selected, a hint appears in the Request Builder reminding you to pick one. - Values in the URL bar are inserted as-is, not URL-encoded. If a value has spaces or
&, put the parameter in the Params tab, which encodes values (see Query parameters).
Sidebar tools
Click a variable to copy its value. Right-click it for more:
| Command | Copies |
|---|---|
| Copy Variable | The value, such as sk_test_123 |
| Copy Variable Name | API_KEY |
| Copy as {{VARIABLE}} | {{API_KEY}}, ready to paste into a request |
Right-click an environment and choose Open Environment File to edit its .env file. From the Command Palette (Ctrl+Shift+P), Search Variable and Show All Variables find a variable across every environment.
Session variables (set by scripts)
Scripts can create or change variables with dotfetch.env.set(). For example, a login request can save the token it gets back:
// Post-Response script on POST /login
dotfetch.env.set('ACCESS_TOKEN', dotfetch.response.json().token);GET {{BASE_URL}}/me
Authorization: Bearer {{ACCESS_TOKEN}} β uses the token from the login responseSession variables:
- Never touch your
.envfiles. They live in memory only. - Win over file values. If
.envhasACCESS_TOKEN=oldand a script setsnew, requests usenew. - Belong to one environment. A token set while
stagingis active isn't visible underproduction. - Last until you close the Request Builder tab. Closing it clears them all.
- Don't appear in the Environments sidebar, which shows file values only.
See dotfetch.env for the full API.