[GH-ISSUE #1425] Environment vars not working #459

Closed
opened 2026-03-16 15:26:14 +03:00 by kerem · 9 comments
Owner

Originally created by @cddm13 on GitHub (Jan 12, 2021).
Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/1425

Describe the bug
Fails to set a value to env var when using an endpoint.

To Reproduce
Steps to reproduce the behavior:

  1. Create a new environment "Development"
  2. Create a new env var called "bearerToken" and set initial value as "initial"
  3. In the Test tab of the request pw.env.set("bearerToken", "aNewToken"); I've also tried with pw.environment.set("bearerToken", "aNewToken") and the same for the pre-request scrip, however when I check the env var bearerToken it still contains the "initial" value.

Expected behavior
When the request is completed the bearerToken environment variable value should be changed.

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS Mojave
  • Browser Chrome
  • Version 87

Additional context
am I doing anything wrong? how can I accomplish this?

Originally created by @cddm13 on GitHub (Jan 12, 2021). Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/1425 **Describe the bug** Fails to set a value to env var when using an endpoint. **To Reproduce** Steps to reproduce the behavior: 1. Create a new environment "Development" 2. Create a new env var called "bearerToken" and set initial value as "initial" 3. In the Test tab of the request `pw.env.set("bearerToken", "aNewToken");` I've also tried with `pw.environment.set("bearerToken", "aNewToken")` and the same for the pre-request scrip, however when I check the env var bearerToken it still contains the "initial" value. **Expected behavior** When the request is completed the bearerToken environment variable value should be changed. **Screenshots** ![image](https://user-images.githubusercontent.com/76781996/104330494-caf44600-54cc-11eb-915b-c3276f31246a.png) **Desktop (please complete the following information):** - OS: MacOS Mojave - Browser Chrome - Version 87 **Additional context** am I doing anything wrong? how can I accomplish this?
kerem 2026-03-16 15:26:14 +03:00
Author
Owner

@liyasthomas commented on GitHub (Jan 12, 2021):

  1. In the Test tab of the request pw.env.set("bearerToken", "aNewToken");

You cannot set an environment variable from Tests script - this feature is not available. Tests are for verifying your API responses only - read how.

To add environment variables you can either write a Pre-request script or use "Environments" tab in the sidebar.


Add environment variable using pre-request script:

Use pw.env.set("env_variable","env_value")

Example:

pw.env.set('bearerToken', 'initial value');

Once an environment variable is set, you need to switch environments from "Environments" tab to change it's value. For making use of multiple environments - you can create an environment with name "Globals" whose variables are accessible on every environments.


Expected behavior
When the request is completed the bearerToken environment variable value should be changed.

This is not the expected behavior.

Let me know if you've any doubts.

<!-- gh-comment-id:758727074 --> @liyasthomas commented on GitHub (Jan 12, 2021): > 3. In the Test tab of the request pw.env.set("bearerToken", "aNewToken"); You cannot set an environment variable from `Tests` script - this feature is not available. Tests are for verifying your API responses only - [read how](https://github.com/hoppscotch/hoppscotch/wiki/Post-Request-Tests). To add environment variables you can either write a [`Pre-request script`](https://github.com/hoppscotch/hoppscotch/wiki/Pre-Request-Scripts) or use "Environments" tab in the sidebar. --- #### **Add environment variable using pre-request script:** Use `pw.env.set("env_variable","env_value")` Example: ``` pw.env.set('bearerToken', 'initial value'); ``` Once an environment variable is set, you need to switch environments from "Environments" tab to change it's value. For making use of multiple environments - you can create an environment with name "Globals" whose variables are accessible on every environments. --- > **Expected behavior** > When the request is completed the bearerToken environment variable value should be changed. This is not the expected behavior. Let me know if you've any doubts.
Author
Owner

@cddm13 commented on GitHub (Jan 12, 2021):

I did what you said but the env variable is not changing its value, here's my pre-request code:
pw.env.set('bearerToken', 'newToken');

and here is my environment/ variable configuration:
image

as you can see the value of bearerToken is not changing to "newToken"

also, how can I set my environment variable "bearerToken" to = a property of the response (data.token) after this request is completed? because what I want is perform login and then assign that token value into my "barierToken" env that is used in another endpoint on which I need to be authenticated

<!-- gh-comment-id:758802917 --> @cddm13 commented on GitHub (Jan 12, 2021): I did what you said but the env variable is not changing its value, here's my pre-request code: `pw.env.set('bearerToken', 'newToken');` and here is my environment/ variable configuration: ![image](https://user-images.githubusercontent.com/76781996/104347912-ea489e80-54df-11eb-82e9-6f6839dbd7f2.png) as you can see the value of bearerToken is not changing to "newToken" also, how can I set my environment variable "bearerToken" to = a property of the response (data.token) after this request is completed? because what I want is perform login and then assign that token value into my "barierToken" env that is used in another endpoint on which I need to be authenticated
Author
Owner

@liyasthomas commented on GitHub (Jan 12, 2021):

Since this involves making two requests, adding token from first response to environment should be a manual process. Also you can make use of Authentication section to mention Bearer token after fetching the token first.

As of now, environment variables can't be changed from Tests script.

<!-- gh-comment-id:758806073 --> @liyasthomas commented on GitHub (Jan 12, 2021): Since this involves making two requests, adding token from first response to environment should be a manual process. Also you can make use of Authentication section to mention Bearer token after fetching the token first. As of now, environment variables can't be changed from Tests script.
Author
Owner

@skelet00r commented on GitHub (Jun 29, 2021):

@liyasthomas This is a very useful feature when sharing collections to setup flows that other users can easily run through steps. Without manually having to set all the little bits up.

I get that the tests tab is for asserting tests but one of the main use cases ive seen this is for exactly what the OP is asking for.
Its very much a blocker for me as an example wanting to migrate away from postman.

This doesn't stop at just bearer tokens for example, think getting a USERID from a login the subsequent request may require.

Is the status of this ticket WONTFIX or up for debate? If the feature is a PR away then happy to have a go at it.

<!-- gh-comment-id:870454841 --> @skelet00r commented on GitHub (Jun 29, 2021): @liyasthomas This is a very useful feature when sharing collections to setup flows that other users can easily run through steps. Without manually having to set all the little bits up. I get that the tests tab is for asserting tests but one of the main use cases ive seen this is for exactly what the OP is asking for. Its very much a blocker for me as an example wanting to migrate away from postman. This doesn't stop at just bearer tokens for example, think getting a USERID from a login the subsequent request may require. Is the status of this ticket WONTFIX or up for debate? If the feature is a PR away then happy to have a go at it.
Author
Owner

@liyasthomas commented on GitHub (Jun 29, 2021):

This is one of our planned features. The already existing Hoppscotch APIs comes little short for all this features and some security concerns. We're working on an isolated JavaScript compiler along with a set of revamped Hoppscotch APIs that allows users to add/edit/delete environment variables from both pre-request and post-request scripts.

This first iteration of the MVP is expected to arrive in a week or so. Thank you for the patience, feel free to jump into our communication channels to connect with the core team. Feedbacks are welcome.

<!-- gh-comment-id:870468352 --> @liyasthomas commented on GitHub (Jun 29, 2021): This is one of our planned features. The already existing Hoppscotch APIs comes little short for all this features and some security concerns. We're working on an isolated JavaScript compiler along with a set of revamped Hoppscotch APIs that allows users to add/edit/delete environment variables from both pre-request and post-request scripts. This first iteration of the MVP is expected to arrive in a week or so. Thank you for the patience, feel free to [jump into our communication channels to connect with the core team](https://github.com/hoppscotch/hoppscotch#contact). Feedbacks are welcome.
Author
Owner

@kahennig commented on GitHub (Dec 15, 2021):

This is one of our planned features. The already existing Hoppscotch APIs comes little short for all this features and some security concerns. We're working on an isolated JavaScript compiler along with a set of revamped Hoppscotch APIs that allows users to add/edit/delete environment variables from both pre-request and post-request scripts.

This first iteration of the MVP is expected to arrive in a week or so. Thank you for the patience, feel free to jump into our communication channels to connect with the core team. Feedbacks are welcome.

Hi liyasthomas,
this would be a great feature to have, I was wondering if the setting of env vars from the post-request scripts is nearing arrival?
Thanks,
Andi

<!-- gh-comment-id:995203643 --> @kahennig commented on GitHub (Dec 15, 2021): > This is one of our planned features. The already existing Hoppscotch APIs comes little short for all this features and some security concerns. We're working on an isolated JavaScript compiler along with a set of revamped Hoppscotch APIs that allows users to add/edit/delete environment variables from both pre-request and post-request scripts. > > This first iteration of the MVP is expected to arrive in a week or so. Thank you for the patience, feel free to [jump into our communication channels to connect with the core team](https://github.com/hoppscotch/hoppscotch#contact). Feedbacks are welcome. Hi liyasthomas, this would be a great feature to have, I was wondering if the setting of env vars from the post-request scripts is nearing arrival? Thanks, Andi
Author
Owner

@sergeidyga commented on GitHub (Jan 11, 2022):

+1. Hi! I also find this feature to be widely used. And it looks like a blocker for our team to migrate from Postman to Hoppscotch.

<!-- gh-comment-id:1010353276 --> @sergeidyga commented on GitHub (Jan 11, 2022): +1. Hi! I also find this feature to be widely used. And it looks like a blocker for our team to migrate from Postman to Hoppscotch.
Author
Owner

@xairoo commented on GitHub (Jan 23, 2022):

+1. Hi! I also find this feature to be widely used. And it looks like a blocker for our team to migrate from Postman to Hoppscotch.

100% agree. Makes things so easy.
Also missing console.log output for some debugging when writing tests or pre-request scripts.

<!-- gh-comment-id:1019497906 --> @xairoo commented on GitHub (Jan 23, 2022): > +1. Hi! I also find this feature to be widely used. And it looks like a blocker for our team to migrate from Postman to Hoppscotch. 100% agree. Makes things so easy. Also missing `console.log` output for some debugging when writing tests or pre-request scripts.
Author
Owner

@ramunozp commented on GitHub (Apr 8, 2025):

It's 2025 and it still doesn't work...

<!-- gh-comment-id:2786620451 --> @ramunozp commented on GitHub (Apr 8, 2025): It's 2025 and it still doesn't work...
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/hoppscotch#459
No description provided.