# Access environment using newman terminal command

**URL:** <https://community.postman.com/t/access-environment-using-newman-terminal-command/22229>\
**Category:** Help Hub\
**Tags:** newman-project, tests\
**Created:** [April 3, 2021, 9:12am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229 "2021-04-03T09:12:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:12am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/1 "2021-04-03T09:12:59Z")

</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).

I have a tests script in Postman where I access the data using **pm.environment.get(“lastSetDate”)** How do I achieve the same using

```
newman.run({
    collection: require('./Request.json'),
    reporters: 'cli'
}).on('request', (error,data) => {
    let rsp = data.response.stream.toString()
   // pm.environment.get("lastSetDate")
    }     
});

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:22am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/2 "2021-04-03T09:22:02Z")

</div>

Hey @girishnair12345

Not really sure what you’re trying to do there, that Newman run doesn’t include an environment so it wouldn’t currently run with one.

You can’t use any of the same `pm.*` commands outside of the collection like that because that node script doesn’t know what they mean.

The Newman Summary object will contain information about the full run though and you can access that in a similar way as you have done in the ‘request’ event.

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:22am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/3 "2021-04-03T09:22:38Z")

</div>

I need to just write to file, is that possible with test?  
How can I write file in test section of request  
`pm.environment.get("lastSetDate")`

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:27am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/4 "2021-04-03T09:27:18Z")

</div>

You cannot directly write to an external file from within the Collection.

You can write multiple things to a file, using the node `fs` module in that script. The data from the run will be in the Newman Summary object.

The `summary.environment` object will contain the environment variables used for the run.

> **[postmanlabs/newman - newman.run~callback(error: object , summary: object)](https://github.com/postmanlabs/newman#newmanruncallbackerror-object--summary-object)**
>
> Newman is a command-line collection runner for Postman

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:29am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/5 "2021-04-03T09:29:59Z")

</div>

Posts like this have shown different ways that items can be written to a file.

> [@How to Save Get response to local file?](https://community.postman.com/t/how-to-save-get-response-to-local-file/17942/2):
>
> Hey @bukhari.adac This is a very common question and doing a quick search in the forum would have returned a few different ways of writing those to file. grin Using Newman as a library and paring that with the Node fs module you can save the responses to a file like this: const newman = require('newman'), fs = require('fs'); newman.run({ collection: '\<Postman Collection\>' }).on('request', (err, args) =\> { fs.writeFile(`./results/${args.item.name}.json`, args.response.stream, (err…

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:34am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/6 "2021-04-03T09:34:48Z")

</div>

That’s the same that I am using but cannot access the values the way we can access in postman Eg pm.environment.get() is not available

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:37am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/7 "2021-04-03T09:37:07Z")

</div>

It will give you an object of all the values, that you can then use get anyone you like.

What have you tried in the node script and what is currently not working?

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:37am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/8 "2021-04-03T09:37:25Z")

</div>

> [@danny-dainton](#):
>
> environment

`newman.summary.environment` is Invalid and `newman.summary` is **undefined**

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:39am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/9 "2021-04-03T09:39:05Z")

</div>

That’s not the why you would reference that.

Where did you get the original script, which is using the `request` on event? Is that something you have copied or wrote yourself?

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:40am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/10 "2021-04-03T09:40:32Z")

</div>

> [@danny-dainton](#):
>
> Where did you get the original script, which is using the `request` on event? Is that something you have copied or wrote yourself?

Took from a youtube video, here is the code

```
newman.run({
 collection: require('./Request.json'),
     reporters: 'cli'
 }).on('request', (error,data) => {
     // console.log(newman.summary)
 });

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:46am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/11 "2021-04-03T09:46:24Z")

</div>

There are several `on` actions (you can find these in the Newman docs) which give you different pieces of information about the Collection run.

That is using the `request` event which has information about the request, this is all stored in the `data` arg of that function.

Doing a `console.log(JSON.stringify(data))` in that function will show you that request information.

You probably want the `beforeDone` or `done` event to get the environment variable.

```javascript
newman.run({
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('beforeDone', (error, data) => {
   console.log(JSON.stringify(data.summary.environment))
});

```

I haven’t run any of this so it might be wrong, I’m not at my machine to try this out.

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 9:52am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/12 "2021-04-03T09:52:45Z")

</div>

Unfortunately, the data is not in JSON but in HTML but that is not what I want, it’s the value of `pm.environment.get()` to be written on a file. Tried using `beforeDone`, `done` and `JSON.stringify (data. environment)` but all are giving `undefined`

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 3, 2021, 9:55am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/13 "2021-04-03T09:55:57Z")

</div>

The whole Newman Summary is in JSON, it’s a JSON file.

I can’t see what you have done or what the output of that is without you sharing images of what’s in front of you.

When something is `undefined`, try working backwards and log out the whole thing before trying the log something specific.

The Summary object of a Newman run will look something like this:

> <https://gist.github.com/DannyDainton/8d673fd164d9040fed11b6026f5aa300#file-sample_newman_summary-json>

You would need this to log the environment variables:

```javascript
newman.run({
 collection: require('./collection.json'),
 reporters: 'cli'
}).on('done', (error, data) => {
   console.log(JSON.stringify(data.summary.environment))
});

```

---

<div class="post-metadata">

**Author:** ![girishnair12345](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/girishnair12345/32/7944_2.png) [@girishnair12345](https://community.postman.com/u/girishnair12345)\
**Post date:** [April 3, 2021, 10:10am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/14 "2021-04-03T10:10:12Z")

</div>

> [@danny-dainton](#):
>
> ```auto
> console.log(JSON.stringify(data.summary.environment))
> 
> ```

Yes, it was a typo on my side. It’s working. Just a quick question How do I access a variable from `data.summary.environment` let say the variable name is **lastSetDate**. This is the output  
`{"type":"any","value":"2010-08-06T18:30:00.000Z","key":"lastSetDate"}`

---

<div class="post-metadata">

**Author:** ![kappagaa](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@kappagaa](https://community.postman.com/u/kappagaa)\
**Post date:** [February 28, 2022, 4:56am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/15 "2022-02-28T04:56:53Z")

</div>

I just got this to work. You can try…

> console.log(summary.environment.values.reference..value);

Next I will be dumping this to a file.

You can also play around with the summary.environment object and dump things like…

//this should list all variable values…

> ```
> for (i=0; i<summary.environment.values.members.length; i++){
> console.log(summary.environment.values.members[i]);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![VennilaSaha](https://avatars.discourse-cdn.com/v4/letter/v/8baadc/32.png) [@VennilaSaha](https://community.postman.com/u/VennilaSaha)\
**Post date:** [March 9, 2022, 7:12am UTC](https://community.postman.com/t/access-environment-using-newman-terminal-command/22229/16 "2022-03-09T07:12:04Z")

</div>

Hi @kappagaa ,  
thanks, this is working. Am able to read environment variable value now.  
Is there any way to read specific collection variable from summary.collection?  
If yes, can you please share the syntax?

Thanks!
