Skip to main content
All CollectionsHow-tosArticles
Example Approach: API Pagination
Example Approach: API Pagination

In this article we'll explore an approach to implementing pagination within a story

Yanni Hajioannou avatar
Written by Yanni Hajioannou
Updated over a week ago

Intro

When we reach out to an API, sometimes it only shows us 20 items on one page. This is similar to going to eBay and wanting to buy antique furniture. We click the next page each time we want to view items.

Pagination is a technique where we hit an API and get the page's "0." Then, we want to iterate to reach the next page. We can do this by dynamically updating the page "index" when we reach out to the given service.

Example: Link

What data can be used for a loop?

API pagination often requires looping through multiple pages of data. We'll want to first check to see when we reach out to a service if it has additional pages.

Let's check this pokemon API to see when we reach out to an endpoint if it has more results.

Press Play/Pause to follow along with the GIF below

When reaching out, we can see that there is a "next" value with a URL. Indicating that we have more pages of data. We can also see that there is a pretty large number for the results, but we only get a handful of pokemon.

Example steps:

  1. Pull in an HTTP request action

  2. Set the name

  3. Set the URL

  4. Set the content type

    1. JSON

  5. Set the method

    1. GET

      1. Most services that don't require you to authentication with a password or API token will typically be GET by default.

  6. Run the action to review the results in the event data

    1. Review that there is a URL in the "next" value

How can you dynamically call an API service?

While looping, we need our HTTP requests to be dynamic. This means adjusting them based on the current state of the loop, so we’re always pulling the correct data from the correct page.

Press Play/Pause to follow along with the GIF below

Notice, we retain the same HTTP request name for the action. This is because when we loop later on we want to have our trigger look at the "get_data.body.next".

Example steps:

  1. Select your HTTP request

  2. CMD + C to copy the action (CTRL + C on windows)

  3. CMD + V to paste (CTRL + V on windows)

  4. Drag arrow from previous action to new one

  5. Click into the URL and delete all the text

  6. Click the + and then value

  7. Set to get_data.body.next

    1. This is the URL for the next page

  8. Set the name

  9. Set the URL

  10. Select the first action and click run

  11. Check the event data and review the results

    1. We'll see that we have the next page of data and now "next" has a new value for the following page

    2. We can also see the "previous" page

    3. Notice there is an "offset" in the URL

How can you validate a loop?

To ensure our loop is functioning correctly, we implement a trigger that validates the loop’s progression. This trigger acts as a checkpoint to confirm that our loop is moving forward and pulling in the correct data.

Press Play/Pause to follow along with the GIF below

With our trigger set in place, we can now loop the data. By dragging from the bottom of the HTTP request to the Trigger. Having it go through and iteratively going to the next page. Since we dynamically reference the "get_data.body.next" it will look at the next page.

Example steps:

  1. Drag a trigger into your story

  2. Set the name

    1. In this example I'm calling it "Continue?"

  3. Drag it between the two HTTP requests

  4. Pull the arrow from the first action to the trigger

  5. Click into the data pill under rules that says "somekey.subkey.subkey.goal"

  6. Set the JSON path to be "get_data.body.next"

    1. This tells us what the next page of the URL we'd want to call

  7. Click to the front of the JSON path and type out "IS_PRESENT" and click on it

    1. This function tells us if the data is present. If it is, the result will be true. Otherwise, the result will be false. This is a really great way to test using a trigger to see if something is there or not.

  8. Click the drop down that says "is equal" and change it "formula is true"

  9. Drag the arrow from the trigger to the second HTTP request action

  10. Drag the arrow from the bottom of the second HTTP request action and pull it into the Trigger

    1. This is the loop that we've been developing. Now when we run it from the top it will

      1. Check the trigger if there is another page

      2. Make an API call to the next page

      3. Loop these steps until there is no value for "next"

    2. If your trigger is not set up correctly, the loop will continue indefinitely. If that happens you can click off of your action and in the properties panel click Disable. This will stop your story from continuing to execute

  11. Check the event data for the last thing that executes from the second HTTP request action

    1. You'll see that the next value is "null" and that the "results" are only showing a portion of the data. Where when we first started making the HTTP call there were a lot more items. This is just because we are at the "last" page.

Which action does the loop end?

During the loop, we might need to capture specific elements dynamically. It’s important to be mindful of our triggers here, ensuring they exit correctly to prevent any errors or endless loops.

Press Play/Pause to follow along with the GIF below

Example steps:

  1. Drag an event transform action into your story

  2. Set the name

    1. In this example I'm calling it "Loop done"

  3. Hover over the trigger and drag from the "No match" into the event transform

    1. Anytime the trigger condition isn't met, it'll go to this action

  4. Click into the event data of the trigger

    1. Click the ... for "get_data" and find the previous value

      1. This is the second to last page from the loop

    2. Click the two squares to the left of previous to copy that JSON path

  5. Click into your event transform

  6. Click into the purple text to the right of "message" in the properties panel

  7. Delete that and CMND + V to paste the copied JSON path (CTRL + V on windows)

    1. If you want to add some extra flair you can go to the beginning of that data pill and add text like "Second to last page is: " just to add some static value before the URL.

  8. Click on the trigger and then the 3 dots on the bar under it

  9. Select re-emit last event

  10. Click on the event transform action's event data to review the final results

Did this answer your question?