# Cant add new envrionemtn variables via postman api

**URL:** <https://community.postman.com/t/cant-add-new-envrionemtn-variables-via-postman-api/33784>\
**Category:** Help Hub\
**Created:** [March 23, 2022, 7:55pm UTC](https://community.postman.com/t/cant-add-new-envrionemtn-variables-via-postman-api/33784 "2022-03-23T19:55:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile.Swain](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@Emile.Swain](https://community.postman.com/u/Emile.Swain)\
**Post date:** [March 23, 2022, 7:55pm UTC](https://community.postman.com/t/cant-add-new-envrionemtn-variables-via-postman-api/33784/1 "2022-03-23T19:55:31Z")

</div>

**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](https://documenter.getpostman.com/view/631643/JsLs/#6517e0d6-3bc3-3da5-ab57-7a578a8504ce), I can authenticate and successfully call the [https://api.getpostman.com/environments/{{environment\_uid}](https://api.getpostman.com/environments/%7B%7Benvironment_uid%7D)} api end point.

**Code**  
I run this in a pre-request script, and then use postman echo to return the values.

```auto

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.

---

<div class="post-metadata">

**Author:** ![Emile.Swain](https://avatars.discourse-cdn.com/v4/letter/e/a6a055/32.png) [@Emile.Swain](https://community.postman.com/u/Emile.Swain)\
**Post date:** [March 23, 2022, 8:10pm UTC](https://community.postman.com/t/cant-add-new-envrionemtn-variables-via-postman-api/33784/2 "2022-03-23T20:10:26Z")

</div>

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)`

```auto
/// 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](https://postman-echo.com/get)

```auto
//// ##### 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);

```
