# Pagination

A list pages in one of two ways. Most lists are **page lists**: you ask for a page by its number. Four lists page **by time**, because their rows are a stream with no fixed pages: log search (`GET /v1/logs`), a custom metric’s raw samples, and a service’s scans and runs. Every list’s [reference](https://developer.watchtower15.com/api/reference/) entry says which it is.

## Page lists

A page list answers the rows of one page and where that page sits:

```json
{ "data": [ … ], "current_page": 1, "per_page": 25, "last_page": 4, "total": 87, "meta": { … } }
```

| Parameter | Meaning |
| --- | --- |
| `page` | The page to read, from 1; 1 by default. |
| `per_page` | Rows per page: 1 to 100, 25 by default. A metric source’s series and summary take 1 to 25 metrics and default to 25. |
| `limit` | The same as `per_page`, kept for existing clients. When both are sent, `per_page` is used. |
| `include_total` | Accepted and ignored: `total` is always present. |

| Field | Meaning |
| --- | --- |
| `current_page` | The page this answer holds. |
| `per_page` | How many rows a page holds. |
| `last_page` | The last page, at least 1. |
| `total` | How many rows match the filters, across every page. |

To read a whole list, ask for `page` 2, 3 and so on with the same filters while `current_page` is below `last_page`. A list with no rows answers `current_page` 1, `last_page` 1 and `total` 0. A `page` past the last answers no rows (`data` is empty) and the `current_page` you asked for. A `page` below 1, or a `per_page` outside its range, is [`422 invalid_request`](https://developer.watchtower15.com/api/errors/invalid_request/).

The rows are counted when the page is read, so a list that changes between two calls can shift a row from one page to the next. A page list ignores a `cursor`: it answers the page named by `page`, or the first page.

## Lists that page by time

Log search, a custom metric’s samples and a service’s scans and runs answer the rows and a cursor for the next page:

```json
{ "data": [ … ], "next_cursor": "eyJ…", "total": null, "meta": { … } }
```

| Parameter | Meaning |
| --- | --- |
| `per_page` | Rows per page. Log search takes 1 to 2,000 and defaults to 100; a custom metric’s samples take 1 to 10,000 and default to 1,000; a service’s scans and runs take 1 to 100 and default to 25. |
| `limit` | The same as `per_page`. |
| `cursor` | The `next_cursor` of the previous answer. Omit it for the first page. |

`next_cursor` is `null` on the last page. To read the whole list, repeat the call with the cursor until it is `null`. `page` is not read here: it is ignored like any parameter an operation does not take.

-   A cursor belongs to the call that made it: with a different filter, or sent to another operation, it is refused with `422 invalid_request` on `cursor`. Start again without it. A cursor stays valid when you change `per_page`.
-   Treat a cursor as opaque. Do not build or edit one.
-   `total` is `null` on log search and on a service’s runs, which are not counted. A service’s scans and a custom metric’s samples are counted once, on the first page, and the cursor carries that count to the later pages. A custom metric’s samples answer `next_cursor` and `total` inside `data`; see [charting custom metrics](https://developer.watchtower15.com/api/charting-custom-metrics/).

## On every list

-   `meta` appears only on operations that have facts about the whole answer, such as the counts of an alarm list.
-   Some lists keep the product’s own cap rather than paging past it: a domain’s scans and changes return the newest 20, a service’s checks the newest 50.
-   **A query parameter an operation does not take is ignored**, with no error: `offset` or `page_size` on any list, and `page` on a list that pages by time, change nothing. A misspelt filter is not applied, so a call that reads as filtered may answer every row: check the names against the [reference](https://developer.watchtower15.com/api/reference/).
-   The [SDKs](https://developer.watchtower15.com/sdks/) page in both ways for you. A copy of an SDK from before the page lists moved to `page` reads only the first page of a page list, without an error: upgrade it.

## Filters that take several values

A filter that takes a list, such as `status`, `health`, `connectivity`, `tag_uuids` or `security_health` on servers, accepts the values in any of three forms. All three read the same, and the reference says so on each list parameter:

```text
GET /v1/servers?health=warning&health=critical
GET /v1/servers?health=warning
GET /v1/servers?health[]=warning&health[]=critical
```

-   Repeat the parameter for several values, or send one value: a single value is a list of one. The bracket form (`health[]=warning`) is accepted too.
-   A value that is empty is dropped: `health=&health=warning` reads as `health=warning`, and a filter whose values are all empty is not applied.
-   An indexed form (`health[0]=warning&health[1]=critical`) is read as it is sent.
-   A value the operation does not know is refused with [`422 invalid_request`](https://developer.watchtower15.com/api/errors/invalid_request/), on the value’s own position (`health.0`), and the message lists the values it accepts.
-   A parameter that is not a list takes the last value sent: `per_page=1&per_page=2` reads as `per_page=2`.

**GET /v1/domains?per_page=2**

curl:

```bash
curl -G https://rest.watchtower15.com/v1/domains \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d per_page=2

# the next page: the same call, with page
curl -G https://rest.watchtower15.com/v1/domains \
  -H "Authorization: Bearer $WT15_API_KEY" \
  -d per_page=2 \
  -d page=2
```

PHP:

```php
$wt15 = WatchTower15\Client::fromEnvironment();

// autoPaging() fetches the next page only when you reach it.
foreach ($wt15->domains->list(['per_page' => 100])->autoPaging() as $domain) {
    echo $domain->name, ' ', $domain->healthStatus, "\n";
}
```

Node.js:

```ts
const wt15 = WatchTower15.fromEnvironment();

// for await fetches the next page only when you reach it.
for await (const domain of wt15.domains.list({ per_page: 100 })) {
  console.log(domain.name, domain.health_status);
}
```

Python:

```python
client = Client.from_environment()

# Iterating fetches the next page only when you reach it.
for domain in client.domains.list(per_page=100):
    print(domain.name, domain.health_status)
```

**Response (200 OK)**

```json
{
  "data": [
    { "uuid": "0f8c7a52-3c1e-4b9e-9d2a-6c41e5b7a913", "name": "example.com", "health_status": "healthy", … },
    { "uuid": "5d2e9b14-7a3f-4c6d-8e1b-2f4a6c8e0b37", "name": "shop.example.com", "health_status": "warning", … }
  ],
  "current_page": 1,
  "per_page": 2,
  "last_page": 3,
  "total": 5
}
```

Source: https://developer.watchtower15.com/api/pagination/
