# Pagination

Read long lists page by page with cursors, and pull only what is new.

Lists come in pages. Each answer has the items in `data` and, in `pagination.nextCursor`, where the next page starts:

```json
{
  "data": [],
  "pagination": { "nextCursor": "eyJ2IjoxLCJiaW5kaW5nIjoiZXhhbXBsZSJ9" }
}
```

- `limit` sets how many items a page has, from 1 to 100. The default is 50.
- Leave out `cursor` for the first page. For the next one, send `nextCursor` as `cursor` and repeat the same filters and order.
- `nextCursor` is `null` on the last page.
- A cursor only works with the request it came from. With other filters, or another order, it is refused with a 400. Don't read or build cursors yourself.
- Conversations are sorted by their latest message, so one that gets a message while you read the pages moves to the top, and you can miss it.

## Read every page

**Node.js**:

```js
async function* everyItem(path, query = {}) {
  let cursor;
  do {
    const search = new URLSearchParams({ ...query, limit: "100", ...(cursor && { cursor }) });
    const response = await fetch(`https://api.chatwise.so${path}?${search}`, {
      headers: { Authorization: `Bearer ${process.env.CHATWISE_API_KEY}` },
    });
    const body = await response.json();
    if (!response.ok) throw new Error(`${response.status}: ${body.message}`);
    yield* body.data;
    cursor = body.pagination.nextCursor;
  } while (cursor);
}

for await (const message of everyItem("/v1/messages", { channel: "WHATSAPP" })) {
  console.log(message.id, message.text);
}
```

**Python**:

```python
import json
import os
import urllib.parse
import urllib.request


def every_item(path, **query):
    cursor = None
    while True:
        search = urllib.parse.urlencode({**query, "limit": 100, **({"cursor": cursor} if cursor else {})})
        request = urllib.request.Request(
            f"https://api.chatwise.so{path}?{search}",
            headers={"Authorization": f"Bearer {os.environ['CHATWISE_API_KEY']}"},
        )
        with urllib.request.urlopen(request) as response:
            body = json.load(response)
        yield from body["data"]
        cursor = body["pagination"]["nextCursor"]
        if not cursor:
            return


for message in every_item("/v1/messages", channel="WHATSAPP"):
    print(message["id"], message["text"])
```

## Which routes have pages

[Conversations](https://chatwise.so/docs/api/list-conversations.md), [messages](https://chatwise.so/docs/api/list-messages.md), [a conversation's messages](https://chatwise.so/docs/api/list-conversation-messages.md), [WhatsApp templates](https://chatwise.so/docs/api/list-whatsapp-templates.md) and [webhook deliveries](https://chatwise.so/docs/api/list-webhook-deliveries.md). Webhook endpoints and event types are short lists and come whole.

## Filter by date

Conversations and messages take `from` and `to`: ISO 8601 times with a time zone and at most milliseconds, such as `2026-11-01T00:00:00Z`. `from` is included, `to` isn't, and `from` must come before `to`. In a URL, write the `+` of a time zone such as `+05:30` as `%2B`.

## Pull only new messages

To copy messages on a schedule, sort by when Chatwise stored them. That also finds imported history, whose `sentAt` is older:

```bash
curl 'https://api.chatwise.so/v1/messages?timeField=ingestedAt&order=asc&from=2026-11-13T00:00:00Z&to=2026-11-14T00:00:00Z&limit=100' \
  -H "Authorization: Bearer $CHATWISE_API_KEY"
```

Keep `to` fixed while you read the pages of one run, let runs overlap a little, and dedupe by message `id`. To get messages as they arrive instead, use [webhooks](https://chatwise.so/docs/webhooks.md).
