Using Insomnia Environment Variables to Switch API Endpoints and Tokens
Learn how to define and reuse Insomnia environment variables for dynamic URLs, headers, and scripts, plus limits and verification steps.
22 Dec 2025, 02:27 UTC

Quick answer
Insomnia lets you define environment variables such as {{baseUrl}} or {{authToken}} and reuse them in request URLs, headers, bodies, and scripts. Switching the environment dropdown updates every reference at once, so you can move between dev, staging, and production without editing each request.
Worked example
- Open the environment manager (the dropdown at the top‑right of the window) and choose Manage Environments → New Environment.
- Create an environment named
devwith two variables: baseUrl→https://api.dev.example.comauthToken→dev-token-123(toggle the Secret switch if you want the value masked in the UI).- Repeat the steps to create a
stagingenvironment with its own host and token. - Create a new GET request. In the URL field enter:
https://{{baseUrl}}/v1/resources
- Add an Authorization header:
Bearer {{authToken}}
- Select the
devenvironment from the dropdown and send the request. Insomnia expands the placeholders, producing a request tohttps://api.dev.example.com/v1/resourceswith the dev token. - Change the dropdown to
stagingand resend. The same request now points to the staging host and uses the staging token, without any further edits.
Using variables in scripts
Pre‑request and test scripts can read and write environment variables. Insomnia provides the insomnia.env object (for native scripts) and also supports the Postman‑compatible pm.environment API for users migrating collections.
// Pre‑request script – fetch a fresh token from a vault
const fresh = await getTokenFromVault(); // placeholder for your vault call
insomnia.env.set('authToken', fresh); // or pm.environment.set('authToken', fresh)
After the script runs, the {{authToken}} placeholder in the Authorization header contains the newly set value.
Limits and common mistakes
- Secret masking is UI‑only. Marking a variable as secret hides it in the editor, but the exported workspace file still stores the value in plain text unless you have an external vault integration enabled. Treat the workspace as sensitive if you rely on secret variables without a vault.
- Hard‑coded values bypass substitution. If you type a literal URL like
https://api.dev.example.cominstead of using {{baseUrl}}, changing environments will have no effect. Verify that every dynamic part uses the double‑curly syntax. - Variable naming rules. Names are case‑sensitive, must start with a letter, and may contain only letters, numbers, and underscores. Spaces, hyphens, or other symbols cause the template to stay unresolved, so the literal placeholder (e.g., {{base Url}}) is sent in the request.
- Scope confusion. Variables can be defined globally, per workspace, or per environment. A workspace‑level variable with the same name as an environment variable is overridden by the environment value when that environment is active, which can lead to unexpected behavior if you rely on the workspace value.
Practical way to check the result
- After configuring environments and the request, choose File → Export → Export Workspace.
- Open the exported JSON file in a text editor.
- Locate the
environmentsarray. You will see entries similar to:
{
"name": "dev",
"values": [
{"key": "baseUrl", "value": "https://api.dev.example.com"},
{"key": "authToken", "value": "dev-token-123", "type": "secret"}
]
}
Confirm that the values match what you entered (secret values appear as plain text here). Then, in the Insomnia UI, switch the environment dropdown and watch the request URL and headers update in the preview pane.
When to use this feature
- Moving between dev, test, and production endpoints without duplicating requests.
- Managing tokens or API keys that change frequently, especially when paired with an external vault for true encryption at rest.
- Driving data‑scenario tests where a pre‑request script sets different payloads or headers based on a variable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.