Change environment scope in pre request or test scripts

I have created multiple environments, I would like to access the variables in a specific environment based on the response body of the request in the current environment.
Is this possible?

I have had no response so my assumption that is not currently possible. Logged feature request:

Hi @leonvz,

Welcome to the Community :wave:!

First, thank you for opening a feature-request. It helps provide visibility and gauge user interest. So just wanted to say we appreciate your feedback and involvement!

Regarding your inquiry, you’re right as far as I know there isn’t a way to do so; however, you should be able to accomplish by making use of pm.sendRequest and the Postman API. So you’d use pm.sendRequest to fetch the relevant Environment and save it to a local variable in your script and use it accordingly.

More information here:

1 Like

Thanks Sabri , this suggestion is certainly plausible, Are there any security considerations when using this method ? My “project” seems to be public ?

As long as you’re not exposing your API key. You’re forming the request in your scripts to with pm.sendRequest and fetching the Environment from the Postman API. Additionally, given you’re setting the relevant value from your Environment to a local variable, it doesn’t persist between sessions. As mentioned here: https://learning.postman.com/docs/sending-requests/variables/#defining-local-variables

For example, if you need to process a temporary test value for a single request or collection run locally, and don’t want the value to sync with your team or remain available when the request / collection has finished running, you can use a local variable.

There’s a blog post about this here: https://blog.postman.com/send-asynchronous-requests-with-postmans-pm-api/ as well as a Template that you can check out here: https://documenter.getpostman.com/view/1559645/RWgtScW3#b1b45d19-e877-474c-b013-43e5dabdbf5f

1 Like

This solves my requirement in the short term, with some workarounds as I cannot connect to the Postman API while connected to the Customer’s network.
I would still appreciate it if the Postman team considers making this feature available locally. Thanks for all your input.:ok_hand: