Cant add new envrionemtn variables via postman api

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.

I’ve worked out a temporary solution. Though I do think that the rest API should provide this functionality. To add new variables to a specific environment.

Because i’m making the call from Postman itself, I can add the environment variable by using pm.environment.set(val.key, val.value)

/// the postman request is calling https://postman-echo.com/get
// this is in the pre-request script along with the above request to the postman rest api.
variables.map(
    (val)=>{
        pm.environment.set(val.key, val.value);
        pm.request.url.query.add(val.key+'=' + pm.environment.get(val.key));
    }
)

Obviously this isn’t ideal, as you have to select the environment in postman. Incidentally, this is actually a solution to a different problem, in that the pm.environment.set() function can’t set the initial value, where as the rest api does. So together these two approaches solve a couple of issues i’ve seen on other threads.

The Full pre-request script
Collection request endpoint https://postman-echo.com/get

//// ##### comment return when you are sure you have this correctly updated.
//return 

let variables = [
    {  key: "value_exists",value:"Value is update" },
    {  key: "value_doesnt_exist",value:"Is value added ?????" },
]

pm.request.url.query.add("Followup=Helpfull message for users running this script returned in echo.");

variables.map(
    (val)=>{
        pm.environment.set(val.key, val.value);
        // Add the environment value to the request.
        pm.request.url.query.add(val.key+'=' + pm.environment.get(val.key));
    }
)

//// TODO make this await so we see the properly updated values. I don't think pre-request script will wait for this to complete. https://community.postman.com/t/executing-sync-requests-programmatically-from-a-pre-request-or-test-scripts/14678

pm.sendRequest({
    url: 'https://api.getpostman.com/environments/'+pm.variables.get("postman_environment_uid_birley_dev"),
    method: 'PUT',
    header: {
        "x-api-key" : pm.globals.get('postman_api_key'),
        "Content-Type" :"application/json",
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
    "environment": {
        "values": variables
    }
})
    }
}, function (err, res) {
    console.log(res);
});

/// ##############################################################
// BLAME would be nice to have pm.user.name
/// ##############################################################
var moment = require('moment');
let timeex = moment().format(("YYYY-MM-DD HH:MM"));
pm.environment.set("Helper_script_setDevEnvVars", "Last executed "+timeex);