# Newman library in node.js script: access iterationData value for each iteration cycle

**URL:** <https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564>\
**Category:** Help Hub\
**Tags:** newman-project, library\
**Created:** [November 10, 2020, 1:06pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564 "2020-11-10T13:06:14Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 1:06pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/1 "2020-11-10T13:06:14Z")

</div>

Hello,

I have a node.js script reading data from an inpout CSV file and making request to a 3rd party API. For each iteration of the input data I want to log to the console the values read but can not fine a way to do so? I have looked into the ‘args’ content for a number of events and can not find any populated with the data read from the input file?

thanks

---

<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:** [November 10, 2020, 1:53pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/2 "2020-11-10T13:53:57Z")

</div>

Hey @guiom

Welcome to the community! ⭐

The best way to see what’s happening is to share the script that you’re using, it’s far easier that trying to describe it 😁

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 1:59pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/3 "2020-11-10T13:59:02Z")

</div>

there is nothing to the script really:

the initial section is as follows:

```auto
newman
    .run({
        collection: options.collection,
        environment: options.environment,
        iterationData: options.data,
        reporters: "cli",
        verbose: true
    })

```

I then have a section dealing with the response data from the remote API:

```
.on('request', function(error, args) {
    if(args.response.json()) {
     write response body to file
    }

```

}

what I need is to be able to write to the output file the data read from the input CSV and I can not find a way to access the iteration data in any of the events.

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 2:05pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/4 "2020-11-10T14:05:21Z")

</div>

the input data for example would be:

number  
12345  
23456  
78901  
etc.

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 2:05pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/5 "2020-11-10T14:05:54Z")

</div>

on write the response body to the file I would for example what to prepend the ‘number’ value to the line entry

---

<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:** [November 10, 2020, 2:23pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/6 "2020-11-10T14:23:41Z")

</div>

This is something that I would use to run the Collection and write the response to a file:

```auto
const newman = require('newman'),
      fs = require('fs');

newman.run({
  collection: 'collection.json',
  environment: 'environment.json',
  iterationData: 'data.json'
}).on('request', function (error, args) {
  if (error) { 
    console.error(error)
  } else {
    fs.writeFile('response.json', args.response.stream, function (error) {
      if (error) { console.error(error) }
        console.log(`File Created`)
    })
  }
})

```

Not sure I follow the part about the iteration data from the CSV as you have that separate to the collection now in the `iterationData` property.

You might need to explain that flow a little more.

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 3:20pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/7 "2020-11-10T15:20:24Z")

</div>

thanks and yes that is fine to write the response data to file and essentially how it is done currently.

With regards to the iteration data, you say this is available in the iterationData property. How would you output a console entry with the data (from the iteration data at the current index) being used in the current request?

for sake of argument if my iterationData: ‘data.csv’ is as follows:

field1,field2  
a,b  
c,d  
e,f  
etc.

So effectively 3 iterations of data with 2 values each.

As you write the response stream to file. How would I get the field1 and field2 values that were used in the corresponding request?

---

<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:** [November 10, 2020, 4:59pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/8 "2020-11-10T16:59:32Z")

</div>

I tried to answer that here:

