Question
I was hoping that I could script the addition of a variable to an existing environment using the postman API.
Attempted
I have attempted to do this by following this guide here, I can authenticate and successfully call the https://api.getpostman.com/environments/{{environment_uid}} api end point.
Code
I run this in a pre-request script, and then use postman echo to return the values.
pm.sendRequest({
url: 'https://api.getpostman.com/environments/'+pm.variables.get("postman_environment_uid_dev"),
method: 'PUT',
header: {
"x-api-key" : pm.globals.get('postman_api_key'),
"Content-Type" :"application/json",
},
body: {
mode: 'raw',
raw: JSON.stringify({
"environment": {
[
{"key": "existing_value", "value": "Value Updates"},
{"key": "new_value", "value": "value isnt added"}
]
}
})
}
}, function (err, res) {
console.log(res);
});
Results
The call successfully completes. However what I have noticed is that if the variable doesn’t already exist, then the that variable is not added.
If the variable exists, then its value is successfully updated.
Possible alternatives
I guess i could delete and re-create the environment. But that seems a bit drastic, and could loose values that have been manually added.
Side Note
To be honest, i shouldn’t have to do this, but what i’ve noticed with environments is that I can’t easily propogate a new value from say my development Environment into my Production and UAT environments.
I.e. Or at least its not clear how I can merge changes in Dev into other environments. So i’m script it.
I may revist the forking functionality of environments.