Skip to main content

Nested loops for paginated APIs

Use nested loops when each page of an API response contains an array that you need to process before requesting the next page. An outer loop requests the pages, and an inner loop processes each page's items. This example sends up to ten movie quotes per page to Launch Console, then sends Done with everything after the last page finishes.

Watch the walkthrough, then use the template and key settings below as you follow along.

Copy the demonstrated Wrkflow​

Click Copy template, open a Wrkflow in the Designer, and paste with Ctrl + V on Windows or Cmd + V on Mac.

Connect Start to the initial page-count Perform an API call and select your own The One API account connection on both API Wrk Actions before launching. Account credentials are not included.

Key settings​

Outer loop: request each page​

The initial API request discovers the page count without processing its items. The outer loop then requests pages 1 through pages. This differs from Number range loops, where page 1 has already been requested and the range begins at 2. Here, starting at 1 ensures the first page's items are processed by the inner loop.

Wrk ActionConfiguration
Initial Perform an API callUse GET with https://the-one-api.dev/v2/quote. Capture $.pages from Response body as a numeric custom output named pages.
Loop through number rangeSet Starting index to 1, Ending index to pages, and Loop increment to 1. Map the outer Continue loop's Continue loop ID into its Continue loop ID input.
Set variableImmediately after Loop through number range, save its Loop ID in an output named outsideLoopId. Do this before the inner loop runs.
Per-page Perform an API callUse GET with https://the-one-api.dev/v2/quote?page={{loopNumberRangeCurrentIndex}}. Capture $.docs as a Text (long) custom output named docs, and $.page as a numeric custom output named page.
Outer Continue loopSet Loop ID to outsideLoopId. Connect Continue back to the range loop and Exit to the final logger, using Info with Done with everything.

Variables with the same name overwrite the same underlying field. Save the outer Loop ID as outsideLoopId before the inner loop runs, so the inner loop does not overwrite the ID needed by the outer Continue loop.

Inner loop: process the current page's array​

The two inner Loop through JSON array Wrk Actions are parts of one logical loop. The first starts a fresh loop for each page. The second resumes that page's loop after Continue loop returns.

Wrk ActionSetting
First Loop through JSON arrayConnect the per-page API request to this Wrk Action. Set JSON Array to the current page's docs, Maximum number of items to 10, and leave Continue loop ID blank.
Second Loop through JSON arrayUse the same current page's docs and item limit. Map the inner Continue loop's Continue loop ID into its Continue loop ID input. Connect the inner Continue outcome to this Wrk Action.
Both array Wrk Actions' Custom OutputsExtract $.dialog from Item value as a Text (long) output named dialog. Leave Start at last item? unset on both Wrk Actions.
Item Send to Launch ConsoleConnect both array Wrk Actions to this logger. Use Info with Page {{page}} | item {{loopJSONArrayCurrentIndex}} | {{dialog}}. Connect Success to the inner Continue loop.
Inner Continue loopSet Loop ID to the inner array loop's Loop ID. Connect Continue to the second array Wrk Action and Exit to the outer Continue loop.

Continue loop ID is optional so the first array Wrk Action can start fresh. Keep it blank there, but configure it on the second Wrk Action so subsequent items resume the same loop. This prevents a new page from reusing the previous page's inner-loop ID.

Check the result​

Save and launch the Wrkflow. Expect up to ten items from page 1, then up to ten from page 2, continuing through the reported page count. Each message includes its page number, item index, and quote. Done with everything should appear once after the last page's items finish.

The ten-item limit applies to each page, not to the entire Wrkflow. Launch Console shows newer messages first; use timestamps to follow the execution order.

Troubleshooting​

  • The next page starts too early: connect the inner Exit outcome to the outer Continue loop.
  • A page reuses the previous page's inner loop: leave Continue loop ID blank on the first Loop through JSON array action, and connect the inner Continue loop action's Continue outcome to the second Loop through JSON array action.
  • The outer page loop stops or repeats incorrectly: confirm Set variable saves the outer Loop ID as outsideLoopId, and that the outer Continue loop action uses that value.
  • Items are skipped: confirm both array actions use the current page's docs and the number range starts at 1 with an increment of 1.
  • An item field is empty: check the custom output's JSON path against the field name in the API response. In this example, the quote text is in dialog, so the path is $.dialog.