I am working on a POSTMAN collection. Say, I have two separate postman environments with each having URL variables, lets domain1 & domain2. In my initial script in pre-request tab I want to get a list of all the environments available so I can switch them when I need to. How do I get the list of environments? Thanks,
Asked
Active
Viewed 536 times
2 Answers
1
Thanks Christian Bauman. I was able to accomplish by doing following In postman Pre-request Script tab. The response will contain environment array with object having id, name, owner, uid properties. you can then call by id to get further details of an environment.
let options = {
method: 'GET',
url: 'https://api.getpostman.com/environments',
header: {
'x-api-key': 'PMAK-your own key goes here'
},
json: true
};
let envs = [];
pm.sendRequest(options, function(err, response) {
if (!err) {
let data = response.json();
_.forEach(data.environments, function(item) {
envs.push(item);
});
console.log(envs);
} else {
console.log(err);
}
});

Maharaj
- 313
- 2
- 3
- 14
-
What does "PMAK" stand for? – Lior Oct 11 '20 at 18:23
-
I had to look for this too. Here you go: https://learning.postman.com/docs/developer/intro-api/#generating-a-postman-api-key. You may need to get a Postman Pro subscription to generate this API key and use their Pro API, but verify. Postman Pro API documentation here: https://documenter.getpostman.com/view/2483749/6mxbxTS#intro – Kreidol Aug 02 '22 at 00:34
0
It is not possible to select environment from scripts. the closest one can get, is to receive the name of the currently active environment: pm.environment.name

Christian Baumann
- 3,188
- 3
- 20
- 37