Setting an environment variable while not active

Hi,

I have multiple environments set up (i.e. Production, Testing, Local, etc) and there are occasions when I need to test in Local while also having to use data from other environments, so I wonder if there’s a way in a post-response script to set a variable of another environment other than the active one.

Like if I request something while in Test, have the result be stored to the Local environment so then I can test what I need there.

Hope this makes sense.

Hey @desto-ngic :waving_hand:

Welcome to the Postman Community! :postman:

You should be able to use the Postman API to do this by sending an async request, in the post-response script.

This is just a dummy example of what you can add to the script.

The PATCH examples will give you a bunch of different ways to do this and be more targeted, without overwriting the whole environment.

pm.sendRequest({
    url: 'https://api.getpostman.com/environments/your-environment-id',
    method: 'PUT',
    header: {
        'Authorization': 'Bearer your-access-token',
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            name: 'Updated Environment',
            values: [
                {
                    key: 'your-variable-key',
                    value: 'new-value',
                    type: 'string'
                }
            ]
        })
    }
}, function (err, response) {
    if (err) {
        console.error(err);
    } else {
        pm.test('Environment variable updated successfully', function () {
            pm.response.to.have.status(200);
            pm.expect(response.json().name).to.eql('Updated Environment');
        });
    }
});

I should have read the description a little better, but using PUT replaced ALL of my environment variables with the one I was testing with. Should be a PATCH instead.

But even then, this is a half-baked solution. It requires to set the variable with the index instead of the name. I can make it work but it seems odd to rely on the variable index instead of the name. Any change in the ordering of variables has to be taken into account.

I did mention that it had the potential for doing that :grimacing:

We’re you using one of the examples listed under the update environments?

Yes, I used the PUT from there, but I switched to the PATCH and it worked, now I need to implement that in the Post-response script but I’m struggling to set the header correctly so the request is authenticated.

There a number of different payload options that perform different actions so it’s worth exploring them.

Please do share your final scripts as these might help others in the community.

This is what I currently have:

// Replace these with your own
let api_key = pm.globals.get("postman_api_key");
let env_local_id = pm.globals.get("env_local_id");

pm.sendRequest({
    url: "https://api.getpostman.com/environments/" + env_local_id,
    method: "PATCH",
    header: {
        "X-API-Key" : api_key,
        "Content-Type": "application/json"
    },
    body: {
        mode: "raw",
        raw: JSON.stringify([
            {
                "op": "replace",
                "path": "/values/2/value",
                "value": "New Value Here"
            }
        ])
    }
}, function (err, response) {
    if (err) {
        console.error(err);
    } else {
        console.log("Sent request to postman");
        console.log(response);
    }
});

It more or less works, but it is setting the Initial value of the variable, not the Current value, which doesn’t really help me.

As it’s using the Postman API, it’s a synced variable (initial) as all the current variables stay local.

Another method you could use is the Postman Vault, you can programmatically set variables within scripts (when explicitly enabled), those values are local and can be used across Workspaces/Collections.