# Page through long lists

> Get every row of a long list, such as all of a site's searches or pages, a page at a time with a cursor.

Endpoints that return a list, such as a site's searches, pages or tracked keywords, send it a page at a time. Each response says how many rows there are in all and how to get the next page.

## The page object

Every list response has a `page` next to its rows:

```json
{
  "rows": ["..."],
  "page": { "shown": 25, "total": 140, "nextCursor": "p.3k9x1z" }
}
```

<ResponseField name="page.shown" type="integer" required>
  Rows in this response.
</ResponseField>

<ResponseField name="page.total" type="integer" required>
  Rows across all pages.
</ResponseField>

<ResponseField name="page.nextCursor" type="string | null" required>
  Send it as `cursor` to get the next page. `null` on the last page.
</ResponseField>

## Parameters

<ParamField query="limit" type="integer">
  Rows per page. Each endpoint's reference shows its default and maximum, often 25 and 100.
</ParamField>

<ParamField query="cursor" type="string">
  The page to get: `page.nextCursor` from the previous response. Leave it out for the first page.
</ParamField>

## Get every row

Send the same request again with `cursor` set to the last `page.nextCursor`, until it's `null`. Keep every other parameter the same: a cursor belongs to the request that returned it, and sending it with a different `range` or filter is refused with `invalid_input`.

<CodeGroup>
```bash title="curl"
# First page
curl "https://api.onsite.so/api/v1/sites/example.com/performance/queries?range=last_28d&limit=200" \
  -H "Authorization: Bearer $ONSITE_API_KEY"

# Next page: the same request, plus the cursor from page.nextCursor
curl "https://api.onsite.so/api/v1/sites/example.com/performance/queries?range=last_28d&limit=200&cursor=p.3k9x1z" \
  -H "Authorization: Bearer $ONSITE_API_KEY"
```

```javascript title="JavaScript"
async function allRows(path, key) {
  const rows = [];
  let cursor = null;

  do {
    const url = new URL(`https://api.onsite.so/api/v1${path}`);
    url.searchParams.set("limit", "200");
    if (cursor) url.searchParams.set("cursor", cursor);

    const response = await fetch(url, { headers: { Authorization: `Bearer ${process.env.ONSITE_API_KEY}` } });
    const body = await response.json();
    if (!response.ok) throw new Error(`${body.error.code}: ${body.error.message}`);

    rows.push(...body[key]);
    cursor = body.page.nextCursor;
  } while (cursor);

  return rows;
}

const queries = await allRows("/sites/example.com/performance/queries?range=last_28d", "rows");
```

```python title="Python"
import os
import requests

def all_rows(path, key):
    rows, cursor = [], None
    while True:
        params = {"limit": 200}
        if cursor:
            params["cursor"] = cursor
        response = requests.get(
            f"https://api.onsite.so/api/v1{path}",
            params=params,
            headers={"Authorization": f"Bearer {os.environ['ONSITE_API_KEY']}"},
        )
        body = response.json()
        response.raise_for_status()
        rows += body[key]
        cursor = body["page"]["nextCursor"]
        if not cursor:
            return rows

queries = all_rows("/sites/example.com/performance/queries?range=last_28d", "rows")
```
</CodeGroup>

<Tip>
Each page counts toward your [rate limit](/api/rate-limits). Ask for the largest `limit` the endpoint allows, and narrow the list with filters such as `query`, `page` or `country` before paging through all of it.
</Tip>

## Related

<CardGroup cols={2}>
  <Card title="Rate limits" icon="gauge" href="/api/rate-limits">
    How many requests a key can make.
  </Card>
  <Card title="Errors" icon="circle-alert" href="/api/errors">
    What each error means.
  </Card>
</CardGroup>
