Hi,
When I run a collection with an environment using Postman CLI it doesn’t seem to set or pickup the environment variables:
postman collection run (collection-uuid) -e (environment-uuid)
This is the format of the request - https://api.{{Env}}.mycompany.tech/person/create
This is my collection pre-request:
var currEnv = pm.environment.name;
console.log("Current Environment: " + currEnv);
if (currEnv == "TEST") {
pm.environment.set('vaultSubKey', await pm.vault.get("APISubKeyTest"));
pm.environment.set('Env', "test");
console.log("Env: " + pm.environment.get('Env'));
console.log("vaultSubKey: " + pm.environment.get('vaultSubKey'));
} else if (currEnv == "STAGING") {
pm.environment.set('vaultSubKey', await pm.vault.get("APISubKeyStg"));
pm.environment.set('Env', "staging");
} else {
throw new Error("Environment is not set to TEST or STAGING!");
}
If the Collection is run via the Postman desktop runner everything works fine, however when run via CLI this is what’s returned
getaddrinfo ENOTFOUND api..mycompany.tech
I have read Editing Environmental variables while using Postman CLI but don’t know if that’s what I need to do to “fix” my runs in CLI?
I am also using vault keys and have uploaded a test data file to the workspace. Does this all get picked up by the CLI or will I need to do something more to get them picked up?
Ok, the issue with the environmental variables seems to be fixed by “sharing” the variables to the cloud 
Hi @faisalk-ps Thanks for this post.
You’re right, Postman CLI will only fetch and use the environment values marked as “Shared” to the cloud, given it executes independently of the desktop/web apps and needs to fetch the synced variable values from the cloud to run a collection.
If there’s anything else we can help with, do let us know.
1 Like
@devesh-kumar-postman- I think the environment is created using the UUID with the Keys correct? Why doesn’t pm.environment.set(‘Env’, “test”); in the collection pre-request set the value at runtime?
Hi @faisalk-ps It seems that you’re using Vault methods in the pre-request script which aren’t currently supported in Postman CLI (Your Vault is linked to your app instance - I.E: The desktop app or desktop agent you run), and hence the statements for setting the environment variables aren’t getting executed.
Will check why the errors are silent for script failure, but replacing the usage of vault will fix the issue you’re facing.
Yes, for the vault codes etc figured out can’t access them on the cloud and have updated the runner to pass them in as an env variable. Also passing in test data file from a known path:
postman collection run XXXX-XXX-XXXX -e YYYY-YYYY-YYYY --env-var “vaultSubKey=ZZZZZ” -d “C:\Postman Test Data\TEST.json” -r junit,html
This is the console logging from the desktop app:
Current Environment: STAGING
Env: staging
vaultSubKey: *****
and this is the logging from the runner:
┌
│ ‘Current Environment: TEST’
└