Using Insomnia’s Hierarchical Environments to Simplify Multi‑Environment API Testing
Learn how Insomnia’s variable inheritance, secret masking, and request chaining let you manage dev, staging, and prod configurations without duplicating requests.
23 Apr 2026, 20:02 UTC

Problem: juggling multiple API environments
When you develop against the same API across dev, staging, and production, you often end up copying requests, hard‑coding URLs, or maintaining separate collections. Any change to a base endpoint or credential forces you to edit many places, increasing the chance of drift.
Thesis: Insomnia’s hierarchical environments let you define shared values once and override them per stage, while keeping secrets masked and enabling request chaining for multi‑step flows.
How variable scopes work
Insomnia treats variables as a hierarchy: workspace → environment → request. A variable defined at the workspace level is inherited by all environments unless an environment (or request) redefines it. At request time, the most specific value wins.
This means you can store a common base URL at the workspace level, then override baseUrl in each environment to point to https://api.dev.example.com, https://api.staging.example.com, or https://api.prod.example.com.
Secret handling
Variables can be marked as “secret”. In the UI they appear as masked characters (e.g., ********), but when the request is sent Insomnia substitutes the actual value. Secrets are still stored in the workspace file; encryption depends on your local setup or sync provider, so treat them as protected but not guaranteed end‑to‑end encrypted.
Worked example: auth token chaining
- Create a workspace named
API‑Project. - Add a workspace variable
baseUrlwith valuehttps://api.example.com(no environment override yet). - Add three environments:
dev,staging,prod. In each, setbaseUrlto the appropriate host (e.g.,https://api.dev.example.com). - Add a secret variable
apiKeyat the workspace level and give it a placeholder value. - Create a request
GET /userswith URL{{baseUrl}}/usersand headerAuthorization: Bearer {{apiKey}}. - Now create a second request
POST /auth/loginthat returns a JSON token. On the “Tests” tab, add a script likepm.environment.set('token', json.token);(Insomnia’s built‑in helper uses{{ }}syntax for environment variables). - Set the first request’s
Authorizationheader toBearer {{token}}and make sure it runs after the login request by dragging it into a folder and enabling “Run after previous request”. - Switch between environments in the client header; the request URL updates automatically, while the secret
apiKeyremains masked but is still substituted.
Trade‑offs and limitations
- Version sensitivity: Variable resolution syntax changed between Insomnia v7 and v8 (e.g., handling of undefined variables). Opening a workspace created in an older version may trigger a migration prompt or produce unexpected empty values.
- Hidden dependencies: Request chaining via environment variables creates implicit links. If you duplicate an environment or share a workspace, a missing chaining step can cause silent failures that are hard to trace.
- Secret storage: While secrets are masked in the UI, they are stored in plain text within the workspace JSON unless you enable an external vault or encrypted sync. Assume no guaranteed end‑to‑end encryption by default.
How to verify your setup
- Open the workspace, select the
devenvironment, and click the “Code” tab on theGET /usersrequest. Confirm the URL shows the dev host and the header contains the maskedapiKeyvalue. - Enable “View raw request” (or use a proxy like mitmproxy) to see the actual outgoing request; you should see the real
apiKeyvalue substituted. - Run the login request first, then the users request. Check that the
Authorizationheader contains the token returned from login. - Close Insomnia, re‑open the workspace, and note any migration dialog about variable syntax. If none appears, the workspace is compatible with your current version.
Actionable closing
Start by moving shared values (base URLs, common headers, non‑secret tokens) into workspace variables. Use environment variables only for values that truly differ per stage. Mark any credentials as secrets, and verify substitution with a proxy. Finally, document any chaining steps in a README so teammates know the hidden dependencies.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.