Guide
Using Hoppscotch Environment Variables and Pre‑request Scripts to Build Dynamic API Requests
Learn how to combine Hoppscotch’s global, workspace, and environment variables with a pre‑request script to build dynamic API requests and verify the results.
Published by Tasadduq Burney
19 Jan 2026, 01:22 UTC
3 min100.8K views0

Desired outcome
Create a request in Hoppscotch that automatically resolves a base URL, API version, and endpoint from variables, and injects a dynamically generated token via a pre‑request script.
Prerequisites
- Hoppscotch installed (web or desktop) and a user account with edit rights.
- Basic familiarity with the Hoppscotch UI (request builder, environments panel).
- No external tools required; all actions are performed inside Hoppscotch.
Procedure
- Set up variable scopes
- Open Hoppscotch and create a new workspace (or use an existing one).
- Navigate to
Environments → Globaland add a variable: - Name:
baseUrl - Value:
https://api.example.com - Go to
Environments → Workspace(select the current workspace) and add: - Name:
version - Value:
v1 - Select or create an environment (e.g.,
dev) and add: - Name:
endpoint - Value:
/users
- Build the request URL using variable substitution
- Create a new request (GET) and enter the URL:
- Hover over the URL field; Hoppscotch will show the resolved value as a tooltip (expected:
https://api.example.com/v1/users).
{{baseUrl}}/{{version}}{{endpoint}} - Add a pre‑request script to inject a token
- Click the
Pre‑request Scripttab for the request. - Paste the following JavaScript (uses Hoppscotch’s
pmAPI): - In the
Headerssection, add: - Key:
Authorization - Value:
Bearer {{token}} - Send the request.
- After sending, open the
Request Detailspane (or console) and verify that theAuthorizationheader containsBearer abc123.
// Generate a simple static token for demonstration pm.environment.set('token', 'abc123'); - Click the
- Export and import the environment (optional)
- With the
devenvironment selected, clickExportto download a JSON file. - Delete the
endpointvariable from the UI to simulate loss. - Click
Import, choose the exported JSON, and confirm. - Check that
endpointreappears with its original value (/users). Note that after import Hoppscotch treats all variables as environment‑level; you may need to movebaseUrlandversionback to Global/Workspace scopes if desired.
- With the
Expected checks
- URL resolution: confirm the tooltip or request preview shows
https://api.example.com/v1/users. - Header injection: verify the
Authorizationheader equalsBearer abc123in the sent request details. - Environment import: after re‑import, the
endpointvariable exists with value/users.
Recovery options (rollback)
If the imported environment overwrites needed variables, you can:
- Re‑import a previously saved JSON that contains the correct scope distribution.
- Manually delete or edit the offending variable via the Environments panel.
- Because Hoppscotch does not automatically version environments, keep a backup JSON file outside the app for safety.
Limitations
- Variable substitution occurs after pre‑request scripts; changes made in the test script (after response) do not affect the current request.
- If a variable name exists in multiple scopes, the most specific scope (environment) overrides global and workspace values, which can cause unexpected behavior if a global variable is unintentionally shadowed.
- Exported environment JSON does not retain scope information; upon import all variables are treated as environment‑level unless you move them manually.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.