How to access --env-var variable in pre-request script?

My question:
How to access environment variable passed from command line in pre-request script?
command I am using:

newman run ‘collection.json’ --env-var “Key=Value”

In pre-request script:

Value for above expression is undefined .

Details :
I wanted to pass secrets from pipeline rather than hardcoding in the newman script. Its best practise to keep secrets out of code repository. So I am using environment variable.
Postman version to create collection: v10.8.0
newman npm version: 5.3.2

How I found the problem:
When specified env var in command line and tried to extract it in pre-request script.

I’ve already tried:
I have tried global variables in command line as well, not working it either.

Any help much appreciated.

It looks like you are running the collection without an environment.

Don’t you need to include the environment in the newman command line before you can include the environment variable.

–env-var CLI option provides a way to specify env var dynamically. Please correct if that’s not true.

Yes, but you have to have an environment set for it to be added to. Otherwise where is it going to store the variable?

1 Like

Thank you @mdjones its working after specifying env file path. I misunderstood the --env-var usage I guess.
Below command worked and pre-request can access cli env param.
newman run ‘collection.json’ -e ‘environmentfile.json’ --env-var “Key=Value”

1 Like