Color theme

Pagination

Walk a list a page at a time.

Pages

A list answers a page at a time, newest first:

{
  "data": [{ "id": "ent_ba2kccpsr4", "name": "Ana Souza" }],
  "hasMore": true,
  "nextCursor": "ent_ba2kccpsr4"
}
data
array, required

The page's items.

hasMore
boolean, required

Whether another page follows.

nextCursor
string | null, required

Pass as cursor to get the next page; null on the last one.

A page holds 20 items unless you ask for another limit, up to 100.

Walking every page

Ask for the next page with nextCursor until hasMore is false:

let cursor = null

do {
  const url = new URL('https://api.swite.co/v1/leads')

  url.searchParams.set('limit', '100')

  if (cursor) {
    url.searchParams.set('cursor', cursor)
  }

  const response = await fetch(url, {
    headers: { Authorization: `Bearer ${process.env.SWITE_API_KEY}` },
  })
  const page = await response.json()

  for (const lead of page.data) {
    // …
  }

  cursor = page.nextCursor
} while (cursor)

Keep the other parameters, such as search, the same on every page, and treat a cursor as opaque: pass it back as you received it.

A stale cursor

A cursor points at the last item of the page you read. If that item is deleted before you ask for the next page, the API answers 400 with request.invalid rather than starting over from the first page, which would make a walk through every page loop. List again from the first page.