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

# Pagination

> Walk through a complete list with cursors.

Lists return one page of results and a cursor for the next page:

```bash theme={null}
curl "https://rest.newlead.ai/v1/leads?limit=2" \
  -H "Authorization: Bearer $NEWLEAD_API_KEY"
```

```json theme={null}
{
  "object": "list",
  "data": [
    { "object": "lead", "id": "00000000-0000-4000-8000-000000000003" },
    { "object": "lead", "id": "00000000-0000-4000-8000-000000000005" }
  ],
  "has_more": true,
  "next_cursor": "WyIyMDI2LTExLTE2VDEwOjAwOjAwLjAwMFoiLCI4YzBmNmMxZSJd"
}
```

The `data` items above are shortened.

| Parameter | Default | Maximum | Description |
| - | - | - | - |
| `limit` | `20` | `100` | Number of items per page, from 1 to 100. |
| `cursor` | None | 512 characters | The `next_cursor` of the previous page. Omit it for the first page. |

## Fetch every page

Repeat the request with `cursor` until `has_more` is `false`:

```javascript Node.js theme={null}
let cursor;

do {
  const url = new URL("https://rest.newlead.ai/v1/leads");
  url.searchParams.set("limit", "100");
  if (cursor) url.searchParams.set("cursor", cursor);

  const response = await fetch(url, {
    headers: { Authorization: `Bearer ${process.env.NEWLEAD_API_KEY}` },
  });
  if (!response.ok) throw new Error(`Request failed with ${response.status}`);

  const page = await response.json();
  for (const lead of page.data) console.log(lead.id);
  cursor = page.has_more ? page.next_cursor : undefined;
} while (cursor);
```

```python Python theme={null}
import os

import requests

params = {"limit": 100}

while True:
    response = requests.get(
        "https://rest.newlead.ai/v1/leads",
        headers={"Authorization": f"Bearer {os.environ['NEWLEAD_API_KEY']}"},
        params=params,
        timeout=30,
    )
    response.raise_for_status()

    page = response.json()
    for lead in page["data"]:
        print(lead["id"])

    if not page["has_more"]:
        break
    params["cursor"] = page["next_cursor"]
```

## Order of each list

| List | Order |
| - | - |
| `GET /v1/text_agents` | Newest first |
| `GET /v1/text_agents/{text_agent_id}/lead_fields` | The order set on the agent |
| `GET /v1/text_agents/{text_agent_id}/templates` | Newest first |
| `GET /v1/voice_agents` | Newest first |
| `GET /v1/leads` | Newest first |
| `GET /v1/leads/{lead_id}/messages` | Newest first |
| `GET /v1/calls` | Newest first, by `started_at` |
| `GET /v1/webhook_endpoints` | Newest first |
| `GET /v1/webhook_endpoints/{webhook_endpoint_id}/deliveries` | Newest first |
| `GET /v1/properties` | Most recently added first |

## Rules

* Treat the cursor as opaque: do not build or parse it.
* Use a cursor only with the list that returned it.
* Keep the same filters on every page.
* A `limit` outside 1 to 100 returns `400 parameter_invalid`.
* A cursor that is not valid returns `400 parameter_invalid`. This also happens if the item it points to was deleted: start again from the first page.


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