T10 Postman · Lesson 1
Requests in Postman
Postman is an app for asking APIs questions by hand: you fill in a request, press Send and read the answer. Learn its window in words, write a request as a collection's JSON, keep several requests in a first collection, run them with Newman, Postman's open-source runner, and read its report.
- name the parts of Postman's window and say which part of a request each one writes
- write a request in the collection format: its name, method, URL, headers and body
- keep several requests in a collection file and say in what order they run
- run a collection with Newman's library and print what each request answered
- add a first test to a request and find a failed test in Newman's summary
- install Newman, run a collection from the command line and read its report and exit code
The back cover
Every hall of the yard has questions for its servers: list the gauges, add a reading, read one back. Asked once, by hand, a question is easy. Asked every day, by many people, it needs writing down, so that nobody has to remember how it was asked.
These pages bring that writing desk to the web. They walk through Postman, the app where requests are written, sent and kept, and map every part of its window to the JSON it stores. They gather requests into a first collection, run it with Newman, Postman's open-source runner, and read its report, from the first tick to the exit code.
Before you start
The story: Questions without a ledger
Across the road from the Couriers' Yard stands the Testing House, a quiet room of tall desks. Every hall sends its questions here, to be asked of the yard by hand. You find the desks covered in loose slips: list the gauges, add the fan, read the chimney, each written a little differently by whoever asked last.
Nobody can find yesterday's slip, so every question is written again from memory. One clerk forgets to say what kind of parcel is inside, and the yard cannot read it; another sends a question to the wrong door and waits for an answer that never comes.
Postmaster Meera Franking sweeps the slips into one pile. "A question worth asking once is worth asking again," she says. "Write each one down whole, keep them in one box in order, and let a runner ask them all."
In the APIs track you wrote servers, and you asked them questions with fetch, one line of code at a time. That works, but each question lives inside a program, and you write it again whenever you want to ask it again. People who build and test APIs need something steadier: a place to write a question once, keep it, ask it again tomorrow, and share it with a friend.
Postman is that place. It is an app for asking APIs questions by hand. You fill in a request, press Send, and read the answer. You save the request, and it waits for you. This lesson teaches the app's window in words, the request as Postman stores it, a first collection of requests, and Newman, Postman's open-source runner, which asks a whole collection in one go.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| HTTP methods. | GET reads, POST adds | The method menu at the left of every request. |
| URLs, hosts and ports. | http://localhost:3000/gauges | The URL box, and the url field of a request. |
| Query strings. | /gauges?unit=C | The Params tab. |
| Headers. | Accept: application/json | The Headers tab, and the headers Postman adds. |
| JSON bodies and Content-Type. | express.json() | The Body tab's raw JSON. |
| Status codes. | 200, 201, 404 | The response pane, and a first test. |
| An Express server. | app.get("/gauges", ...) | The gauges server every block asks. |
| Modules. | module.exports = app | server.js, required by each block's app.js. |
| npm and npx. | npm install newman | Installing Newman and running its command. |
| Events and listeners. | run.on("request", fn) | Everything a Newman run reports. |
| Exit codes. | echo $? | How a run tells a script that a test failed. |
By the end of this lesson you will be able to:
- name the parts of Postman's window and say which part of a request each one writes;
- write a request in the collection format: its name, method, URL, headers and body;
- keep several requests in a collection and say in what order they run;
- run a collection with Newman's library and print what each request answered;
- add a first test to a request and find a failed test in Newman's summary;
- install Newman, run a collection from the command line, and read its report and exit code.
How to follow this lesson
Postman is an app with a window, and this page cannot hold that app. So the window is described in words, part by part, from Postman's own documentation, and every part is matched with the JSON that it writes. That JSON is what Postman really keeps, and it is what this page runs.
Every block is one program: it starts the gauges server, then runs a collection against it with Newman's library, and prints what happened. It runs on the page in the small Node with a small Newman, and it has also been run with the real Newman 6.2.3 in real Node 22, where it prints the same.
In one line
Postman is an app for writing API requests, sending them by hand and keeping them; a saved request is JSON in a collection, and Newman runs a whole collection for you.
Postman's window, in words
At a glance
Postman is an app for asking APIs questions by hand, and its window is built around that one job.
The window has four areas: a header, a sidebar with collections and History, a workbench of request tabs, and a footer with the Console. A request tab starts with the method menu, the URL box, Send and Save, then the tabs Params, Authorization, Headers, Body, Scripts and Settings. The answer fills the response pane below. Every part of the tab writes one field of the request's JSON, which is what Postman keeps.
Take a guess: Did Postman begin inside a big company, or as one person's side project?
Postman runs as a desktop app for Windows, macOS and Linux, or as a web app in a browser. Either way, its window has the same four areas. Learn where they are first, the way you learn the rooms of a new house before you learn what is in each drawer.
| Area | Where it is | What it holds |
|---|---|---|
| the header | across the top | your workspaces, a search box, your settings and your account |
| the sidebar | down the left side | your collections and environments, and a History of the requests you have sent |
| the workbench | in the middle | a tab for each open request: the request at the top, and its answer below it |
| the footer | along the bottom | the Console, which logs every request Postman sends, and other tools |
Most of your time is spent in the workbench, inside one request's tab. So that is where we go next, from the top of the tab to the bottom.
A request tab, from the top
The top line of a request tab is the request itself. On the left is the method menu. A new request starts on GET, and you can pick POST, PUT, PATCH, DELETE and others. Next to it is the URL box, where you type the address. Then comes Send, which asks the question now, and Save, which keeps the request in a collection.
One small kindness hides in the URL box. If you type an address with no scheme, such as localhost:3000/gauges, Postman puts http:// in front of it for you.
Under the top line is a row of tabs. Each tab writes one part of the request:
- Params: the query pairs, such as unit=C. Postman keeps this tab and the URL in step, so a pair typed in one appears in the other.
- Authorization: who you are, for an API that asks.
- Headers: the headers you add. Selecting hidden at its top shows the headers that Postman adds by itself.
- Body: the parcel that the request carries. It starts on none. For JSON you pick raw, then JSON from a small menu of languages: Text, JavaScript, JSON, HTML and XML. Picking a language also sets the Content-Type header for you.
- Scripts: two places for JavaScript. Pre-request runs before the request goes out, and Post-response runs after the answer comes back. Tests live in Post-response.
- Settings: small choices for this one request, such as whether to encode the URL.
The response pane, below
When you press Send, the answer fills the lower half of the tab, the response pane. Its top line shows three numbers: the status code, such as 200 OK, the time the answer took in milliseconds, and its size. Under them is a second row of tabs:
| Tab | What it shows |
|---|---|
| Body | the answer's body, shown as JSON, XML, HTML or plain text |
| Cookies | the cookies the server set |
| Headers | the answer's headers, as names and values |
| Test Results | how many of the request's tests passed, out of how many ran |
A form and its copy
Think of the request tab as a printed form at a post office: boxes for the kind of request, the address, the extra notes and the parcel. You fill in the boxes. What the clerk keeps is not the form's paper; it is what you wrote in each box.
Every part writes JSON
When you save a request, Postman keeps it inside a collection, a named box of saved requests. A collection is a JSON document. When you export one, Postman writes it in the Postman Collection Format, version 2.1. Each part of the request tab writes one field of that JSON:
| Part of the window | What you do there | The field it writes |
|---|---|---|
| the name you give at Save | name the request | name, on the item |
| the method menu | pick GET, POST and so on | request.method |
| the URL box | type the address | request.url |
| the Params tab | add query pairs | the query part of request.url |
| the Headers tab | add headers | request.header, a list of key and value pairs |
| the Body tab: raw, JSON | type the parcel and pick its language | request.body: mode raw, the text, and the language json |
| the Authorization tab | say who you are | request.auth |
| the Scripts tab | write JavaScript | event: a script that listens for prerequest or test |
| Save | keep the request | one item in the collection's item list |
| Send | ask once, now | Newman asks every item of a collection, in order |
This table is the key to the whole track. The window is one way to write the JSON; typing it is another. Newman, Postman's runner, reads the JSON either way. So every block on this page writes the JSON that the window would write, and runs it.
Worked example 1
In the app, you pick POST, type localhost:3000/gauges, choose Body, then raw, then JSON, and type { "name": "fan", "value": 12 }. Write the same request as JSON, and send it to the gauges server with Newman.
What we have: four choices made in the window: a method, an address with no scheme, a raw body and its language
What we need: the same request as an item of a collection, and the server's answer to it
The plan: match each choice with its field from the table, put the item in a collection, run it, and print the method, the address and the answer
- POST, from the method menu, becomes
method: "POST". - The address goes into
urljust as it was typed, with no scheme. - Body, raw and JSON become
body: its mode is raw, the text sits in raw, and json goes into options.raw.language. The language also sets the Content-Type header, as the menu does in the app.
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "add the fan",
request: {
method: "POST",
url: "localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "fan", "value": 12 }', options: { raw: { language: "json" } } },
},
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => {
console.log(args.request.method, args.request.url.toString(), "|", args.response.code, args.response.status);
console.log(args.response.text());
})
.on("done", () => server.close());
});
Terminal: node app.js
POST http://localhost:3000/gauges | 201 Created
{"name":"fan","value":12}
- The address printed begins with http://. Newman added it, exactly as the app's URL box would.
- The server answered 201 Created and sent back the fan it stored, so it read the body as JSON.
The item has method POST, url localhost:3000/gauges and a raw body with the language json; the server answers 201 Created with {"name":"fan","value":12}.
Check: The answer repeats the fan's name and value, which the server could only do if it read the body as JSON.
Another way: leave out the language and add a header yourself: header: [{ key: "Content-Type", value: "application/json" }]. Newman sends a header you set exactly as you wrote it.
A path that fails: typing the pair name=fan into the Params tab instead of the body. Params go into the URL as a query, so the server finds no JSON body and cannot store the fan.
When you are stuck: Where is it in the window?
Is it about what you send? Look along the request's tabs: Params, Authorization, Headers, Body, Scripts and Settings.
Is it about what came back? Look in the response pane: the status, time and size, then Body, Cookies, Headers and Test Results.
Is it something you kept? Look in the sidebar, under Collections, or under History for requests you sent but never saved.
Did something go wrong that the pane does not explain? Open the Console from the footer: it logs every request Postman sent.
Your turn
Which tab of a request writes request.header?Tap to reveal
- Each tab writes one part of the request.
- Headers are added in the tab with their name.
The Headers tab.
You type postman-echo.com/get into the URL box and press Send. To which address does Postman send the request?Tap to reveal
- The address has no scheme.
- Postman adds one for you.
To http://postman-echo.com/get: Postman puts http:// in front of an address with no scheme.
cat > app.js <<'END'
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [{ name: "read the pump", request: { method: "GET", url: "localhost:3000/gauges/pump" } }],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.request.url.toString(), "|", args.response.code, args.response.text()))
.on("done", () => server.close());
});
END
node app.js
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
http://localhost:3000/gauges/pump | 200 {"name":"pump","value":40}
- The address has no scheme, so Newman puts http:// in front.
- The pump is in the gauges object, so the server answers 200 with its name and value.
It prints http://localhost:3000/gauges/pump | 200 {"name":"pump","value":40}.
Did you know?
A side project from Bangalore
Postman began as a side project. Its own about page says that Abhinav Asthana set out to build a tool that would make testing APIs simpler. When the tool's use quickly exploded, Abhinav asked two former colleagues, Ankit Sobti and Abhijit Kane, to help create Postman, Inc. The company is now headquartered in San Francisco, and the same page notes that it was founded in Bangalore.
Why it fits here: It answers this topic's question: Postman began as one person's side project, and the company was founded in Bangalore.
Source: About Postman
From a Chrome app to Electron
Postman first ran inside Google Chrome, as a Chrome app with a helper extension called Interceptor. In March 2017, Postman's blog urged users to move to its native apps for Mac, Windows and Linux, which run on Electron, because Google had announced the end of Chrome apps on those systems. The native apps could also send headers such as Origin and User-Agent, which the Chrome app could not.
Why it fits here: It explains why Postman's window is a desktop app today: the first Postman lived inside Chrome, and moved to Electron when Chrome apps ended.
Source: Postman blog: Going native
In one line
Postman's window has a header, a sidebar of collections, a workbench of request tabs with the answer below each request, and a footer with the Console; every part of a request tab writes one field of the request's JSON.
A request, written down
At a glance
A saved request is a small piece of JSON, and reading the JSON is reading the request.
An item has a name and a request: the method, the url, a list of headers and a body. A raw body's language sets its Content-Type when no header does. The format also allows short forms, such as a request written as a plain URL, and a long form, a url written as an object of parts. Newman sends the collection's headers first, then adds its own, such as User-Agent, Host and Accept-Encoding.
Take a guess: In the header Accept-Encoding: gzip, deflate, br, what does br stand for?
A saved request is called an item. An item has a name, which is what the sidebar shows, and a request, which is what is sent. Here is the fan's request from the last section, written as an item, the way a collection file holds it:
One item of a collection
{
"name": "add the fan",
"request": {
"method": "POST",
"url": "http://localhost:3000/gauges",
"header": [],
"body": {
"mode": "raw",
"raw": "{ \"name\": \"fan\", \"value\": 12 }",
"options": { "raw": { "language": "json" } }
}
}
}
Read it from the top. name is the label you gave at Save. Inside request, method is the method menu, url is the URL box, header is the Headers tab, here an empty list, and body is the Body tab.
The body has three parts. mode says how the body is written; raw means plain text, typed as it is. raw is the text itself, a JSON object written inside a string. options.raw.language is the small language menu, which also decides the Content-Type header.
The gauges server, and a first run
Every block in this lesson asks one server: the Engine Hall's gauges, which you could have written in the APIs track. It keeps two readings, answers GET /gauges with all of them, GET /gauges/:name with one or a 404, and POST /gauges by storing a new reading. It is a module: it builds the app and exports it, and each block's app.js starts it.
Here is the smallest run: a collection with one item, run by Newman's library, which is the runner Postman publishes as an npm package:
server.js
const express = require("express");
const app = express();
app.use(express.json());
const gauges = { boiler: 95, pump: 40 };
app.get("/gauges", (req, res) => res.json(gauges));
app.get("/gauges/:name", (req, res) => {
if (!(req.params.name in gauges)) return res.status(404).json({ error: "no such gauge" });
res.json({ name: req.params.name, value: gauges[req.params.name] });
});
app.post("/gauges", (req, res) => {
gauges[req.body.name] = req.body.value;
res.status(201).json(req.body);
});
module.exports = app;
app.js
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{ name: "list gauges", request: { method: "GET", url: "http://localhost:3000/gauges" } },
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => {
console.log(args.item.name, "|", args.response.code, args.response.status);
console.log(args.response.text());
})
.on("done", () => server.close());
});
Terminal: node app.js
list gauges | 200 OK
{"boiler":95,"pump":40}
Walk through app.js line by line. require("newman") loads Newman, and require("./server") loads the gauges app. The collection object has an info with the collection's name, and an item list with one item.
app.listen(3000, ...) starts the server, and its callback runs once the server is listening. Only then does newman.run start, so the first request finds someone at the door.
.on("request", ...) is called once for each request, after its answer arrives. Its second argument, args, carries the item, the request as sent and the response. args.response.code is the status code, status is its reason phrase and text() is the body as text. Last, .on("done", ...) closes the server when the run ends; without it, the program would keep listening and never end.
One program, two sides
In the app, Postman is the client and some server answers it. On this page both live in one program: Express plays the server and Newman plays Postman. The request still travels the whole way, from Newman's client to the server's port and back, exactly as it would between two machines.
Worked example 2
Ask the server for two gauges by name, the boiler and the chimney, and print each status and body.
What we have: the gauges server, with a route GET /gauges/:name
What we need: two answers, one for each name
The plan: write two items whose urls end in the names, run the collection, and print the name, status and body from each request event
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{ name: "read the boiler", request: { method: "GET", url: "http://localhost:3000/gauges/boiler" } },
{ name: "read the chimney", request: { method: "GET", url: "http://localhost:3000/gauges/chimney" } },
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, args.response.status, "|", args.response.text()))
.on("done", () => server.close());
});
Terminal: node app.js
read the boiler | 200 OK | {"name":"boiler","value":95}
read the chimney | 404 Not Found | {"error":"no such gauge"}
- The boiler is one of the readings, so its answer is 200 OK with its name and value.
- The server has no chimney gauge, so it answered 404 Not Found with an error message in JSON.
- Newman did not stop at the 404. An answer of any status is still an answer; the run goes on to the next item.
read the boiler | 200 OK with its reading, then read the chimney | 404 Not Found with {"error":"no such gauge"}.
Check: Two items, two request events, in the order the items were written.
Another way: put the name in a query instead, as /gauges?name=boiler. That needs a server route that reads req.query; this server reads the path.
A path that fails: expecting Newman to report the 404 as a failure. A 404 is the server's honest answer, and the request did not fail; only a test, later in this lesson, can say that 404 was not what you wanted.
Short forms, and the long form
The format allows shorter and longer ways to write the same request. A request may be a plain string, which is its URL, asked with GET. A request with no method is sent as a GET too.
The url may also be an object of parts instead of a string: the protocol, the host, the port, the path, and the query as a list of pairs. It often carries a raw copy of the whole address as well:
A url written as its parts
"url": {
"raw": "http://localhost:3000/gauges?unit=C",
"protocol": "http",
"host": ["localhost"],
"port": "3000",
"path": ["gauges"],
"query": [{ "key": "unit", "value": "C" }]
}
Here is a detail worth knowing: Newman builds the address from the parts, and it does not read raw at all. A url object holding only raw is treated as an empty address. Each pair in query, and each header, may also carry "disabled": true, which keeps it in the file but leaves it out of the request. That is how a pair or a header is switched off without being deleted.
The headers that go out
You write some headers yourself, and Postman adds others. Which ones, and in what order? The best way to know is to ask the server, so this block's server prints every header it receives, by name, in the order they came. Only the value of Postman-Token is hidden, because it is a new random id on every request:
Worked example 3
Find out exactly which headers reach the server when the collection sets one header, Accept, and has a second one switched off.
What we have: a request with two headers, Accept: application/json and X-Hall: engine, the second marked disabled
What we need: the headers as the server receives them, in order
The plan: write a server whose route prints req.rawHeaders in pairs, run the collection against it, and read the list
app.js
const express = require("express");
const newman = require("newman");
const app = express();
app.get("/gauges", (req, res) => {
const raw = req.rawHeaders;
for (let i = 0; i < raw.length; i += 2) {
console.log(raw[i] + ":", raw[i] === "Postman-Token" ? "(a new random id)" : raw[i + 1]);
}
res.json({});
});
const collection = {
info: { name: "What the server sees" },
item: [
{
name: "list gauges",
request: {
method: "GET",
url: "http://localhost:3000/gauges",
header: [
{ key: "Accept", value: "application/json" },
{ key: "X-Hall", value: "engine", disabled: true },
],
},
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }).on("done", () => server.close());
});
Terminal: node app.js
Accept: application/json
User-Agent: PostmanRuntime/7.39.1
Cache-Control: no-cache
Postman-Token: (a new random id)
Host: localhost:3000
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
- The collection's own header comes first, written exactly as it was in the file.
- X-Hall is missing: a disabled header stays in the file and never goes out.
- Then come the headers that postman-runtime, the engine inside Newman, adds by itself. User-Agent names the engine and its version, 7.39.1. Cache-Control: no-cache asks any cache on the way to check with the server before it answers from a stored copy. Postman-Token is a fresh random id on every request.
- Host names the server and its port, as HTTP/1.1 requires. Accept-Encoding lists the ways of squeezing a body that the client can unpack: gzip, deflate and br. Connection: keep-alive asks the server to keep the connection open for the next request.
- There is no second Accept. Postman adds Accept: */* only when the collection has not set Accept itself.
Seven headers: Accept from the collection, then User-Agent, Cache-Control, Postman-Token, Host, Accept-Encoding and Connection from the runtime; the disabled X-Hall is not sent.
Check: Count them: one of yours, six of the runtime's, and Accept appears once.
Another way: in the app, select hidden at the top of the Headers tab: it lists the same added headers before you send.
A path that fails: judging from the collection file alone. The file lists one header that goes out, but the server receives seven.
A body's Content-Type works the same way: one you set in header wins, and without one, the body's language decides it. These were measured with Newman:
| options.raw.language | Content-Type sent |
|---|---|
| json | application/json |
| javascript | application/javascript |
| html | text/html |
| xml | application/xml |
| text, or none at all | text/plain |
When you are stuck: The request is not what I wrote
Print what the server receives, as the last block did: its view is the truth.
Is a header marked disabled? It stays in the file but is not sent.
Is the url an object? Then Newman reads its parts, not its raw copy; check the host, port and path.
Is a JSON body arriving as text/plain? Set its language to json, or add a Content-Type header.
Your turn
An item's request is the string "http://localhost:3000/gauges", with nothing else. Which method does Newman use?Tap to reveal
- The format says what a request written as a string means.
GET: a request written as a string is its URL, asked with GET.
A raw body has the language xml and no Content-Type header. Which Content-Type does the server receive?Tap to reveal
- The language decides it when no header is set.
- Look at the table.
application/xml.
cat > app.js <<'END'
const newman = require("newman");
const app = require("./server");
const collection = { info: { name: "Engine Hall gauges" }, item: [{ name: "read the chimney", request: "http://localhost:3000/gauges/chimney" }] };
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.request.method, args.response.code, args.response.text()))
.on("done", () => server.close());
});
END
node app.js
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
GET 404 {"error":"no such gauge"}
- The request is a plain string, so it is a GET.
- The server has no chimney, so it answers 404 with an error in JSON.
It prints GET 404 {"error":"no such gauge"}.
Did you know?
br is a small Swiss bread
The br in Accept-Encoding stands for Brotli, a way of squeezing data that Google introduced in September 2015. Its announcement says that, like Google's earlier Zopfli, it is named after a Swiss bakery product: Brötli means small bread in Swiss German. Google reported 20 to 26 percent better compression than Zopfli, and Brotli became RFC 7932 in July 2016.
Why it fits here: It answers this topic's question: br is Brotli, named after a Swiss German word for a small bread.
Sources: Google Open Source Blog: Introducing Brotli; RFC 7932: Brotli Compressed Data Format
No Host, no answer
Every HTTP/1.1 request must carry a Host header. RFC 9112, of June 2022, says that a client must send one, and that a server must answer 400 Bad Request to a request that lacks it. The reason is in RFC 9110: Host lets one server tell apart the resources of the many host names it serves, so many websites can share one machine.
Why it fits here: It explains one header Newman always adds: an HTTP/1.1 server must refuse a request that comes without Host.
Sources: RFC 9112: HTTP/1.1; RFC 9110: HTTP Semantics
In one line
A saved request is an item: a name, and a request with a method, a url, a list of headers and a body; Newman sends your headers first, then its own, and a raw body's language sets its Content-Type.
A first collection
At a glance
One saved request is handy. A box of them, kept in order, describes how to use an API.
A collection has a name and a list of items. Newman asks the items in the order they are written, one at a time, so a request can rely on the ones above it. In the app, Save and New Collection make one, and Export collection writes it as a JSON file in the Collection Format, version 2.1, after checking it for secrets. Newman can run that file by its path.
Take a guess: Is there a standard file format for describing a whole API, other than a Postman collection?
One saved request is useful. Several saved requests, kept together in the order you ask them, are far more useful: they are a description of how to use an API, which anyone can run again. That is a collection: a named box of items.
A collection has two parts that matter today. info holds its name, and item is the list of items. Newman asks the items in the order they are written, from the top down. It sends one request, waits for its answer, and only then sends the next. So a request may rely on what the requests before it did.
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{ name: "list gauges", request: { method: "GET", url: "http://localhost:3000/gauges" } },
{
name: "add the fan",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "fan", "value": 12 }', options: { raw: { language: "json" } } },
},
},
{ name: "read the fan", request: { method: "GET", url: "http://localhost:3000/gauges/fan" } },
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, "|", args.response.text()))
.on("done", () => server.close());
});
Terminal: node app.js
list gauges | 200 | {"boiler":95,"pump":40}
add the fan | 201 | {"name":"fan","value":12}
read the fan | 200 | {"name":"fan","value":12}
Read the three lines with the server in mind. The list showed the two readings that the server starts with. The POST stored a third reading, the fan. The last GET found the fan, because the POST had already happened. Three requests, in order, each one seeing the work of the ones above it.
In the app
The app builds the same thing with a few clicks. Press Save on a request tab, choose New Collection, give it a name, and save: the collection appears under Collections in the sidebar, with the request inside it. Next to a collection in the sidebar is an Add request button for the next one.
To take a collection out of the app, find it under Collections, open its View more actions menu, choose More, then Export collection. Postman checks the collection for secrets, such as passwords or keys typed into it, and then saves it as a JSON file in the Collection Format, version 2.1. That file is what Newman reads.
Keep secrets out of collections
A collection is meant to be shared, so a password typed into a header travels with it to everyone who gets the file. That is why the export looks for secrets first. Lesson 03 shows where such values belong instead: in variables kept outside the collection.
Worked example 4
Show that the order of a collection matters: put "read the fan" first, above the list and the POST, and run the collection on a fresh server.
What we have: the same three items as above, in a new order: read the fan, list gauges, add the fan
What we need: the three answers, and the reason the first one changed
The plan: move the item to the top of the list, run, and compare each answer with the run above
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{ name: "read the fan", request: { method: "GET", url: "http://localhost:3000/gauges/fan" } },
{ name: "list gauges", request: { method: "GET", url: "http://localhost:3000/gauges" } },
{
name: "add the fan",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "fan", "value": 12 }', options: { raw: { language: "json" } } },
},
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, "|", args.response.text()))
.on("done", () => server.close());
});
Terminal: node app.js
read the fan | 404 | {"error":"no such gauge"}
list gauges | 200 | {"boiler":95,"pump":40}
add the fan | 201 | {"name":"fan","value":12}
- "read the fan" ran first, before any fan existed, so the server answered 404 with its error.
- The list then showed only the two starting readings, because the fan was still not added.
- The POST came last; it stored the fan, but nothing after it asked for the fan.
404 for the fan, then the two starting readings, then 201 for the POST: the same requests, but in the wrong order for reading the fan.
Check: Each server starts fresh with its two readings, so the only thing that changed between the runs is the order.
Another way: keep the order, and name the items so that the order reads like a story: list, add, read.
A path that fails: assuming the requests run at the same time. Newman asks one at a time, in order; that is why the first run could read the fan at all.
A collection in its own file
A collection is usually kept as a file, not typed inside a program. Then the same file can be run by Newman, opened in the app or shared with a friend. Here is the collection above as a file, with one more line in info: schema, the address of the format's own description, which names its version, 2.1.
Newman accepts a file's path wherever it accepts a collection object. It reads the file itself:
gauges.postman_collection.json
{
"info": {
"name": "Engine Hall gauges",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{ "name": "list gauges", "request": { "method": "GET", "url": "http://localhost:3000/gauges" } },
{
"name": "add the fan",
"request": {
"method": "POST",
"url": "http://localhost:3000/gauges",
"body": { "mode": "raw", "raw": "{ \"name\": \"fan\", \"value\": 12 }", "options": { "raw": { "language": "json" } } }
}
},
{ "name": "read the fan", "request": { "method": "GET", "url": "http://localhost:3000/gauges/fan" } }
]
}
app.js (server.js is the gauges server; the collection file is shown above)
const newman = require("newman");
const app = require("./server");
const server = app.listen(3000, () => {
newman.run({ collection: "./gauges.postman_collection.json" })
.on("start", () => console.log("start"))
.on("request", (err, args) => console.log(args.item.name, "|", args.request.method, args.response.code))
.on("done", () => {
console.log("done");
server.close();
});
});
Terminal: node app.js
start
list gauges | GET 200
add the fan | POST 201
read the fan | GET 200
done
The run is the same as before; only the place the collection lives has changed. Notice the start and done lines around it: they come from two events of the run, which the next section looks at closely.
Worked example 5
Change the pump's reading to 41 with one request, then read it back with a second one, written in the short form.
What we have: the gauges server, whose POST /gauges stores a name and value, so posting the pump again replaces its reading
What we need: a collection whose second answer shows the new reading
The plan: a POST with the JSON body { "name": "pump", "value": 41 }, then a GET written as a plain string
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "set the pump",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "pump", "value": 41 }', options: { raw: { language: "json" } } },
},
},
{ name: "read the pump", request: "http://localhost:3000/gauges/pump" },
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, "|", args.response.text()))
.on("done", () => server.close());
});
Terminal: node app.js
set the pump | 201 | {"name":"pump","value":41}
read the pump | 200 | {"name":"pump","value":41}
- The POST stored 41 under the name pump, replacing the 40 the server started with.
- The GET, written as a plain string, is a GET of that address; it ran second, so it saw 41.
set the pump | 201, then read the pump | 200 | {"name":"pump","value":41}.
Check: The value read back is the value that was sent, 41, not the starting 40.
A path that fails: writing the GET first. It would show 40, and the POST after it would change a reading that no request then reads.
When you are stuck: The answers are not what the order should give
Read the item list from the top: that is the order of the run, one request at a time.
Does a request rely on an earlier one? Then the earlier one must be written above it.
Did each run start from a fresh server? Every block here starts its own, so readings never carry over from one run to the next.
Your turn
A collection holds two items: "read the fan", then "add the fan". On a fresh gauges server, what status does "read the fan" get?Tap to reveal
- The items run in the order written.
- When "read the fan" runs, has any fan been added?
404: the fan is added only after it is read.
After you press Save and choose New Collection, where in the app does the new collection appear?Tap to reveal
- Saved things live in one of the four areas of the window.
In the sidebar, under Collections, with the request inside it.
cat > app.js <<'END'
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "set the pump",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "pump", "value": 42 }', options: { raw: { language: "json" } } },
},
},
{ name: "read the pump", request: "http://localhost:3000/gauges/pump" },
],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, "|", args.response.text()))
.on("done", () => server.close());
});
END
node app.js
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
set the pump | 201 | {"name":"pump","value":42}
read the pump | 200 | {"name":"pump","value":42}
- The POST replaces the pump's reading with 42.
- The GET runs after it, so it reads 42.
It prints set the pump | 201 and read the pump | 200, both with {"name":"pump","value":42}.
Did you know?
OpenAPI began as Swagger
A widely used format for describing a whole API is the OpenAPI Specification. Its own FAQ says that it was based on the Swagger 2.0 specification, renamed, which SmartBear Software donated in 2015. In November 2015, SmartBear, Google, IBM, Microsoft, PayPal and five other companies formed the OpenAPI Initiative, an open source project under the Linux Foundation, to look after it.
Why it fits here: It answers this topic's question: the OpenAPI Specification, which began as Swagger, describes a whole API in one file.
Source: OpenAPI Initiative: FAQ
Collections as plain text
Not every API client keeps collections the way Postman does. Bruno, an open source tool for exploring and testing APIs, stores each collection directly in a folder on your computer, written in a plain text language of its own called Bru, so that a team can share collections through Git. Its README also promises that Bruno is offline only, with no plans to add cloud sync, ever.
Why it fits here: It shows another way to keep this topic's collections: plain text files in a folder, shared through Git.
Source: Bruno on GitHub
In one line
A collection is a named list of items, kept as a JSON file; Newman asks the items in the order they are written, one at a time, so a request can rely on the ones above it.
Running a collection with Newman
At a glance
Newman runs a collection inside your own program, and tells you everything as it happens.
newman.run takes the options, such as the collection, and gives back an event emitter. Events fire in a fixed order: start, then for each item beforeItem, request, assertion and item, and done at the end. The callback then receives the summary, whose run.stats counts requests and tests, and whose run.failures explains each failure. A test, pm.test with a check inside, turns an answer into passed or failed.
Take a guess: Where does the name Newman come from?
Newman is Postman's collection runner, published as an npm package. You have already called it: newman.run(options, callback). This section looks at what it gives back, and adds the first test: a check that turns an answer into passed or failed.
What newman.run takes and gives
The options object names what to run. collection is a collection object or the path of its file. Other options come in the next lessons: an environment, a number of rounds, and reporters. In library mode, Newman prints nothing of its own unless you ask for a reporter, which is why every line on this page comes from our own console.log.
newman.run gives back an event emitter, the kind of object you met in the Node track: it calls your listeners as things happen. Each listener receives two arguments, an error or null, and an object of details. The run starts a moment after newman.run returns, so listeners added on the next lines are in time. Here is every event of a run with one request, in the order they fire:
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [{ name: "list gauges", request: "http://localhost:3000/gauges" }],
};
const names = ["start", "beforeIteration", "beforeItem", "beforePrerequest", "prerequest", "beforeRequest",
"request", "beforeTest", "test", "item", "iteration", "beforeDone", "done"];
const server = app.listen(3000, () => {
const run = newman.run({ collection });
for (const name of names) run.on(name, () => console.log(name));
run.on("done", () => server.close());
});
Terminal: node app.js
start
beforeIteration
beforeItem
beforePrerequest
prerequest
beforeRequest
request
beforeTest
test
item
iteration
beforeDone
done
The names come in pairs: a before event, then the event itself. Read them as a story. The run starts, and its first round, an iteration, begins. The item begins. Its pre-request script would run here; there is none, but the events still fire. The request goes out and its answer arrives: that is the request event. The item's test script would run next. The item ends, the round ends, and the run is done.
| Event | Fires when | Useful details |
|---|---|---|
| start | the run begins | nothing yet |
| beforeItem | an item is about to begin | args.item, with its name |
| request | an answer arrived, or the request failed | err for a failed request; args.request, args.response |
| assertion | a test finished | args.assertion, the test's name; err if it failed |
| item | an item is complete | args.item |
| done | the run is over | the summary |
After the done event, Newman calls the callback, the second argument of newman.run, with an error or null and the summary. The summary's run.stats counts what happened: requests, tests and scripts, each with a total and a number failed. run.failures lists every failure in detail.
Worked example 6
Count the requests of a run, and how many of them failed, from the summary.
What we have: three requests: the list, the boiler and the chimney, which the server does not have
What we need: two counts: the requests sent, and the requests that failed
The plan: run the collection with a callback, and print run.stats.requests and run.stats.assertions from its summary
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{ name: "list gauges", request: "http://localhost:3000/gauges" },
{ name: "read the boiler", request: "http://localhost:3000/gauges/boiler" },
{ name: "read the chimney", request: "http://localhost:3000/gauges/chimney" },
],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
const { requests, assertions } = summary.run.stats;
console.log("requests:", requests.total, "| failed:", requests.failed);
console.log("tests:", assertions.total, "| failed:", assertions.failed);
server.close();
});
});
Terminal: node app.js
requests: 3 | failed: 0
tests: 0 | failed: 0
- Three requests were sent, and none of them failed. The chimney's 404 is an answer, and a request fails only when no answer comes at all, as when no server listens at the address.
- There were no tests, so the test counts are zero: nothing checked whether a 404 was welcome.
requests: 3 | failed: 0, and tests: 0 | failed: 0.
Check: The total of requests equals the number of items, 3, because each item sent one request.
A path that fails: reading requests.failed as "answers that were not 200". It counts requests that got no answer at all; judging an answer is a test's job.
A first test
A test is a few lines of JavaScript that run after the answer arrives and say whether it was the answer you wanted. In the app, open the request's Scripts tab, choose Post-response, and pick the snippet "Status code: Code is 200". Postman writes the test for you, and after Send, the response pane's Test Results tab shows whether it passed.
In a collection, a test is part of the item's event list: an event that listens for "test", with a script whose exec holds the lines of JavaScript. One line is enough:
One test
pm.test("status is 200", () => pm.response.to.have.status(200));
pm is Postman's toolbox inside a script. pm.test(name, fn) runs fn; if fn throws, the test failed, and if it returns, the test passed. pm.response is the answer, and pm.response.to.have.status(200) throws when its status is not 200. Lesson 04 is all about tests; one is enough to see a run pass and fail:
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "read the boiler",
request: { method: "GET", url: "http://localhost:3000/gauges/boiler" },
event: [
{ listen: "test", script: { exec: ['pm.test("status is 200", () => pm.response.to.have.status(200));'] } },
],
},
{
name: "read the chimney",
request: { method: "GET", url: "http://localhost:3000/gauges/chimney" },
event: [
{ listen: "test", script: { exec: ['pm.test("status is 200", () => pm.response.to.have.status(200));'] } },
],
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
const stats = summary.run.stats;
console.log("tests:", stats.assertions.total, "| failed:", stats.assertions.failed);
server.close();
})
.on("beforeItem", (err, args) => console.log("→", args.item.name))
.on("assertion", (err, args) => console.log(err ? " FAIL: " + args.assertion + " | " + err.message : " pass: " + args.assertion));
});
Terminal: node app.js
→ read the boiler
pass: status is 200
→ read the chimney
FAIL: status is 200 | expected response to have status code 200 but got 404
tests: 2 | failed: 1
The boiler's test passed, so the assertion event came with err null. The chimney's test failed, and its err carries the message: the status should have been 200, and it was 404. The summary counted two tests and one failure. The same 404 that the last example called a fine answer is now a failure, because a test said it should have been 200.
Worked example 7
List every failure of the run above: which item, which test, where, and why.
What we have: the two-item collection with a status test on each
What we need: for each failure: the item's name, the test's name, the place it happened and the message
The plan: loop over summary.run.failures in the callback and print source.name, error.test, at and error.message
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "read the boiler",
request: { method: "GET", url: "http://localhost:3000/gauges/boiler" },
event: [
{ listen: "test", script: { exec: ['pm.test("status is 200", () => pm.response.to.have.status(200));'] } },
],
},
{
name: "read the chimney",
request: { method: "GET", url: "http://localhost:3000/gauges/chimney" },
event: [
{ listen: "test", script: { exec: ['pm.test("status is 200", () => pm.response.to.have.status(200));'] } },
],
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
for (const failure of summary.run.failures) {
console.log(failure.source.name, "|", failure.error.test, "|", failure.at);
console.log(" " + failure.error.message);
}
server.close();
});
});
Terminal: node app.js
read the chimney | status is 200 | assertion:0 in test-script
expected response to have status code 200 but got 404
source.nameis the item where it happened;error.testis the test's name.atsays where: the first test, numbered from 0, of the item's test script. A second failing test in the same script would be assertion:1.error.messageis the message of the check that failed.
One failure: read the chimney | status is 200 | assertion:0 in test-script, because expected response to have status code 200 but got 404.
Check: The summary counted one failed test, and the list holds exactly one failure.
Another way: print the same facts from the assertion event as the run goes, as the block before this one did; the summary gives them all together at the end.
When you are stuck: My listener never prints
Is the event's name spelled exactly, with its capitals? beforeItem, not beforeitem.
Did you add the listener to what newman.run returned? Chain .on after the call, or keep the return value in a variable.
Is the callback the second argument of newman.run, not an .on listener? The callback runs once, after done.
Your turn
Which event tells you that a test finished, and how do you know whether it failed?Tap to reveal
- Tests are assertions.
- Every listener gets an error or null first.
The assertion event; its first argument, err, is null when the test passed and the error when it failed.
A run asks for a gauge that does not exist and has no tests. What are requests.failed and assertions.failed in its summary?Tap to reveal
- A 404 is an answer.
- No tests ran.
Both are 0: the request got an answer, and no test judged it.
cat > app.js <<'END'
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "read the pump",
request: "http://localhost:3000/gauges/pump",
event: [{ listen: "test", script: { exec: ['pm.test("found", () => pm.response.to.have.status(200));'] } }],
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
console.log("failures:", summary.run.failures.length);
server.close();
}).on("assertion", (err, args) => console.log(args.assertion, err ? "failed" : "passed"));
});
END
node app.js
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
found passed
failures: 0
- The pump exists, so the answer is 200.
- The test checks for 200, so it passes, and there are no failures.
It prints found passed, then failures: 0.
Did you know?
Named after a mail carrier
Newman shares its name with a character in the American comedy Seinfeld: Newman, an employee of the United States Postal Service, and so a postman. In March 2017, someone asked on Newman's GitHub page whether the name was a reference to Seinfeld. A contributor to the project answered on 3 April 2017: Yes, it is.
Why it fits here: It answers this topic's question: Postman's runner is named after Newman, the postal worker in Seinfeld.
Sources: Newman issue 963: Is this a reference to Seinfeld?; Wikipedia: Newman (Seinfeld)
Rewritten as a library first
The Newman you call as a library is not its first design. Newman's migration guide says that version 3.0, published on npm in August 2016, was a complete rewrite from the ground up, written with a library-first mindset. Since then, Newman runs collections with Postman Runtime, to give the same results in the Postman apps and on the command line.
Why it fits here: It explains this topic's library mode: since version 3.0, Newman has been written as a library first.
Sources: Newman's migration guide; The npm registry's record of newman
In one line
newman.run(options, callback) runs a collection and gives back an event emitter: request fires for each answer, assertion for each test, and the callback receives the summary, whose run.stats counts and run.failures explain what failed.
Newman on the command line
At a glance
On a build server, nobody clicks Send. Newman's command line runs the whole collection instead.
npm installs Newman, and newman run with a collection file runs it and prints a report. Each request shows its status, size and time, and each test shows a tick or a number that points to the failure table. A table counts what ran and what failed. The exit code is 1 when anything failed, so a build can stop. Postman now recommends the Postman CLI for new work.
Take a guess: Who coined the term continuous integration, for the build servers that run collections on every change?
So far Newman has run inside our own programs, as a library. Its main home is the command line: one command runs a collection file and prints a report. That is how collections run on build servers, where nobody clicks Send. This section installs Newman, runs the collection file from the last section with tests added, and reads every line of the report.
Installing Newman
Newman is an npm package, and it needs Node.js 16 or later. Postman's own page installs it for every folder with npm install -g newman. Here it was installed into one folder, at the exact version this course runs, in a real terminal:
Terminal, measured once in a real terminal on 2026-11-20 (npm needs the network, which no page has)
asha@tower:~/shop$ npm install newman@6.2.3
npm warn deprecated har-validator@5.1.5: this library is no longer supported
npm warn deprecated uuid@3.4.0: uuid@10 and below is no longer supported. For ESM codebases, update to uuid@latest. For CommonJS codebases, use uuid@11 (but be aware this version will likely be deprecated in 2028).
npm warn deprecated uuid@8.3.2: uuid@10 and below is no longer supported. For ESM codebases, update to uuid@latest. For CommonJS codebases, use uuid@11 (but be aware this version will likely be deprecated in 2028).
npm warn deprecated @faker-js/faker@5.5.3: Please update to a newer version.
added 148 packages, and audited 149 packages in 7s
21 packages are looking for funding
run `npm fund` for details
19 vulnerabilities (6 moderate, 12 high, 1 critical)
To address issues that do not require attention, run:
npm audit fix
To address all issues (including breaking changes), run:
npm audit fix --force
Run `npm audit` for details.
Read it from the top. The four lines that begin npm warn deprecated name packages that Newman depends on, whose authors have stopped supporting them. Then npm added 148 packages: Newman, and everything it needs. The audit found 19 known vulnerabilities in those packages and suggests npm audit fix.
These warnings will stay. Postman's own Newman page says that Newman "won't receive updates or fixes". For running your own collections on your own machine, against your own servers, that is a known and accepted cost; the end of this section says what Postman suggests for new work.
A run, and its report
The collection file below is the last section's three requests, with a status test on each. The third item asks for the chimney, which the gauges server does not have, and its test expects 200, so one test will fail on purpose.
The gauges server was started in the background first, with node server.js &. Then one command ran the file. npx runs a command that a package installed in the folder, and --color off asks for plain text, so that the report can be shown here exactly as it came:
gauges.postman_collection.json
{
"info": { "name": "Engine Hall gauges", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" },
"item": [
{ "name": "list gauges", "request": { "method": "GET", "url": "http://localhost:3000/gauges" },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 200\", () => pm.response.to.have.status(200));" ] } } ] },
{ "name": "add the fan", "request": { "method": "POST", "url": "http://localhost:3000/gauges",
"header": [ { "key": "Content-Type", "value": "application/json" } ], "body": { "mode": "raw", "raw": "{ \"name\": \"fan\", \"value\": 12 }" } },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 201\", () => pm.response.to.have.status(201));" ] } } ] },
{ "name": "read the chimney", "request": { "method": "GET", "url": "http://localhost:3000/gauges/chimney" },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 200\", () => pm.response.to.have.status(200));" ] } } ] }
]
}
Terminal, measured on 2026-11-20 in the kit's sandbox: real Newman 6.2.3, Node 22.23.1 and Express 5.3.0
asha@tower:~/shop$ npx newman run gauges.postman_collection.json --color off
newman
Engine Hall gauges
→ list gauges
GET http://localhost:3000/gauges [200 OK, 258B, 34ms]
✓ status is 200
→ add the fan
POST http://localhost:3000/gauges [201 Created, 265B, 12ms]
✓ status is 201
→ read the chimney
GET http://localhost:3000/gauges/chimney [404 Not Found, 267B, 4ms]
1. status is 200
┌─────────────────────────┬──────────────────┬──────────────────┐
│ │ executed │ failed │
├─────────────────────────┼──────────────────┼──────────────────┤
│ iterations │ 1 │ 0 │
├─────────────────────────┼──────────────────┼──────────────────┤
│ requests │ 3 │ 0 │
├─────────────────────────┼──────────────────┼──────────────────┤
│ test-scripts │ 3 │ 0 │
├─────────────────────────┼──────────────────┼──────────────────┤
│ prerequest-scripts │ 0 │ 0 │
├─────────────────────────┼──────────────────┼──────────────────┤
│ assertions │ 3 │ 1 │
├─────────────────────────┴──────────────────┴──────────────────┤
│ total run duration: 128ms │
├───────────────────────────────────────────────────────────────┤
│ total data received: 73B (approx) │
├───────────────────────────────────────────────────────────────┤
│ average response time: 16ms [min: 4ms, max: 34ms, s.d.: 12ms] │
└───────────────────────────────────────────────────────────────┘
# failure detail
1. AssertionErr… status is 200
expected response to have status code 200 but got 404
at assertion:0 in test-script
inside "read the chimney"
asha@tower:~/shop$ echo "exit: $?"
exit: 1
Read the report from the top. It opens with newman and the collection's name. Each item then gets an arrow and its name, then its request on one line: the method, the address, and in brackets the status, the size of the answer in bytes and the time it took. Under the request come its tests: a tick for each one that passed, and a number for each one that failed. The number points into the table of failures at the bottom.
The big table counts what was executed and what failed: 1 iteration, 3 requests, 3 test scripts, no pre-request scripts, and 3 assertions, which are the tests, with 1 failed. Under it come the run's total duration, the data received and the average response time, with the fastest, the slowest and the s.d., the standard deviation, a measure of how spread out the times were.
Last comes the table of failures. Failure 1 is an AssertionError, its name cut short to fit the column. It belongs to the test status is 200, its message says the status was 404, it happened at assertion:0 in test-script, and it was inside "read the chimney".
The times change on every run
34ms, 12ms and 128ms are what this one run measured. Run it again and the times differ, because the machine is never exactly as busy twice. That is why the blocks on this page never print a time: a block must print the same thing every time it runs.
The exit code
The last command of the session printed the exit code of newman run: 1. Newman's command sets the exit code to 1 whenever its run has any failure, such as a failed test or a request that got no answer, and leaves it at 0 when everything passed. If you need a run to end with 0 anyway, the -x option, in full --suppress-exit-code, keeps it at 0. The --bail option stops the run at the first failure instead of finishing it.
Build servers read the exit code: 0 lets a build go on, and anything else stops it. So one failed test in a collection can stop a release, which is the whole point of running collections there.
Worked example 8
A teammate runs the collection on a build server, sees the report above and exit code 1, and asks you what broke. Answer from the report alone.
What we have: the measured report and its exit code
What we need: which request failed, which test, why, and what the build server will do
The plan: start from the failure table, follow its number back to its item, and read the exit code
- The failure table holds one row, failure 1: the test status is 200.
- Its detail says expected response to have status code 200 but got 404, inside "read the chimney".
- Back in the item list, the chimney's request line shows [404 Not Found, ...], and under it the 1. that points to this failure.
- The other two items show ticks, so the list and the POST behaved as their tests expected.
- The exit code is 1, so the build server stops the build.
"read the chimney" failed its test status is 200, because the server answered 404: it has no chimney gauge. The exit code 1 stops the build.
Check: The summary table agrees: 3 assertions, 1 failed.
Another way: decide which side is wrong. If the chimney gauge should exist, the server needs fixing; if it should not, the test should expect 404.
A path that fails: blaming the server because a request failed. requests shows 0 failed in the table: every request got an answer. Only the test disagreed with that answer.
The same collection runs on this page with Newman's library. The page cannot run a server in the background and a command beside it, so this program starts the server and the run together, as every block here does, and prints the same facts without the times and sizes:
gauges.postman_collection.json
{
"info": { "name": "Engine Hall gauges", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" },
"item": [
{ "name": "list gauges", "request": { "method": "GET", "url": "http://localhost:3000/gauges" },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 200\", () => pm.response.to.have.status(200));" ] } } ] },
{ "name": "add the fan", "request": { "method": "POST", "url": "http://localhost:3000/gauges",
"header": [ { "key": "Content-Type", "value": "application/json" } ], "body": { "mode": "raw", "raw": "{ \"name\": \"fan\", \"value\": 12 }" } },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 201\", () => pm.response.to.have.status(201));" ] } } ] },
{ "name": "read the chimney", "request": { "method": "GET", "url": "http://localhost:3000/gauges/chimney" },
"event": [ { "listen": "test", "script": { "exec": [ "pm.test(\"status is 200\", () => pm.response.to.have.status(200));" ] } } ] }
]
}
app.js (server.js is the gauges server; the collection file is shown above)
const newman = require("newman");
const app = require("./server");
const server = app.listen(3000, () => {
newman.run({ collection: "./gauges.postman_collection.json" }, (err, summary) => {
const failures = summary.run.failures.length;
console.log("failures:", failures, "| exit code:", failures ? 1 : 0);
server.close();
})
.on("request", (err, args) => console.log("→", args.item.name, "|", args.request.method, args.response.code, args.response.status))
.on("assertion", (err, args) => console.log(err ? " ✗ " + args.assertion : " ✓ " + args.assertion));
});
Terminal: node app.js
→ list gauges | GET 200 OK
✓ status is 200
→ add the fan | POST 201 Created
✓ status is 201
→ read the chimney | GET 404 Not Found
✗ status is 200
failures: 1 | exit code: 1
Newman today, and the Postman CLI
Postman's own Newman page still calls Newman its command-line collection runner, for collections in the v2.1 format. But it also says that Newman won't receive updates or fixes. It recommends the Postman CLI for new work instead, installed with npm install -g postman-cli. The Postman CLI runs every collection format, including the newer v3, and supports most of Newman's options with the same syntax.
This course runs Newman on the page because it is open source, it runs inside one program, and it reads v2.1 files without any account or network. What you learn here carries over: collections, requests, variables, scripts and tests are the same ideas in the app, in Newman and in the Postman CLI.
When you are stuck: The command line run does not work
Is the server running? Newman asks a real address; start the server first, in the background or in a second terminal.
Is the file name right? newman run takes the path of the collection file, as exported.
Do you see exit code 1 but expected 0? Read the failure table: one failed test or one request without an answer is enough.
Your turn
In the report, a request line ends in [201 Created, 265B, 12ms]. What are the three parts?Tap to reveal
- A status, a size and a time.
The status, 201 Created; the size of the answer, 265 bytes; and the time it took, 12 milliseconds.
Which exit code does newman run give when every test passes, and which when one fails?Tap to reveal
- Build servers read it to decide whether to go on.
0 when everything passes, 1 when there is any failure.
Why does no block on this page print a time?Tap to reveal
- What does the tip box say about running the same command twice?
Because times change on every run, and a block must print the same output every time.
Did you know?
From newman -c to newman run
Old tutorials run Newman as newman -c collection.json. Newman's migration guide explains the change: since version 3.0, collection runs happen with the run command, and the -c option was deprecated in favour of passing the file's path alone. Version 4.0, published in August 2018, then dropped all the deprecated options of version 2, so the old form no longer works.
Why it fits here: It helps with this topic's command: a tutorial that shows newman -c was written before version 3.0.
Sources: Newman's migration guide; The npm registry's record of newman
Who named continuous integration
Build servers that run every change through its tests practise continuous integration. Martin Fowler's article on it, in its version of 18 January 2024, says the practice was developed by Kent Beck as part of Extreme Programming in the 1990s. Some people credit Grady Booch with coining the term, but Fowler notes that Booch used the phrase only as an offhand description, in a single sentence of a book.
Why it fits here: It answers this topic's question: Kent Beck developed the practice, and Grady Booch used the phrase once, in passing.
In one line
newman run collection.json runs a collection from the command line and prints a report. It shows each request with its status, size and time, and each test with a tick or a number, then tables of counts and failures. Its exit code is 1 when anything failed.
Where it is used
Postman sits beside almost every API that people build. Here is where each idea of this lesson does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| the request tab and Send | building an API, one route at a time | trying a new route by hand before any page calls it |
| collections | describing how to use an API | a team's collection of every route of its API, shared with everyone who calls it |
| the collection file, format 2.1 | moving work between people and tools | a collection kept in git beside the server's code |
| Newman, and now the Postman CLI | build servers | a build that stops when a request breaks |
| tests and the exit code | catching a change that broke something | a status test that fails the day a route starts answering 404 |
To practise against a server you did not write, Postman runs a service for exactly that, Postman Echo: its own quick start sends a first request to postman-echo.com/get. And many public APIs publish a collection of their own, so that a new user can try every route in minutes instead of reading every page of the documentation first.
In the Testing House, a card in the box is an item, the box is the collection, and the runner who carries it across the road is Newman.
Mistakes and debugging
These are the mistakes people make most in their first collections, each with the way to see it and the fix.
1. A JSON body sent as text
A raw body with no language goes out as text/plain, and Express's JSON reader skips it. This block's server prints the type and the body it received, for the same body sent twice, once without a language and once with json:
app.js
const express = require("express");
const newman = require("newman");
const app = express();
app.use(express.json());
app.post("/gauges", (req, res) => {
console.log("type:", req.headers["content-type"], "| body:", req.body);
res.status(201).json({});
});
const collection = {
info: { name: "Two parcels" },
item: [
{
name: "no language",
request: { method: "POST", url: "http://localhost:3000/gauges", body: { mode: "raw", raw: '{ "name": "fan" }' } },
},
{
name: "json",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "fan" }', options: { raw: { language: "json" } } },
},
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }).on("done", () => server.close());
});
Terminal: node app.js
type: text/plain | body: undefined
type: application/json | body: { name: 'fan' }
Without a language, the body arrived as text/plain, and req.body stayed undefined: express.json() reads only JSON. With the language json, the same text arrived as application/json and became an object. Set the language to json, or add a Content-Type header, whenever a body is JSON.
2. Asking at the wrong port
A request to a port where nothing listens gets no answer at all. This one asks port 3001, while the server listens on 3000:
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [{ name: "list gauges", request: "http://localhost:3001/gauges" }],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
console.log("requests failed:", summary.run.stats.requests.failed);
server.close();
}).on("request", (err, args) => console.log(args.item.name, "|", err ? err.code : args.response.code));
});
Terminal: node app.js
list gauges | ECONNREFUSED
requests failed: 1
The request event's err is set, with the code ECONNREFUSED: the connection was refused, because no program was listening there. Unlike a 404, this is a failed request, and the summary counts it. Check the port, and check that the server was started before the run.
3. A url object with only raw
When you write url as an object, Newman builds the address from its parts. An object holding only the raw copy has no parts, so the address is empty:
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [{ name: "list gauges", request: { method: "GET", url: { raw: "http://localhost:3000/gauges" } } }],
};
const server = app.listen(3000, () => {
newman.run({ collection })
.on("request", (err, args) => console.log(args.item.name, "|", err ? err.message : args.response.code))
.on("done", () => server.close());
});
Terminal: node app.js
list gauges | runtime:extensions~request: request url is empty
Write url as a plain string, or give the object its parts: protocol, host, port and path.
4. A body on a GET
A GET reads; it carries no parcel. Newman does not send a body written on a GET at all: measured with Newman, the server received neither the body nor a Content-Type. If a request must carry data, it is usually a POST, PUT or PATCH; if a GET needs a detail, put it in the URL's path or query.
5. A program that never ends
A block that starts a server must close it. Without server.close() in the done listener or the callback, the run finishes, the report is printed, and the program keeps listening for ever.
6. Reading a 404 as a failed request
A 404 is an answer: the request did not fail, and requests.failed stays at 0. If a 404 is wrong for that item, say so with a test, and the test fails instead.
The debugging corner: ask the server what it saw
When a request does not do what you meant, print what the server receives, as the request section's block did: the method, the address, every header and the body. The server's view is the truth.
In the app, the Console, opened from the footer, logs every request Postman sends, with all of its headers, so you can compare what went out with what you meant to send.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
app.js (server.js is the gauges server from the request section)
const newman = require("newman");
const app = require("./server");
const collection = {
info: { name: "Engine Hall gauges" },
item: [
{
name: "add the fan",
request: {
method: "POST",
url: "http://localhost:3000/gauges",
body: { mode: "raw", raw: '{ "name": "fan", "value": 12 }', options: { raw: { language: "json" } } },
},
event: [{ listen: "test", script: { exec: ['pm.test("created", () => pm.response.to.have.status(201));'] } }],
},
{
name: "read the fan",
request: "http://localhost:3000/gauges/fan",
event: [{ listen: "test", script: { exec: ['pm.test("fan is 12", () => pm.expect(pm.response.json().value).to.equal(12));'] } }],
},
],
};
const server = app.listen(3000, () => {
newman.run({ collection }, (err, summary) => {
const s = summary.run.stats;
console.log("requests:", s.requests.total, "| tests:", s.assertions.total, "| failed:", s.assertions.failed);
server.close();
})
.on("request", (err, args) => console.log(args.item.name, "|", args.response.code, args.response.status))
.on("assertion", (err, args) => console.log(err ? " FAIL: " + args.assertion : " pass: " + args.assertion));
});
Terminal: node app.js
add the fan | 201 Created
pass: created
read the fan | 200 OK
pass: fan is 12
requests: 2 | tests: 2 | failed: 0
| When you see | It means | Do |
|---|---|---|
| type: text/plain on the server for a JSON body | the body has no language and no Content-Type | set options.raw.language to json |
| ECONNREFUSED in the request event | nothing listens at that address | start the server; check the port |
| request url is empty | url is an object with only raw | write url as a string, or give its parts |
| a 404, and requests failed 0 | an answer, not a failed request | add a test if the 404 is wrong |
| expected response to have status code 200 but got 404 | a status test failed | read the failure's item name and message |
| exit code 1 from newman run | at least one failure in the run | read the failure table at the bottom |
| a program that never ends | the server is still listening | close it in done or in the callback |
The story: The box of questions
By evening the loose slips are gone. In their place stands one box with the Engine Hall's name on its lid, and inside it three cards in order, each a whole question: the method, the address, the headers and the parcel. Each card ends with a small check: the answer should be this.
A runner carries the box across the road, asks each question in turn, and comes back with a report: two checks passed, one failed, and the chimney has no gauge at all.
The Postmaster reads the report and nods. "Now the questions outlive the clerk who wrote them," she says. "Tomorrow we sort the box into drawers."
Reagents gathered: request, collection, method, URL, Newman, summary.
Sources
- Newman's README (its command line options, library API, run events and reporters) (Apache-2.0 (the package's LICENSE.md, read 2026-11-20); facts and ideas only)
- Postman Learning Center (Postman's documentation of its app, collections, variables and scripts) (Not stated as open: Postman's own documentation (read 2026-11-20); facts and ideas only)
- The Express documentation (CC BY-SA 3.0 US (the site's footer, read 2026-11-15); facts and ideas only)