Pagination
Request a page → Save the records → Send nextPageToken as pageToken → Stop when there's none
Request a page
Paginated list operations take two optional parameters:
pageSize: how many records to return. Most operations default to 50 and return at most 100. Larger values are reduced to the maximum. Each operation's reference lists its own default and maximum.pageToken: thenextPageTokenfrom the previous response. Leave it out to get the first page.
GET operations take these as query parameters:
curl -G "https://api.flownally.com/v1/teams" \
-H "Authorization: Bearer $FLOWNALLY_API_TOKEN" \
--data-urlencode "pageSize=100" \
--data-urlencode "pageToken=$NEXT_PAGE_TOKEN"POST /contacts:search and POST /segments/{segmentId}/contacts:search take them in the JSON body instead.
Read the response
Each page returns its records in an array named after the resource, such as contacts, conversations or teams. It also returns nextPageToken when there's another page.
When nextPageToken is missing or empty, you've reached the last page. Some responses also include totalSize, the total number of matching records.
A token is tied to the request that produced it, so a changed request or an expired token gets a 400. Treat page tokens as opaque strings:
- Pass the token back unchanged.
- Keep the request the same. Send the same filters and sort order with every page.
- Use the token soon. If it's rejected with
400, start again from the first page.
Some lists aren't paginated and return everything in one response, for example GET /inboxes, GET /tags, GET /users and GET /contacts/custom-fields.
Read a full list
This loop reads every contact that isn't archived, 100 at a time, oldest first:
let pageToken;
do {
const response = await fetch("https://api.flownally.com/v1/contacts:search", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.FLOWNALLY_API_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
query: { match: "all", conditions: [] },
orderBy: "createdAt",
pageSize: 100,
pageToken,
}),
});
if (!response.ok) throw new Error(`Contact search failed with ${response.status}`);
const page = await response.json();
await saveContacts(page.contacts);
pageToken = page.nextPageToken || undefined;
} while (pageToken);A job can stop partway. Make the function that saves contacts an upsert keyed by contact id, so you can run it again from the first page without creating duplicates.
Keep data in sync
Paging is for backfills: copying a list into your system once, or rebuilding it.
To keep that copy current, subscribe to webhooks such as contact.created and contact.updated instead of reading the whole list again.
Which page size should I use?
Larger pages mean fewer requests. Smaller pages use less memory and cost less to retry.
Start with the maximum for background jobs. Then lower pageSize if processing a page takes too long or retries become frequent.
FAQ
Why does my page token return 400?
400?The token expired, or the request doesn't match the one that produced it. Send the same filters and sort order with every page, and use each token soon after you get it. To recover, start again from the first page.
How do I know I've reached the last page?
The response has no nextPageToken, or it's empty. Stop the loop there.
Why doesn't GET /inboxes return a nextPageToken?
GET /inboxes return a nextPageToken?Some lists aren't paginated and return everything in one response. These include GET /inboxes, GET /tags, GET /users and GET /contacts/custom-fields.
What happens if I ask for more than the maximum page size?
Larger values are reduced to the maximum. Most operations return at most 100 records, and each operation's reference lists its own default and maximum.
Where do page parameters go in a search request?
POST /contacts:search and POST /segments/{segmentId}/contacts:search take pageSize and pageToken in the JSON body. GET operations take them as query parameters.