> ## Documentation Index
> Fetch the complete documentation index at: https://docs.routa.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# Pagination

> Walk through lists with async iterators or control the cursor yourself with .page().

The SDK list methods return an object that is both an **async iterator** and has a `.page()` method. They use the API's [cursor pagination](https://docs.routa.chat/en/reliability/pagination.md).

## Iterate everything

The iterator fetches the following pages on demand:

```ts theme={null}
for await (const message of routa.messages.list({ direction: 'inbound', limit: 50 })) {
  console.log(message.id, message.from, message.content)
}
```

`limit` sets the size of each page (1 to 100), not the total. To stop early, use `break`:

```ts theme={null}
for await (const event of routa.events.list({ type: 'message.failed' })) {
  await handle(event)
  if (shouldStop()) break
}
```

## One page at a time

`.page()` returns a single page, with the pagination metadata:

```ts theme={null}
const page = await routa.messages.list({ limit: 20 }).page()

console.log(page.data.length) // items on the page
console.log(page.hasMore) // are there more pages?
console.log(page.nextCursor) // cursor of the next page, or null
```

For the next page, pass `cursor`:

```ts theme={null}
const next = await routa.messages.list({ limit: 20, cursor: page.nextCursor ?? undefined }).page()
```

The shape of `Page<T>` is:

```ts theme={null}
interface Page<T> {
  readonly data: readonly T[]
  readonly hasMore: boolean
  readonly nextCursor: string | null
}
```

## Paginated methods

| Method | Filters |
| - | - |
| `routa.messages.list()` | `direction`, `limit`, `cursor` |
| `routa.events.list()` | `type`, `after`, `limit` |
| `routa.templates.list()` | `channel`, `limit`, `cursor` |
| `routa.webhooks.deliveries.list(webhookId)` | `state`, `limit`, `cursor` |
| `routa.webhooks.list()` | Returns a `Page` with every endpoint (5 at most). |
| `partner.projects.list()` | `externalRef`, `hasChannel`, `limit`, `cursor` |
| `partner.channels.list()` | `status`, `projectId`, `externalRef`, `limit`, `cursor` |

> [!NOTE]
>
> In `routa.events.list()`, the cursor is the ID of the last event, passed in `after`. See [Reconcile events](https://docs.routa.chat/en/webhooks/reconciliation.md).

The two listings of [`RoutaPartner`](https://docs.routa.chat/en/sdk/partner) work the same way, but `limit` goes from 1 to 200 and the default is 50.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.