Skip to main content

Example approach: API pagination

In this article, we'll walk through an approach to implementing pagination within a story.

Written by Angie Ruhstorfer
Updated over a week ago

Overview

When communicating with APIs, you may receive a response that doesn't include all the results you expected. API pagination is a way to break large amounts of retrieved data into smaller, more manageable chunks. Compare this to online shopping, where you click "next" each time you want to view more items.

This article explains how to dynamically manage API pagination in Tines, ensuring you retrieve all the data you need.

Tines references

Before getting started, we recommend familiarizing yourself with the functionality this article covers:

Tines tip: Import our Implement pagination with these techniques story into your tenant to help visualize how pagination can work in your story flow.

Make it happen

Identify pagination data

API pagination often involves looping through multiple pages of data. So first, we need to determine if the API endpoint we're accessing has additional pages.

In this video, we reach out to the Pokémon API endpoint and verify if it has more results than what's provided in the first page (i.e., the first response).

In the API response (via the HTTP Request action's events), we can see that there is a next value with a URL. This URL indicates there are more pages of data:

An event data payload from the Pokémon API showing the first page of results. The response shows count: 1302, with the next value highlighted in a red border containing a URL to the second page (offset=20&limit=20) and previous set to null, indicating this is the first page of results.

Dynamically call an API endpoint

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

In this video, we look at how referencing the next value of the upstream Get data action works to dynamically pull the next page of the API endpoint.

Note:

  • When creating a pagination loop in Tines, we retain the same name for the actions that run the HTTP Requests. When we build our loop later on, we want to have our Condition action dynamically use whatever value is at the get_data.body.next path.

  • Other platforms may provide pagination values in their API responses differently than the example in this article. We always recommend checking your endpoint's documentation for more information.

In the output of the second HTTP Request action (the "pagination" action), we can see in its events a next value (showing there's more data to pull) and a previous value (showing that there were results pulled before this request).

A Tines storyboard showing two HTTP Request actions both named "Get data," with the second one selected and its event data panel open. The event payload shows the response from the Pokémon API, with count: 1302 and two highlighted values in a red border: next containing a URL to the next page of results (offset=40&limit=20) and previous containing a URL to the prior page (offset=0&limit=20).

Validate a pagination loop

To ensure our pagination loop is functioning correctly, we add a Condition action that validates the loop's progression, acting as a checkpoint to confirm that the loop is moving forward and pulling in the correct data.

In this video, we set up a Condition action to check if the next object of the Pokémon API endpoint returns a non-null value.

Note: In this video, you'll see the action referred to as a "Trigger." This action has since been renamed to "Condition action." The functionality is the same.

With the Condition action in place, we can now loop the data, having the flow iterate to the next page.

A Tines storyboard showing the Condition action "Continue?" selected, with its Build panel open on the right. The Rules section is highlighted with a red border, showing a single rule: the formula IS_PRESENT(get_data.body.next) with the rule type set to "formula is true." The storyboard shows the pagination loop structure with a red arrow indicating the loop direction from the bottom HTTP Request action back up to the top.

End a pagination loop

All loops must come to an end. We can use the Condition action's "no match" flow to handle this, essentially defining what the story should do when next returns a null value.

In this video, we connect an Event Transform action to act as the end of the loop when the Condition doesn't match.

Once next returns a null value, the story flow will go to the "Loop done" action, closing out the pagination loop.

A Tines storyboard showing a pagination loop pattern. An HTTP Request action named "Get data" connects to a Condition action named "Continue?" which branches into two paths: the match path loops back to a second HTTP Request action named "Get data" (which connects back up to the Condition action, forming a loop indicated by a red arrow), and the no-match path (shown as a dashed line) leads to an Event Transform action named "Loop done," highlighted with a red border.

Did this answer your question?