Looping Through Multiple Pages of a REST API Using Recursive Workflows in Retool

Hey everyone,

I wanted to share a method we’ve been using to loop through multiple pages of a REST API. This approach leverages Retool’s option to run workflows directly from within a workflow, so there’s no need to use the webhook trigger. I found some of the other solutions offered on the forum a bit convoluted for my use case, so I'm sharing this in hopes it helps someone in a similar situation.

How It Works

  1. Initial API Call: We start with an API call to grab the data.

  2. Handling the Response: When we get the response, we run an if-statement:

    • Check for Items: First, we check if any items are returned.
    • Check for Next Page Token: We then check for a next page token.
  3. Stopping Condition:

    • If no items are returned, the workflow stops—meaning all pages have been fetched and the response is empty.
    • If there is a next page token, we grab it and feed it back into the same workflow, creating a loop.
  4. Recursive Workflow: This creates a simple, clean recursive workflow that continues fetching pages until no next page token is found. It automatically stops when it reaches the end.

Why This Method?

This approach is cleaner and simpler than others I’ve seen, which often involve using two different workflows with different triggers. It’s straightforward and stops automatically when there are no more pages to fetch.

I’ve come across a couple of other forum posts that discuss iterating through paginated REST API responses, but they didn’t quite hit the mark for me (e.g., looping through 2 workflows). They use a more complex setup, while I found this method keeps things tidy with just one workflow.

I’ve included an annotated screenshot below that gives a high-level overview of the workflow blocks in action:

Hope this helps! Let me know if you have any questions, suggestions to modify this, or other methods that have worked well for you. For example, some REST APIs offer options to combine HTTP requests using multipart, but for a routine database update, this seemed much simpler.

5 Likes

For a bit more context, here’s a screenshot of the workflow step that makes the process recursive, as mentioned in the overview. The other parts of the workflow handle straightforward tasks, like pulling data from the API and upserting records into the Retool database. This setup allows for quick dashboard queries without needing to fetch all pages of thousands of records each time. The workflow can run automatically each day or asynchronously when an app is loaded or when a Refresh button is pressed.

1 Like

Since you offered for us to share, here is how I solved the recursive problem :slight_smile:

Hi @explore, thanks for sharing your implementation of looping through multiple pages of a REST API in Retool using recursion within a single workflow.

It's both elegant and powerful.

Why does mine refuse to trigger more than one child workflow?

It would appear you are missing Response blocks. I don't see any
image
in your screenshot of the workflow summary.

I love me some re-re-recursion!

1 Like