Skip to main content

Features â€șEnvironments

Auf GitHub bearbeiten

Diese Dokumentation ist auf Englisch verfĂŒgbar.

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):

CODE
my-project/
├── .env               → default
├── .env.local         → local
├── .env.development   → development
├── .env.staging       → staging
└── .env.production    → production

The 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

FileEnvironment name
.envdefault
.env.locallocal
.env.development or .env.devdevelopment
.env.staging or .env.stagestaging
.env.production or .env.prodproduction
.env.test or .env.testingtest
.env.production.localproduction-local
.env.anythinganything

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

BASH
# 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}}/v2

Variable 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:

CODE
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:

CODE
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 TOKEN isn't defined, {{TOKEN}} is sent literally, which usually means a 401 from the server.
  • Nested variables resolve. API_URL={{BASE_URL}}/v2 turns 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).

Click a variable to copy its value. Right-click it for more:

CommandCopies
Copy VariableThe value, such as sk_test_123
Copy Variable NameAPI_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:

JS
// Post-Response script on POST /login
dotfetch.env.set('ACCESS_TOKEN', dotfetch.response.json().token);
CODE
GET {{BASE_URL}}/me
Authorization: Bearer {{ACCESS_TOKEN}}   ← uses the token from the login response

Session variables:

  • Never touch your .env files. They live in memory only.
  • Win over file values. If .env has ACCESS_TOKEN=old and a script sets new, requests use new.
  • Belong to one environment. A token set while staging is active isn't visible under production.
  • 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.

Environments · DotFetch Docs | dotsuite