Editing Environmental variables while using Postman CLI

I have been looking for clarification as to if you can update an environmental variable from a pre-script or test script of a request when running the collection via Postman CLI. There is clear documentation for accessing an environment from Postman by using the -e [UID] when initiating the collection run. It seems that the initial value can be accessed, but I have not been able to confirm that the current value is updated in the environment if the test script does that as part of its commands using pm.environment.set().

When running Postman collections via Postman CLI or Postman Monitors, changes to the environment do not persist.

When running via Runner through the Postman app, by default “current” values will persist. This can be changed from uder the advanced setting when creating a run, by toggling Keep variable values field.


You can, though, use Postman API’s to directly set the Environment values in you Collection scripts: Postman API Reference Workspace

You will need to create a Postman API key, to use these APIs: Access Postman data programmatically | Postman Learning Center

Thank you for the response. It seems that passing -e [UID] before the rest of the request IDs creates an instance of the environment that can be reference and updated for the extent of that individual run. Is that correct and what limitations are there on that environment instance?

@nategerman I assume you mean executing a collection by passing UUIDs for the environment? as follows:

postman collection run <collection-UUID> -e <environment-UUID>

In this instance, changes to environment will persist only for the duration of the collection run. If you want to persist changes past the run, you will need to use Postman’s APIs to update the environment.

Following can be used:

let apiKey = pm.environment.get('pm-api-key'); // Make sure to set your API key

// **Setup Request**
// See how we are passing the environment id and existing environment values
// pm.environment.id - will get the ID we set using `-e [UUID]`
// pm.environment.values - will return the array of variables current set 
updateEnvRequest = {
    url: 'https://api.getpostman.com/environments/' + pm.environment.id,
    method: 'PUT',
    header: {
        'Content-Type': 'application/json',
        'x-api-key': apiKey
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({
            environment: {
                values: pm.environment.values
            }
        })
    }
};

// Execute request to update the environment
pm.sendRequest(updateEnvRequest, (err, response) => {
    if(err) {
        console.log(err)
    } else {
        pm.test('Make sure environment updated passed', () => {
            pm.expect(response).to.have.property('code', 200);
            console.log('Environment updated!');
        });
    }
});

Thank you. That clarifies what I needed to know.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.