> [@Save response in CSV file via newman](https://community.postman.com/t/save-response-in-csv-file-via-newman/15508/7):
>
> It looks like using Newman as a library, rather than from the command line with a specific reporter is going to be the option here. You can tap into the different events and extract the data you want from the response and feed this into a CSV file. The node ‘fs’ module will allow you to write the data into a file. [https://nodejs.org/api/fs.html](https://nodejs.org/api/fs.html) Just a hacky example piece of code and is not a finished or polished solution: const newman = require('newman'), fs = require('fs') newman.…

The only thing that you would need to change is the way you get the values from the data field during the different iterations:

```auto
let fieldOne = pm.iterationData.get('field1'),
    fieldTwo = pm.iterationData.get('field2');

console.log(`\n${fieldOne},${fieldTwo}`)

```

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 10, 2020, 7:51pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/9 "2020-11-10T19:51:05Z")

</div>

> [@danny-dainton](#):
>
> `.iterationData.get('field1')`

ah great thanks. I seem to be getting “ReferenceError: pm is not defined” and presumably need to reference it elsewhere in the script for this to work. Obviously within say a Postman pre-request script and environment it is defined, but how to do this within a node.js script?  
"

---

<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:** [November 10, 2020, 8:02pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/10 "2020-11-10T20:02:52Z")

</div>

Hmm…that’s odd. Which version of Postman are you using?

It just needs to be in the Collection in the places that I mentioned - As in, the code above needs to go in the `Pre-request Script`.

You don’t need to do anything in the node script apart from adding the `.on('console')` part from the linked thread.

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 11, 2020, 1:32pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/11 "2020-11-11T13:32:43Z")

</div>

ah ok fine I get it and many thanks.

this works fine, still its a bit hacky 😉 I would have thought that accessing the iteration data directly from the events in a node.js script would be quite a common requirements. In the same way that saving the response body to file directly from Postman is quite an obvious requirement.

Many thanks for your help, prompt response and so on. This is much appreciated

---

<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:** [November 11, 2020, 1:59pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/12 "2020-11-11T13:59:19Z")

</div>

If you’re using the `datafile` with Newman, you would have it stored _somewhere_ in the first place.

```auto
newman.run({
  iterationData: 'data.json'
})

```

It’s hacky because it feels like, a _round the houses_ way of getting the data via the Collection run, that you already have available before the run. 😃

There is an option to `Send and Download` the response body to a file when sending individual requests but it’s not part of the Collection Runner.

All feature requests can be added here though:

> **[Issues · postmanlabs/postman-app-support](https://github.com/postmanlabs/postman-app-support/issues)**
>
> Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster. - Issues · postmanlabs/...

---

<div class="post-metadata">

**Author:** ![guiom](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/guiom/32/1217_2.png) [@guiom](https://community.postman.com/u/guiom)\
**Post date:** [November 11, 2020, 2:22pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/13 "2020-11-11T14:22:47Z")

</div>

ok let me explain further and maybe it will make sense.

the input file is a number, say a telephone number. this number is used to query a data source which returns a response body with some data related to the number. It does not however echo back the number itself. So to write the number to file together with the response body I need to access to iterationData as the request is made. It works via preRequest scripts and the on console event as we discussed above.

The data is indeed in the options for the Newman run

```auto
newman.run({
       iterationData: 'data.json'
})

```

I dont know if this iterationData can be accessed in the events which is why I have to go via the console at the moment. There is possibly a better way but I havent found it so far.

---

<div class="post-metadata">

**Author:** ![asenecal](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@asenecal](https://community.postman.com/u/asenecal)\
**Post date:** [April 8, 2021, 1:33pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/14 "2021-04-08T13:33:07Z")

</div>

> [@guiom](#):
>
> ```auto
> .on('request', function(error, args) {
> if(args.response.json()) {
> write response body to file
> }
> 
> ```
> 
> }

Hello gulom, I’m trying to achieve the same thing. You can get the iteration index from the args value as follows:

```auto
.on('request', function(error, args) {
    var indexOfIterationDataFileLine = args.cursor.iteration;
    ...
}

```

You can then use that index to look up the line in your data file, be it CSV or JSON.

---

<div class="post-metadata">

**Author:** ![amazpyel](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@amazpyel](https://community.postman.com/u/amazpyel)\
**Post date:** [May 21, 2021, 12:08pm UTC](https://community.postman.com/t/newman-library-in-node-js-script-access-iterationdata-value-for-each-iteration-cycle/17564/15 "2021-05-21T12:08:22Z")

</div>

So there is no proper way to get current iterationDate using newman API, right?
