Skip to content
Docs

How-to: Paginate through an API

Most APIs return results in pages. Publicus has two built-in pager connectors that iterate through pages automatically:

  • Pager / OffsetBased — for APIs that accept offset + limit.
  • Pager / PageBased — for APIs that accept page + size.

This guide shows how to set up each one.

A Pager connector runs the sub-graph wired to its iteration handle once per page. After each page it evaluates the endCondition expression. When that condition is truthy, no further page is queued and the pager’s output handle activates, so any post-pagination steps run.

A page-based pager on the canvas: Every page of invoices holds Fetch a page in its box, and Report the import runs after it
A page-based pager on the canvas: Every page of invoices holds Fetch a page in its box, and Report the import runs after it

Before each page runs, the pager works out the numbers that page will use and freezes them into the page’s start data. The sub-graph reads them through $page:

Path Description
$page->index Zero-based page counter (the first page is 0).
$page->startData["nextPage"] The page number for the page currently running.
$page->startData["nextSize"] The page size for the page currently running.
$page->startData["nextOffset"] The record offset for the page currently running (OffsetBased only).

Inside a @json block the same values are reached as JSONPath strings: "$.page.startData.nextPage" is the @json spelling of $page->startData["nextPage"]. See Connectors for the full $page reference.

Example: GitHub API style (?page=1&per_page=30)

Section titled “Example: GitHub API style (?page=1&per_page=30)”
  1. Drop a Pager / PageBased connector on the canvas.

  2. Connect an upstream node’s output to the connector’s input.

  3. Configure:

    startPage: 1
    startSize: 30
    endCondition: $page->index == 100 || count($page->prevOutput->items) == 0

    That stops pagination when the items array in the response is empty, with a 100-page bound so a misbehaving API cannot page forever.

  4. Wire a Transformer to the connector’s iteration handle:

    @json
    {
    "page": "$.page.startData.nextPage",
    "per_page": "$.page.startData.nextSize"
    }
    @endjson
  5. Connect the Transformer’s output to an OpenApi / Operation or Rest / Operation that calls the API with those parameters.

  6. Wire the API response downstream for processing (store, transform, and so on).

Example: offset + limit (?offset=0&limit=50)

Section titled “Example: offset + limit (?offset=0&limit=50)”
  1. Drop a Pager / OffsetBased connector.

  2. Configure:

    startSize: 50
    startOffset: 0
    endCondition: $page->index == 100 || count($page->prevOutput->data) == 0
  3. Build the request parameters in a Transformer wired to iteration:

    @json
    {
    "offset": "$.page.startData.nextOffset",
    "limit": "$.page.startData.nextSize"
    }
    @endjson

endCondition is the only pager field that can read $page->prevOutput, the output of the page that just finished. The next* fields are frozen before their page runs, so they see only $page->index and the pager’s own start configuration.

Two consequences follow:

  • The idiomatic stop tests the previous response: count($page->prevOutput) == 0, or whatever emptiness marker the API uses.
  • Cursor pagination is not supported. A pager cannot take a next_cursor out of a response and feed it to the following request. The nextPageProperty and nextSizeProperty fields look like they do this, but nothing reads them. They are reserved, inert, and safe to leave empty.

outputMode decides what leaves the pager’s output handle once every page has run:

Mode Emits
summary {"count": n}, the number of pages that ran. This is the default.
combine Every page’s output, in page order, as one JSON array.

Set combine when a node after the pager has to read the records themselves. Keep summary when the iteration sub-graph already did the work, because the combined aggregate is stored in the run’s record and a long run makes that large.

The node wired to the output handle receives the result as $prev. Any node further down reads the same result by the pager’s name, for example $workflow->node('Fetch all orders').

On the canvas, show a run with Show on the canvas in the Runs rail (see Watching runs on the canvas). The pager then says how its pages are going, under its own state: for example “12 done · 1 running · 2 failed”, with failed in red. The counts update while the run goes on. There is no total, because the pager finds out it is on the last page only when the end condition says so. A pager inside another pager counts the pages of every outer page together.

A pager on the canvas with a run shown: Done under its title, and under that how many of its pages are done
A pager on the canvas with a run shown: Done under its title, and under that how many of its pages are done

Every page is a separate activity in Runs. Open a run and expand the pager to see each page’s input (the request parameters) and output (the API response). This shows exactly where pagination stopped or which page produced an unexpected result.

A run report for a page-based pager: three page runs, each with its Fetch a page request for pages 1, 2 and 3 at size 50 and the invoices it returned, the last one empty
A run report for a page-based pager: three page runs, each with its Fetch a page request for pages 1, 2 and 3 at size 50 and the invoices it returned, the last one empty
  • Set an endCondition that reflects your API’s actual empty state, and keep the page-index bound alongside it.
  • If the API has a rate limit, the per-page retry policy gives you natural throttling.