Postman request chaining lets you pass data between requests, but it’s not about a magical "chain" linking them; it’s about explicitly capturing and injecting values.
Let’s see it in action. Imagine you first create a user and then need to use that user’s ID in a subsequent request to fetch their details.
Request 1: Create User
// Request Body (POST to /users)
{
"name": "Alice Smith",
"email": "alice.smith@example.com"
}
Response 1:
{
"id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"name": "Alice Smith",
"email": "alice.smith@example.com",
"createdAt": "2023-10-27T10:00:00Z"
}
Now, we need to grab that id and use it. In the "Tests" tab of Request 1, we’d add:
const jsonData = pm.response.json();
pm.environment.set("userId", jsonData.id);
This little snippet does two things: pm.response.json() parses the JSON response from the server, and pm.environment.set("userId", jsonData.id) stores the extracted id into a Postman environment variable named userId.
Request 2: Get User Details
Now, in Request 2, we can reference that environment variable. If Request 2 is a GET request to /users/:userId, the URL would look like this:
{{baseUrl}}/users/{{userId}}
When Postman executes Request 2, it will substitute {{userId}} with the value we stored from Request 1’s response.
This mechanism is powerful because it mimics real-world application flows. You authenticate, get a token, and use that token in all subsequent calls. Or you create a resource, get its ID, and then perform operations on that specific resource.
The core components are:
-
pm.response.json(): Parses the JSON body of the previous request’s response. If the response isn’t JSON, you’d usepm.response.text()orpm.response.headers(). -
pm.environment.set(key, value): Stores a variable in your current Postman environment. Other scopes exist:pm.collection.set()for collection-level variables,pm.folder.set()for folder-level, andpm.globals.set()for global variables. Environment variables are usually the sweet spot for request chaining within a specific workflow. -
{{variableName}}: The syntax Postman uses to inject variable values into request URLs, headers, or bodies.
The "Tests" tab is where you extract data from a response, and the request body/URL/headers are where you inject data for the next request. You can chain multiple requests this way, building complex workflows. For instance, you could create a product, get its SKU, then add it to a cart using that SKU.
You can also extract data that isn’t in the JSON body. For example, to get an Authorization header value from a previous response:
const authHeader = pm.response.headers.get("Authorization");
pm.environment.set("authToken", authHeader);
And then use it in a subsequent request header:
Authorization: {{authToken}}
The real magic here is that Postman doesn’t enforce a strict "chain." You can manually run Request 2 after Request 1, or you can use the "Runner" feature to execute a sequence of requests automatically. The "Runner" is where you’d define the order and ensure that the variables set by one request are available to the next. It’s crucial to understand that the "Tests" script in Request 1 runs after Request 1 has completed and received its response, making the data available for Request 2 (or any subsequent request run in the same collection/folder execution).
Many users think you can directly reference a response value from a previous request in the same request’s definition. You can’t. The data has to be explicitly captured and stored as a variable. The "Tests" tab is the gateway for this data transfer, acting as a temporary holding pen between requests.
The next hurdle is managing complex data structures or conditional logic based on response data.