---
docId: doc_04167614260c4255b9d14f5e7a1e2c0f
title: Pagination
description: Read long lists page by page with list, or loop over every item with iterate and paginate.
sidebarTitle: Pagination
---

# Pagination

Use `iterate` to loop over a full list, or `list` to get one page. Both take the same filters.

```ts
// 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 }`.** `nextPageToken` is `null` on the last page.
- **`iterate` loads 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 with `break`, it requests no more pages.
- **`limit` sets the page size.** Each list documents its own range in the API reference. `iterate` takes the same params as `list`, without `pageToken`.
- **Keep filters and workspace.** Send a `pageToken` with the same filters, and from a client with the same `workspace` option. Contacts, chats, campaigns and their audience, posts and comment threads, and scheduled messages answer `InvalidRequestError` with `INVALID_REQUEST` to a token from another workspace; conversations answer `InvalidRequestError` with `INVALID_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.list` returns `{ templates, warnings }`.

See [Pagination in the API](/api-reference/pagination) for how to use tokens.

## Methods with `iterate`

You can use `iterate` on:

- Agents, `agents.versions`, `agents.publications` and `agents.files`.
- `files`, `secrets`, `chat`, `clients`, `conversations`, `conversations.messages` and `cases`.
- `campaigns` and `campaigns.audience`.
- `posts` and its threads.
- Scheduled messages, `channels.connections` and `agents.evals.runs`.

Use `tools.databases.iterateQuery` to loop over query rows. Use `tools.functions.iterateRuns` for function executions.

<span id="lists-that-page-differently"></span>

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.

<span id="conversation-pages"></span>

## 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`.

```ts
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 is `null`: `iterate`, `messages.iterate` and `iterateSearch` do it for you.
- **The item iterators drop `included` and `meta`.** Use `list` or `search` page by page when you need them.
- **A search session lasts 15 minutes.** The whole `iterateSearch` loop must finish in that time; after it, the next page throws `GoneError` with `SEARCH_SESSION_EXPIRED`, and you start the search again. A token sent with a different query or filters throws `InvalidRequestError` with `INVALID_CURSOR`.

See [Pagination in the API](/api-reference/pagination#conversations-v2).

## 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 }`.

```ts
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

```ts
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](/api-reference/contacts/list)
- [Retries](/sdk/retries): each page request is retried like any other call.
- [Types](/sdk/types): `Page` and `ListParams`.
