Chaining request data helps you use information from one API response in the next request. This makes testing real and connected, like a conversation between apps.
0
0
Chaining request data in Postman
Introduction
When you need to use a user ID from a login response in a later request.
When you want to test a sequence of API calls that depend on each other.
When you want to automate tests that require dynamic data from previous responses.
When you want to verify workflows that involve multiple steps with shared data.
Syntax
Postman
// Extract data from response and save to variable let jsonData = pm.response.json(); pm.environment.set("variable_name", jsonData.key); // Use saved variable in next request URL or body GET https://api.example.com/users/{{variable_name}}
Use pm.response.json() to parse JSON response.
Use pm.environment.set() or pm.collectionVariables.set() to save data for next requests.
Examples
This saves the user ID from the login response to an environment variable called
userId.Postman
// Save user ID from login response let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.user.id);
This uses the saved
userId in the URL of the next request.Postman
// Use saved userId in next request URL
GET https://api.example.com/users/{{userId}}Saves an authentication token for use in headers of future requests.
Postman
// Save token from login response let jsonData = pm.response.json(); pm.environment.set("authToken", jsonData.token);
This adds the saved token to the Authorization header for secure requests.
Postman
// Use token in Authorization header
Authorization: Bearer {{authToken}}Sample Program
This example shows two requests chained together. The first saves the user ID from login. The second uses that ID to get user details and checks it matches.
Postman
// First request: Login and save user ID pm.test("Login successful", function () { let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.user.id); pm.expect(jsonData.user).to.have.property('id'); }); // Second request: Get user details using saved userId // URL: https://api.example.com/users/{{userId}} pm.test("User details fetched", function () { let jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('id', pm.environment.get("userId")); });
OutputSuccess
Important Notes
Always check the response structure before extracting data to avoid errors.
Use environment or collection variables to share data between requests safely.
Chaining helps test real user flows where data depends on previous steps.
Summary
Chaining request data lets you pass info from one API call to the next.
Use pm.response.json() to get data and pm.environment.set() to save it.
This makes your tests more realistic and connected.