Dessert: How to Never Go Hungry with APIs 🤤

Welcome back. If this is your first time, please check out our first tutorial titled “Appetizer: How To Never Go Hungry with APIs 🍉”. Here, you will learn how to make an API call and gather the necessary prerequisites. Once you’ve done that come back here to continue.

Picking up from where we left off, lets generate an integration test and chain a series of APIs that Zomato offers together, not just the Cities API.

2. Magically Generate a Test 🤩

After your API call, click on “Generate Test” at the top of the Client and it will prompt you to create a “Test name” and “Create new project”. You can input anything in there.

When you are ready, hit the green tick to confirm, sit back, and watch magic happen. Soon you will be prompted with the following screen that says “All set!”. Based on your response body, API Connect Test and Monitor is iterating through the payload, understand what test assertions we should generate and parameters to add — with a single click.

Click close. You are now met with a visual drag-and-drop composer of your GET request, headers, and associated test assertions. For example the second line item is an “assert equals” that is ensuring your payload is returning a 200 status code. This suite of tests will ensure a functional uptime of your API, more than simply if its running or not. It should look like this.

Before we go further, lets add a “Global Parameter” with the next GET request we will be using to chain to this test. We are doing this because although you can hardcode a string, in our case the next GET request, the complexity increases with number of tests and if anything changes in the future you only need to update a single value. Lets see how.

Expand the “Data Sets” panel from the left and you should see “GLOBAL PARAMETERS” with a single entry “endpointUrl” with the value of the/cities API we used previously. The next API we want to use is the Zomato /search API to find our favourite cuisine (spoiler: we use dim sum).

Click “Add Global Param” and label “param:” as “searchUrl” and “value:” as It should look like this.

3. Chaining and Integrating APIs Together 🔗

As you recall, our /cities API returned us every city that has Toronto in the name. We need to find the one that is only Toronto, Canada. Click the + circle to open up a palate of options.

Select the blue “Each” option. This will spawn at the bottom of the test, scroll down, and drag it up to the top under your first GET request (not required).

Now, edit the “Expression” field with “payload.location_suggestions”. This will loop through our previous GET request location suggestions which were Toronto. Confirm using the tick button and click on the “for each in” element so it expands. It should look like this.

Now click “+ Add Request/ Assertions” and choose the blue “If Component” icon. Edit the “Expression” to be_1.country_name='Canada' (note, you must put single quotes around Canada). We are doing this to pinpoint Toronto, Canada and not any other. It should look like this.

Next click on the “if” component so it expands, click “+ Add Request/ Assertions”, and choose the green “GET” icon. We will be chaining another GET request, specifically a Zomato /search API to find our favourite cuisine.

If you recall earlier in the tutorial we added the /search API as a global parameter. So now, you only have to enter in “Url” as “${searchUrl}” and give the “Variable” field as “searchResults”.

Now click “+Add parameter” and add the following values:

Name: entity_id, Variable:
Name: entity_type, String value: city
Name: q, String value: Dim Sum (or example cuisine of your choice).
Name: count, String value: 10

The “GET” should now look like this:

Confirm with tick icon in top right. The next thing we need to do is add our user-key back in to authorize our API. This will be the same value as we used for our first API at the start of the tutorial so let’s recycle that instead of inputting it back in.

Scroll to the top of the tests, find our original GET request for cities and clone the user-key header like this.

Now click and hold the new header element with our user-key and drag it under our GET request for /search. It should look like this.

You are almost to the finish. Lets move onto the last step.

4. Run Test 🏃

Click “Save” at the top then click “Run Test”. Select the default downloader and click “Run Test” again. Note, if nothing happens your browser is probably blocking popups, so look for a warning to the right of the address bar and click on it to allow popups from this site. Otherwise a new tab will open titled “Test Report” and contain our 2 GET requests and our assertions.

At this point you can run the test! Click “Save” then “Run Test” and choose default downloader, then “Run Test” again.

If nothing happens, your browser is probably blocking popups, so look for a warning to the right of the address bar and click on it to allow popups from this site only. Otherwise a new tab will open titled “Test Report” and containing 2 GET requests and a number of assertions.

If everything went according to the tutorial, your test report should report “Test passed” and no failures. Lets dig in a bit deeper. For each of the GET requests /cities and /search click “See Request” and scroll to the bottom and click “Load Source”.

Scroll down to the bottom and you will see the resulting JSON data from each of the calls. If there was an error, this would be one of the ways for us to dig in a diagnose what error happened.

Let’s Eat 😋

Good job! You have successfully chained several APIs together with logic that will help you pinpoint a list of dim sum locations in Toronto, Canada. Your next challenge? Try them all!

We hope you found value in this tutorial. Please let us know in the comments what you would like to see next time!

Made with ❤️ from the IBM API Connect team.