[GH-ISSUE #46] Set environment variable #22

Closed
opened 2026-03-16 12:17:06 +03:00 by kerem · 9 comments
Owner

Originally created by @mikheillomidze on GitHub (Jan 21, 2022).
Original GitHub issue: https://github.com/hoppscotch/docs/issues/46

How can I set the environment variable in the tests script?
I was used to setting variables in postman like this:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("TOKEN", jsonData.AuthenticationResult.IdToken);

Can I do something similar in hopscotch?

Originally created by @mikheillomidze on GitHub (Jan 21, 2022). Original GitHub issue: https://github.com/hoppscotch/docs/issues/46 How can I set the environment variable in the tests script? I was used to setting variables in postman like this: ``` var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("TOKEN", jsonData.AuthenticationResult.IdToken); ``` Can I do something similar in hopscotch?
kerem closed this issue 2026-03-16 12:17:11 +03:00
Author
Owner

@rcdmk commented on GitHub (Jan 22, 2022):

Sadly, it seems to only be available in pre-request scripts:

pw.env.set("variable_name", "value");
<!-- gh-comment-id:1019067241 --> @rcdmk commented on GitHub (Jan 22, 2022): Sadly, it seems to only be available in pre-request scripts: ```js pw.env.set("variable_name", "value"); ```
Author
Owner

@kahennig commented on GitHub (Feb 8, 2022):

Hi @mikheillomidze, yes, this was discussed here: #1425.
I am also eager to have this features. It's the last issue keeping me from migrating.

<!-- gh-comment-id:1032747404 --> @kahennig commented on GitHub (Feb 8, 2022): Hi @mikheillomidze, yes, this was discussed here: [#1425](https://github.com/hoppscotch/hoppscotch/issues/1425). I am also eager to have this features. It's the last issue keeping me from migrating.
Author
Owner

@mrteey commented on GitHub (Feb 24, 2022):

I have found a way: Add this to your test script not pre-request script

// Set an environment variable
pw.env.set("token", pw.response.body.token);

<!-- gh-comment-id:1049490500 --> @mrteey commented on GitHub (Feb 24, 2022): I have found a way: Add this to your test script not pre-request script `// Set an environment variable` `pw.env.set("token", pw.response.body.token);`
Author
Owner

@lapo-luchini commented on GitHub (Feb 24, 2022):

I have found a way

Yes! Seems to be available in the most recent release, it wasn't working like this a few days ago.

<!-- gh-comment-id:1049591070 --> @lapo-luchini commented on GitHub (Feb 24, 2022): > I have found a way Yes! Seems to be available in the most recent release, it wasn't working like this a few days ago.
Author
Owner

@liyasthomas commented on GitHub (Feb 24, 2022):

Hoppscotch now allows setting environment variables from Test scripts, hence closing.
Doc: https://docs.hoppscotch.io/quickstart/tests#envsetvariablevalue

<!-- gh-comment-id:1049598978 --> @liyasthomas commented on GitHub (Feb 24, 2022): Hoppscotch now allows setting environment variables from Test scripts, hence closing. Doc: https://docs.hoppscotch.io/quickstart/tests#envsetvariablevalue
Author
Owner

@kahennig commented on GitHub (Feb 24, 2022):

Thank you!!!

<!-- gh-comment-id:1050220493 --> @kahennig commented on GitHub (Feb 24, 2022): Thank you!!!
Author
Owner

@MendDemo commented on GitHub (Jul 18, 2022):

I'm getting below issues
image

<!-- gh-comment-id:1187381710 --> @MendDemo commented on GitHub (Jul 18, 2022): I'm getting below issues ![image](https://user-images.githubusercontent.com/40821798/179518336-ad4aa590-e1e8-4d42-a13f-390a6afe5a37.png)
Author
Owner

@kerembeyazit commented on GitHub (May 6, 2025):

Cannot find a variable called pw.response.

Image
<!-- gh-comment-id:2853794642 --> @kerembeyazit commented on GitHub (May 6, 2025): Cannot find a variable called `pw.response`. <img width="343" alt="Image" src="https://github.com/user-attachments/assets/7339024d-42bb-4eaa-88ec-d794e56f60d3" />
Author
Owner

@Layhout commented on GitHub (Jun 19, 2025):

Cannot find a variable called pw.response.

Image

try it in test tab

<!-- gh-comment-id:2986566695 --> @Layhout commented on GitHub (Jun 19, 2025): > Cannot find a variable called `pw.response`. > > <img alt="Image" width="343" src="https://private-user-images.githubusercontent.com/3125515/440705621-7339024d-42bb-4eaa-88ec-d794e56f60d3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NTAzMDc4MzIsIm5iZiI6MTc1MDMwNzUzMiwicGF0aCI6Ii8zMTI1NTE1LzQ0MDcwNTYyMS03MzM5MDI0ZC00MmJiLTRlYWEtODhlYy1kNzk0ZTU2ZjYwZDMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDYxOSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTA2MTlUMDQzMjEyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9Y2YwZjVhM2QyZDVmMTAxNTkyZjJhY2M5YzRkYTk3YjlmMDZmOTlhOGFkMjRiN2Q4NTFkNThlZjJhMjhlZDNkNyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.2igYysSVDH8UrfabtCr-61LiRF71efwAdiNHGw6ic5Q"> try it in test tab
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/docs#22
No description provided.