Pagination
Use iterate to loop over a full list, or list to get one page. Both take the same filters.
// One page
const first = await platic.clients.list({ limit: 50 });
console.log(first.data, first.nextPageToken);
if (first.nextPageToken) {
const second = await platic.clients.list({ limit: 50, pageToken: first.nextPageToken });
}
// Every item, one page at a time
for await (const client of platic.clients.iterate({ limit: 100 })) {
console.log(client.id, client.name);
} What you need to know
- A page is
{ data, nextPageToken }.nextPageTokenisnullon the last page. iterateloads pages when you need them. It requests the next page only when the loop reaches the end of the current one. If you leave the loop withbreak, it requests no more pages.limitsets the page size. Each list documents its own range in the API reference.iteratetakes the same params aslist, withoutpageToken.- Keep filters and workspace. Send a
pageTokenwith the same filters, and from a client with the sameworkspaceoption. Contacts, chats, campaigns and their audience, posts and comment threads, and scheduled messages answerInvalidRequestErrorwithINVALID_REQUESTto a token from another workspace; conversations answerInvalidRequestErrorwithINVALID_CURSOR. - Some lists are not paged. Tools, MCP servers, webhooks, channels, web chats, tags, custom fields, eval profiles, scenarios and suites return an array.
whatsapp.templates.listreturns{ templates, warnings }.
See Pagination in the API for how to use tokens.
Methods with iterate
You can use iterate on:
- Agents,
agents.versions,agents.publicationsandagents.files. files,secrets,chat,clients,conversations,conversations.messagesandcases.campaignsandcampaigns.audience.postsand its threads.- Scheduled messages,
channels.connectionsandagents.evals.runs.
Use tools.databases.iterateQuery to loop over query rows. Use tools.functions.iterateRuns for function executions.
Use conversations.iterateSearch for search results and clients.iterateConversations for a contact's conversations. Both follow pages with { data, nextPageToken }. Search stops at candidateLimit (250 by default).
search.iterate follows the V1 search. Its total is exact when totalIsExact is true; when false, it is a lower bound on matches.
Conversation pages
conversations.list, conversations.messages.list and conversations.search return more than { data, nextPageToken }: also pagination, meta and, when you ask for related records, included.
const page = await platic.conversations.list({ operation: ["supervised"], limit: 50, include: ["agents"] });
console.log(page.data.length, page.nextPageToken, page.included?.agents);
for await (const hit of platic.conversations.iterateSearch({ query: "invoice", mode: "lexical", limit: 100 })) {
console.log(hit.id, hit.snippet);
} - A page can be short, or even empty, and still have a
nextPageToken. Keep going until it isnull:iterate,messages.iterateanditerateSearchdo it for you. - The item iterators drop
includedandmeta. Uselistorsearchpage by page when you need them. - A search session lasts 15 minutes. The whole
iterateSearchloop must finish in that time; after it, the next page throwsGoneErrorwithSEARCH_SESSION_EXPIRED, and you start the search again. A token sent with a different query or filters throwsInvalidRequestErrorwithINVALID_CURSOR.
Iterate your own function
Use paginate(fetchPage) to loop over a token-paged function with an async iterator. The function receives the token, or undefined for the first page, and returns { data, nextPageToken }.
import { paginate } from "platic-sdk";
const agents = paginate((pageToken) => platic.agents.list({ limit: 50, pageToken }));
for await (const agent of agents) {
console.log(agent.name);
} If the API returns the same nextPageToken twice, paginate throws PlaticError to prevent an infinite loop.
Collect everything
const everyone = [];
for await (const client of platic.clients.iterate()) everyone.push(client); Do this only for lists you know are small. For large workspaces, process each item inside the loop.
Next steps
- Contacts list
- Retries: each page request is retried like any other call.
- Types:
PageandListParams.