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

**URL:** <https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947>\
**Category:** Help Hub\
**Tags:** newman-project, pre-request-script, environment\
**Created:** [January 17, 2023, 6:04am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947 "2023-01-17T06:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maddythedeveloper](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@maddythedeveloper](https://community.postman.com/u/maddythedeveloper)\
**Post date:** [January 17, 2023, 6:04am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947/1 "2023-01-17T06:04:53Z")

</div>

> **Archived topic — this may be out of date.**  
> Newman has been replaced by the **[Postman CLI](https://learning.postman.com/docs/postman-cli/postman-cli-overview/)**, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.  
> Working with an AI coding agent? The [Postman plugin](https://github.com/postmanlabs/postman-plugin) lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.  
> For current help, see the [Postman docs](https://learning.postman.com/docs/), try the free [Postman tools](https://www.postman.com/tools) (no account needed), or join us on [Discord](https://discord.gg/58aNNsRBGR).

**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:  
_pm.environment.get(“Key”)_

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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 17, 2023, 11:22am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947/2 "2023-01-17T11:22:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![maddythedeveloper](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@maddythedeveloper](https://community.postman.com/u/maddythedeveloper)\
**Post date:** [January 17, 2023, 11:29am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947/3 "2023-01-17T11:29:47Z")

</div>

[–env-var](https://learning.postman.com/docs/running-collections/using-newman-cli/newman-options/#miscellaneous-options) CLI option provides a way to specify env var dynamically. Please correct if that’s not true.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 17, 2023, 11:34am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947/4 "2023-01-17T11:34:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maddythedeveloper](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@maddythedeveloper](https://community.postman.com/u/maddythedeveloper)\
**Post date:** [January 18, 2023, 9:03am UTC](https://community.postman.com/t/how-to-access-env-var-variable-in-pre-request-script/42947/5 "2023-01-18T09:03:35Z")

</div>

Thank you @michaelderekjones 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”_
