How to pass a variable with a collection using Newman library

Archived topic — this may be out of date.
Newman has been replaced by the Postman CLI, 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 lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.
For current help, see the Postman docs, try the free Postman tools (no account needed), or join us on Discord.

Hi everyone,

I’m new to the world of node.js / newman so please bear with me :slight_smile:

I used this excellent Youtube clip How to programmatically save the response to file in Postman - YouTube to help me build a script to automate posting of data to supplier and write the response to a file.

In essence, I’m trying to build a script that loops through XML files stored in a windows directory and posts the contents of the XML file to the supplier’s HTTP API using Newman.

The issue; how do I pass the content from the XML to the “collection:require statement” , just unsure of the syntax.

I already have a variable in the collection to hold the API “body” as per screenshot;

Hope this makes sense

Thanks

I think I got this working, using this post - node.js - How can a Postman environment variable value be passed into an nodejs script executing Newman? - Stack Overflow

Define the environmentVariables , in my case data is a variable that contains content from the XML file

environmentVariables = [];
environmentVariables.push(
  {
      key: "testBody",
      value: data,                    
  }
   );

Pass the variable to the collection run

newman.run ({
      collection: require('c:/postman/postman_collection.json'),
      envVar: environmentVariables,
      reporters: 'cli'