What is API chaining?
The API chaining method is to create a sequence of API calls where the output of one API request is used as the input for the next. The technique is an automated and efficient way of executing a Multistep Process.We will learn to get a response from one API and pass it as a request parameter in another API.
Prerequisites :-
1.Postman installed
2.Create Collection and add two requests
3.Basic Understanding of API: Familiarize yourself with the basics of APIs, including request methods (GET, POST, etc.), endpoints, and response structures.
Note: Response of one request should be the input of second request.
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 create user details: POST /api/users
Endpoint to retrieve user’s posts: GET /api/posts/{userId}
Steps 1.
Send the end point POST /api/users in postman with the Json body
{
"user_first_name":"Adi",
"user_last_name":"Ram",
"user_contact_number”:”1234567892 “
"user_email_id":"kk@gmail.com"
}
Steps .2
Response contain user id for the new user.
{
"user_id": 1726,
"user_first_name": "Adi",
"user_last_name": "Ram",
"user_contact_number": 1234567892,
"user_email_id": " kk@gmail.com ",
"creation_time": "2024-09-20T22:40:46.693+00:00
}
Step3:
Write a java script in the SCRIPT tab under POST-RESPONSE section as
var jsondata=pm.response.json();
var user_id= jsondata.user_id;
pm.collectionVariables.set("UserID", jsondata.user_id);
the above code fetch the user id and store that value in the collection level with the variable name UserID.
Step 4
Send the another end point GET /api/posts/{{ UserID} }in postman with the Json body. Then run the collection we get the below response.
{
"user_id": 1726,
"user_first_name": "Adi",
"user_last_name": "Ram",
"user_contact_number": 2632057892,
"user_email_id": "DYKyBlf0ly@gmail.com",
"creation_time": "2024-09-20T22:40:46.693+00:00",
"last_mod_time": "2024-09-20T22:40:46.693+00:00"
}
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.