Meet 2025’s Top-rated Software Test Management Tool. Learn More >

API Chaining with Postman

API Chaining with Postman

In API testing, API chaining is like arranging steps in a dance. It’s about doing many API requests in a row, where each one depends on the last.

Picture this: you have an API to fetch user details and another to fetch posts for each user. Now, with API chaining, you start by gathering user details, then gracefully pluck out their IDs from the response, and finally, glide into fetching posts for each user, one by one. It’s a synchronised flow of data, ensuring that each move sets the stage for the next, ultimately delivering a flawless performance.

This can be achieved using Postman scripts, specifically through the use of environment variables and Postman’s scripting capabilities.

Example with Scenario:

Here’s an example of how to achieve API chaining in Postman using scripts:

Suppose we have two API endpoints:

  • Endpoint to retrieve user details: GET /api/users
  • Endpoint to retrieve user’s posts: GET /api/posts/{userId}

We want to first retrieve the list of users and then for each user, retrieve their posts.

  1. First, send a request to retrieve the list of users:
  • Method: GET
  • URL: https://<userYourApi>/api/users
  1. In the Tests tab of this request, add the following script to extract user IDs from the response and store them in an environment variable:
let users = pm.response.json();
let userIds = users.map(user => user.id);
pm.environment.set(“userIds”, JSON.stringify(userIds));
  •  

Let’s Understand what each line does:

let users = pm.response.json();

  • This line retrieves the response data from the API request made in Postman and parses it as JSON format. The parsed data is then stored in the variable users.
  • let userIds = users.map(user => user.id);
    • Here, the map() function is used on the users array to extract the id property of each user object. This creates a new array called userIds containing only the user IDs.
  • pm.environment.set(“userIds”, JSON.stringify(userIds));
    • Finally, this line sets the value of an environment variable named “userIds” in Postman’s environment. The value assigned to this variable is the JSON string representation of the userIds array. This allows the user IDs to be stored and accessed within the environment for later use in subsequent requests.

rIds));

  1. Now, we’ll use the extracted user IDs to make subsequent requests to retrieve each user’s posts. Create a new request and set the URL as follows:
  • Method: GET
  • URL: https://<userYourApi>/api/posts/{{userId}}

In the Pre-request Script tab of this request, add the following script to dynamically set the user ID for each request:

let userIds = JSON.parse(pm.environment.get(“userIds”));
let userId = userIds.shift();
pm.environment.set(“userId”, userId);

Let’s go through each line:

  • let userIds = JSON.parse(pm.environment.get(“userIds”));
    • This line retrieves the value of the environment variable “userIds” using pm.environment.get(“userIds”), which returns a string representing a JSON array. JSON.parse() then parses this string into a JavaScript array and assigns it to the variable userIds.
  • let userId = userIds.shift();
    • The shift() method is called on the userIds array, which removes and returns the first element of the array. This element (the user ID) is then assigned to the variable userId.
  • pm.environment.set(“userId”, userId);
    • Finally, the value of the userId variable is set as the value of the environment variable “userId” using pm.environment.set(). This allows the user ID to be stored and accessed within the environment for later use in subsequent requests.

erId)

  1. In the Tests tab of the same request, add the following script to remove the processed user ID and set the next user ID for the subsequent request:
let userId = pm.environment.get(“userId”);
pm.environment.set(“userId”, userIds.shift());

Let’s break down each line:

  • let userId = pm.environment.get(“userId”);
    • This line retrieves the value of the environment variable “userId” using pm.environment.get(“userId”) and assigns it to the variable userId. The pm.environment.get() function is used to access environment variables within Postman.
  • pm.environment.set(“userId”, userIds.shift());
    • Here, userIds.shift() is called on the userIds array, which removes and returns the first element of the array. This element (the user ID) is then set as the value of the environment variable “userId” using pm.environment.set(). This allows the updated user ID to be stored in the environment for later use in subsequent requests.
  1. Now, repeat steps 3-5 for as many requests as needed to process each user’s posts.

By following these steps and scripts, you can achieve API chaining in Postman, where the output of one request serves as input for subsequent requests, allowing you to chain multiple API calls together seamlessly.

Conclusion:

In conclusion, API chaining in Postman facilitates sequential data processing by linking multiple API requests. By fetching user data and extracting IDs for subsequent requests, testers can seamlessly retrieve and process information step by step. This method enables efficient and organised data handling, enhancing the overall testing process in Postman.

The beauty of API chaining lies in its ability to mimic real-world scenarios with precision. By linking multiple API requests together, we create a dynamic chain of events that mirrors how users interact with our applications. This not only helps us validate the functionality of interconnected APIs but also paints a vivid picture of how our system performs in the wild. From fetching user profiles to retrieving their latest posts, API chaining enables us to choreograph these interactions effortlessly, ensuring that our APIs dance to the rhythm of user expectations.

Picture of Siddharth

Siddharth

Siddharth is the founder and author of Automation Reinvented and has conducted training sessions on UI/API automation with CICD integration. He also works closely with companies to help them develop new automation tools. Currently working as SDET for a product company and also an active contributor in QA community space

All Posts

Deliver quality software with QA Touch

Questions? Explore our docs, videos, and more just one click away!

Real people with life changing results

Insights from QA Teams on QA Touch’s Impact

Frequently asked questions

Everything you need to know about the product and billing

Why QA Touch?

QA Touch is an AI-driven test management platform built by testers for testers. It simplifies collaboration between developers and QA engineers while helping to manage, track, and organize test cases efficiently. Streamline your testing processes, enhance QA visibility, and deliver high-quality software with ease.

QA Touch offers comprehensive features to manage the entire test management process. From easy migration with CSV files to audio-visual recording of issues and activity logs and a shareable dashboard for real-time reporting to stakeholders, we ensure the testing teams are always on top of things.

Our focus is on providing complete visibility and control over testing workflows and fostering collaboration between testers and other stakeholders (both internal and external). You can have a look at all the features here.

Once you sign up, it takes only 30 minutes to get your QA Touch account up and running. After registration, you will receive an account activation email with all the details. Log in with your account details and create your first test project on QA Touch—it’s that simple. You are now ready to start inviting your team and assigning them roles.

If you are finding it difficult to log in or facing any difficulty, feel free to reach our support team at info@qatouch.com

Why is QA Touch the best test management tool for me?

QA Touch is an AI-driven test management platform that simplifies collaboration between your developers and testers. Beyond creating, organizing, and executing test cases, QA Touch enables you to manage projects, track bugs, and monitor time—all in one platform.

With an intuitive UI and seamless two-way integrations, QA Touch adapts to your workflow, making test management, project oversight, and bug tracking smarter and more efficient.

With secure OKTA, Microsoft Azure SSO, and Google SSO enterprise features, you can stay connected in every app.

We have integrations with dozens of major apps like Slack, Jira, Monday.com, Cypress, and many more. Explore the whole list of integrations now supported here: Explore integrations

The test management tool is a modern software application that helps QA teams and developers manage their testing process efficiently. It provides a structured approach to creating, organizing, executing, and tracking tests to ensure software applications meet specified requirements and function properly before release.

Don’t just take our word for it.

QATouch is a leader in G2 market reports.