Real API workflows are not single requests. They are chains. You log in, get a token, use that token to create a resource, get the resource ID, use that ID to update it, then delete it. Each step depends on the previous one. Variables are the glue that connects them.
POST {{baseUrl}}/auth/login
Body (raw JSON):
{
"email": "{{adminEmail}}",
"password": "{{adminPassword}}"
}// Post-response script for Login request
const response = pm.response.json();
pm.test("Login successful - status 200", function () {
pm.response.to.have.status(200);
});
pm.test("Token is present in response", function () {
pm.expect(response.token).to.be.a("string");
pm.expect(response.token.length).to.be.greaterThan(0);
});
// Save token for subsequent requests
pm.environment.set("authToken", response.token);
pm.environment.set("loggedInUserId", response.user.id.toString());
console.log("Token saved:", response.token.substring(0, 20) + "...");POST {{baseUrl}}/posts
Headers:
Authorization: Bearer {{authToken}}
Content-Type: application/json
Body:
{
"title": "Test Post {{$timestamp}}",
"body": "This post was created by automated tests",
"userId": {{loggedInUserId}}
}// Post-response script for Create Post request
const response = pm.response.json();
pm.test("Post created - status 201", function () {
pm.response.to.have.status(201);
});
pm.test("Post has an ID", function () {
pm.expect(response.id).to.be.a("number");
});
// Save the created post ID for next requests
pm.environment.set("createdPostId", response.id.toString());
console.log("Created post ID:", response.id);// GET {{baseUrl}}/posts/{{createdPostId}}
// Post-response script:
const response = pm.response.json();
pm.test("Retrieved the correct post", function () {
pm.response.to.have.status(200);
pm.expect(response.id.toString()).to.eql(pm.environment.get("createdPostId"));
});
// DELETE {{baseUrl}}/posts/{{createdPostId}}
// Post-response script:
pm.test("Post deleted - status 200", function () {
pm.response.to.have.status(200);
});
// Cleanup: remove temporary variables
pm.environment.unset("createdPostId");
pm.environment.unset("loggedInUserId");By default, Collection Runner runs requests in the order they appear. But sometimes you need to skip a request, repeat one, or jump to a specific step based on a condition. That is where postman.setNextRequest() comes in.
// Jump to a specific request by name
postman.setNextRequest("Delete Post");
// Skip all remaining requests (stop the run)
postman.setNextRequest(null);
// Conditional branching
const statusCode = pm.response.code;
if (statusCode === 401) {
// Token expired — re-login
console.log("Token expired. Re-authenticating...");
postman.setNextRequest("Login");
} else if (statusCode === 201) {
// Success — proceed to verify
postman.setNextRequest("Get Post");
} else {
// Unexpected status — stop the run
console.log("Unexpected status: " + statusCode);
postman.setNextRequest(null);
}postman.setNextRequest() only works in the Collection Runner — not when you send a single request manually. Also, the request name must match EXACTLY (case-sensitive). If your request is named "Login" and you write setNextRequest("login"), it will not work.
Always clean up after chained tests. If you created a user, delete it. If you set temporary variables, unset them with pm.environment.unset("varName"). Leftover test data causes flaky tests on the next run.
Q: How do you chain requests in Postman? How does one request pass data to the next?
A: Request chaining uses environment or collection variables as the data bridge. In the post-response script of Request A, you extract a value from the response (like a token or created resource ID) and save it using pm.environment.set("varName", value). Request B then references it via {{varName}} in its URL, headers, or body. For example: a Login request saves the auth token, and all subsequent requests use Bearer {{authToken}} in the Authorization header. You can also use postman.setNextRequest("RequestName") to control execution order, skip requests, or create loops in the Collection Runner.
Key Point: Chain requests by saving response data to variables in one request and referencing them in the next. Use pm.environment.set() and pm.environment.get() to pass data.