Learn from experts and connect with global tech leaders at POST/CON 24. Register by March 26 to save 30%.

Learn more →
X

How to Make Money Using Postman: Chaining Requests

Avatar

Most people already know how to send a lone request using Postman. Send it off, inspect the response you get from the server. Make some adjustments, and then try it again.

That’s all well and good. However, when you save one or more requests to a collection, you can start using some of the more advanced features in Postman. For example, with Postman, you can write scripts that run before and/or after you send your request and receive a response from the server.

There’s a lot more you can do with these scripts.

  • Do setup and teardown for testing. For example, you can do calculations to use in the main request or send asynchronous requests. Tests scripts are the cornerstone of testing in Postman – to validate your endpoint is working as expected.
  • Direct the next request to be called based on the results of the server response. Using basic JavaScript, you can then use `postman.setNextRequest(“Request name”)` to guide the sequence of the remaining API calls.
  • Extract data from the response. This can be useful to capture session or cookie data or parse information available within the response body to chain requests. In this tutorial, we’ll dig deeper into this scenario.

Set up a Bitcoin tracker

Have you heard of Bitcoins?!? It’s one of many cryptocurrencies based on blockchain technology, something about maths, and… well, there’s probably more I should know about that. What I did know, however, is someone who accidentally purchased 200 Ethereum (another cryptocurrency), instead of $200 worth of Ethereum. With this fortunate mistake, he made enough money to pay for his son’s college tuition over the course of 2 weeks  ?

How can you make money when the price of Bitcoins are so volatile? Turns out, you can make money because the price of Bitcoins are so volatile.

I had serious fear of missing out (FoMO), and wanted to gamble on invest in Bitcoin too! The next time the price of Bitcoin fell to a reasonably low rate, I planned to fill my coffers with Bitcoins like Scrooge McDuck.

Let’s walk through how to extract data from a response and chain together requests. 

  1. Create and select an environment.
    • For this example, we’ll create and select a new environment. We’ll use this environment to store and access our environment variables. Update the values using your own Twilio API credentials. We’ll be using the Twilio API to send ourselves an SMS when the price of Bitcoin falls to certain price. Sign up for a Twilio test account if you don’t already have one.
  2. Send a `GET` request, extract data from the response, and store as an environment variable.
    • Send a `GET` request  to `https://api.coindesk.com/v1/bpi/currentprice.json` using Postman. This request returns a JSON body. Remember to save this request to a collection. We’ll see why this is important in just a bit.
    • Under the Tests tab, add the following JavaScript. The important thing to know here is that we can access the JSON response body using pm.response.json(), and then parse the response object to extract the current Bitcoin rate. Then we can store this value as an environment variable using pm.environment.set("rate", usdRate) where "rate" is the name of our environment variable and usdRate is the value that we’ll store as this variable. 
    • After we hit Send, click on the Quick Look icon to see that we’ve extracted the data from the response body, and saved it as an environment variable. Now, we can use this variable in our next request.
    • In the same manner, can you figure out how to extract the “time” from the response, and then save it as an environment variable?
  3. Send a `POST` request using a stored environment variable. 
    • For this second request, we will use the Twilio API to send an SMS. Send a POST to `https://api.twilio.com/2010-04-01/Accounts/{{twilioAccountSID}}/Messages.json` where {{twilioAccountSID}} is your own Twilio Account SID. Remember to also save this second request to our collection.
    • String substitution: we can use the double curly braces syntax anywhere we would normally input text, like {{variable-name}}, to use a variable. Postman will access the variable’s value and swap in the value as a string. Use the double curly braces in the URL, parameters, or any of the other data editors.
    • More String Substitution: this request requires the authentication credentials we saved to our environment in the first step. We can use the same double curly braces syntax in Postman’s auth helper to handle Basic Auth for the Twilio API. Authentication (and scripts) are available for entire collections and folders too.
    • Special note about accessing variables in scripts: there’s a different way to access variables in the script sections since we’re writing JavaScript, instead of text. Under the pre-request or tests script tabs, use pm.variable.get("variable-name").
  4. Run the collection
    • Instead of sending your requests one at a time, you can run every request in the collection in one go. Run the collection locally with collection runner in the Postman app or with the Newman command line tool. Alternatively, you can run the collection and environment on the Postman cloud as a monitor.

Postman Tip: you can send these requests in the default, ordered sequence, or you can write a script to execute these calls in a specified sequence using postman.setNextRequest("Request name") or to terminate the remaining collection run using postman.setNextRequest(null). In this manner, you can build a sequence of API calls that mirror your use case.

Try it yourself

Click the orange Run in Postman button to import the Bitcoin Tracker collection and environment into your Postman app. 

  1. Enter your Twilio credentials: You will need a Twilio Account SID to update the `twilioAccountSID` value within the Postman environment. You will need a Twilio Auth Token to update the `twilioAuthToken` value within the Postman environment.
  2. Enter a target rate: In this example, set a target rate by updating the `targetRate` value within the Postman environment. If the current Bitcoin rate drops below this target, like in a flash crash, the `POST` request to the Twilio API will be called.
  3. Run it: Run the collection and environment locally using the Postman collection runner.

Need help getting started? Check out the documentation for the Bitcoin Tracker, and follow along with step-by-step instructions, examples of responses, and helpful screenshots.


This is a follow up to this popular post: https://blog.postman.com/2014/01/27/extracting-data-from-responses-and-chaining-requests/

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

10 thoughts on “How to Make Money Using Postman: Chaining Requests

  • Avatar

    twilio — access denied

  • Avatar

    The problem I see is that environments are shared by teams; as a result, so are environment variables. If you have a team using the same collection and environment, you will be stomping all over each other. Maybe I’m missing something, but this seems untenable.

  • Avatar

    Hi thanks for detailed explanation. I am unable to pass value to next request end point-
    request 1- response body as array items and set to env variable
    request 2-in pre request took the response and used below code to get ids-

    var jsonData = pm.collectionVariables.get(“responsefrm_req_1”);
    var jsonData = JSON.parse(jsonData);
    var i,j;
    for (i=0;i<jsonData.length;i++){
    CompId=jsonData[i].competitorId; //pass these 31 competitorId to end point url tag
    };
    pm.collectionVariables.set("NeReqId_1", CompId); //configured NeReqId in end point as

    {{URL}}/tag_1/tag_2/tag_3/{{NeReqId_1}}/tag_4
    this only pass last value received in CompId variable n not all sequentially. I want to run request_2 for 31 times taking each value one time.
    I'm using postman first time .let me know if you need more info.

  • Avatar

    Does i need repeat write this script on my 200+ request ?
    Please add this feature independently, like Insomnia does

    • Avatar

      Under Step 3, the article mentions how you can add collection-level or folder-level scripts to your requests. So that the script runs before or after every request in the collection or folder.

  • Avatar

    How do we get the targetRate to actually trigger the post request? I don’t see that described above

    • Avatar

      Under the Tests tab of the first request, you can see the code comparing the targetRate to the current price of Bitcoin to either trigger the POST request or terminate the collection run.

  • Avatar

    The code comparing the target rate and actual rate does not appear to be working. Postman executes the SMS post request whether the actual rate lies above or below the target rate. I thought maybe I was missing something so I downloaded the tracker here: https://documenter.getpostman.com/view/1559645/RVnYDKD5#59bc44ef-1d0e-47fc-ba72-92f431615102 and exchanged my twilio credentials, same issue. Where can I read more about/find a resolution to this problem?