[GH-ISSUE #1468] Dynamic environment variables #473

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

Originally created by @pikausp on GitHub (Feb 5, 2021).
Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/1468

Hello,

is it possible to dynamically set environment variables e.g. within tests, similar to Postman? Our use-case is to automatically set authentication token after calling sign-in/refresh endpoints and not having to manually copy and set the token.

Thank you for any tips

Originally created by @pikausp on GitHub (Feb 5, 2021). Original GitHub issue: https://github.com/hoppscotch/hoppscotch/issues/1468 Hello, is it possible to dynamically set environment variables e.g. within tests, similar to Postman? Our use-case is to automatically set authentication token after calling sign-in/refresh endpoints and not having to manually copy and set the token. Thank you for any tips
kerem closed this issue 2026-03-16 15:33:52 +03:00
Author
Owner

@liyasthomas commented on GitHub (Feb 5, 2021):

Possibly a duplicate of #1425 and #984

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.

Refer: https://github.com/hoppscotch/hoppscotch/issues/1425#issuecomment-758806073

<!-- gh-comment-id:774053612 --> @liyasthomas commented on GitHub (Feb 5, 2021): Possibly a duplicate of #1425 and #984 > 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. Refer: https://github.com/hoppscotch/hoppscotch/issues/1425#issuecomment-758806073
Author
Owner

@tudordre commented on GitHub (May 11, 2023):

@liyasthomas was this developed? Wasn't able to find more info than here and this issue https://github.com/hoppscotch/hoppscotch/issues/984

<!-- gh-comment-id:1544746481 --> @tudordre commented on GitHub (May 11, 2023): @liyasthomas was this developed? Wasn't able to find more info than here and this issue https://github.com/hoppscotch/hoppscotch/issues/984
Author
Owner

@liyasthomas commented on GitHub (May 12, 2023):

Hi @tudordre, refer to our documentation to learn how to create environment variables from pre request scripts and tests: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables#setting-environment-variables

<!-- gh-comment-id:1545031739 --> @liyasthomas commented on GitHub (May 12, 2023): Hi @tudordre, refer to our documentation to learn how to create environment variables from pre request scripts and tests: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables#setting-environment-variables
Author
Owner

@Muhammad-Mostafa commented on GitHub (Jul 31, 2023):

Hi @liyasthomas

Are there any updates about whether you guys added this feature of the ability to store the auth token in the environment variables. Can i write a script to store the auth token from login response to the environment variables?

I checked out your documentation, but didn't find a related something about this. thanks in advance.

<!-- gh-comment-id:1658120404 --> @Muhammad-Mostafa commented on GitHub (Jul 31, 2023): Hi @liyasthomas Are there any updates about whether you guys added this feature of the ability to store the auth token in the environment variables. Can i write a script to store the auth token from login response to the environment variables? I checked out your documentation, but didn't find a related something about this. thanks in advance.
Author
Owner

@liyasthomas commented on GitHub (Jul 31, 2023):

Hi @Muhammad-Mostafa, you can now create, update environment variables directly from Test scripts.
Documentation: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables%23setting-environment-variables

<!-- gh-comment-id:1658132094 --> @liyasthomas commented on GitHub (Jul 31, 2023): Hi @Muhammad-Mostafa, you can now create, update environment variables directly from Test scripts. Documentation: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables%23setting-environment-variables
Author
Owner

@umardev500 commented on GitHub (Nov 16, 2023):

Hi @Muhammad-Mostafa, you can now create, update environment variables directly from Test scripts. Documentation: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables%23setting-environment-variables

how to set automatically after login

<!-- gh-comment-id:1813535546 --> @umardev500 commented on GitHub (Nov 16, 2023): > Hi @Muhammad-Mostafa, you can now create, update environment variables directly from Test scripts. Documentation: https://docs.hoppscotch.io/documentation/getting-started/rest/pre-request-scripts#setting-environment-variables%23setting-environment-variables how to set automatically after login
Author
Owner

@liyasthomas commented on GitHub (Nov 16, 2023):

Setting Environment Variables from API Responses

<!-- gh-comment-id:1813697882 --> @liyasthomas commented on GitHub (Nov 16, 2023): [Setting Environment Variables from API Responses](https://docs.hoppscotch.io/documentation/features/scripts#setting-environment-variables-from-api-responses)
Author
Owner

@Moni142002 commented on GitHub (Sep 18, 2024):

Hii
Is it possible to dynamically add a token to set an environment variable in Hoppscotch
var response = pw.response.json();
pw.env.set("token", "response.token");
I'm trying this one in a Pre-request Script but its not working as I expected
Pls suggest me some ideas....

<!-- gh-comment-id:2357700339 --> @Moni142002 commented on GitHub (Sep 18, 2024): Hii Is it possible to dynamically add a token to set an environment variable in Hoppscotch var response = pw.response.json(); pw.env.set("token", "response.token"); I'm trying this one in a Pre-request Script but its not working as I expected Pls suggest me some ideas....
Author
Owner

@liyasthomas commented on GitHub (Sep 18, 2024):

Can you remove the quotes ("") around response.token and try again?

var response = pw.response.json();
pw.env.set("token", response.token);

Can you refer to this documentation for more info: https://docs.hoppscotch.io/documentation/features/scripts#setting-environment-variables-from-api-responses

<!-- gh-comment-id:2357710991 --> @liyasthomas commented on GitHub (Sep 18, 2024): Can you remove the quotes ("") around `response.token` and try again? ```js var response = pw.response.json(); pw.env.set("token", response.token); ``` Can you refer to this documentation for more info: https://docs.hoppscotch.io/documentation/features/scripts#setting-environment-variables-from-api-responses
Author
Owner

@Moni142002 commented on GitHub (Sep 18, 2024):

@liyasthomas Tried but not working..
I've reviewed the documentation, but it only shows how to add a random value to the URL. However, I want to dynamically extract a value from the request body and set it as an environment variable

<!-- gh-comment-id:2357722613 --> @Moni142002 commented on GitHub (Sep 18, 2024): @liyasthomas Tried but not working.. I've reviewed the documentation, but it only shows how to add a random value to the URL. However, I want to dynamically extract a value from the request body and set it as an environment variable
Author
Owner

@liyasthomas commented on GitHub (Sep 18, 2024):

Pre-request scripts run before executing the request.
Post-request tests run after executing the request.

If you want to access the response body from scripts, you need to add them to the Test scripts of the request so that it'll have access to the response context.

Can you add the scripts to tests and see if it works?

<!-- gh-comment-id:2357732597 --> @liyasthomas commented on GitHub (Sep 18, 2024): Pre-request scripts run before executing the request. Post-request tests run after executing the request. If you want to access the response body from scripts, you need to add them to the Test scripts of the request so that it'll have access to the response context. Can you add the scripts to tests and see if it works?
Author
Owner

@Moni142002 commented on GitHub (Sep 18, 2024):

@liyasthomas But its not working...I want to know..Is it possible to add token value dynamically to set environment variable when i sign In or sign up in Hoppscotch..

<!-- gh-comment-id:2357749373 --> @Moni142002 commented on GitHub (Sep 18, 2024): @liyasthomas But its not working...I want to know..Is it possible to add token value dynamically to set environment variable when i sign In or sign up in Hoppscotch..
Author
Owner

@liyasthomas commented on GitHub (Sep 18, 2024):

I'm afraid that's not possible in Hoppscotch at the moment. But I'm curious, what is the use-case you're trying to achieve?

<!-- gh-comment-id:2357757911 --> @liyasthomas commented on GitHub (Sep 18, 2024): I'm afraid that's not possible in Hoppscotch at the moment. But I'm curious, what is the use-case you're trying to achieve?
Author
Owner

@Moni142002 commented on GitHub (Sep 18, 2024):

const jsonData = pw.response.body;
pw.env.set("token", jsonData.token);

This script is working using the jsonData variable
Thanks @liyasthomas..

<!-- gh-comment-id:2357840498 --> @Moni142002 commented on GitHub (Sep 18, 2024): const jsonData = pw.response.body; pw.env.set("token", jsonData.token); This script is working using the jsonData variable Thanks @liyasthomas..
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#473
No description provided.