> Markdown version of https://tuna.am/en/docs/api/pagination/. Documentation index: [llms.txt](https://tuna.am/en/llms.txt), full text in one file: [llms-full.txt](https://tuna.am/en/llms-full.txt). Any page is available as Markdown: append `.md` to its URL without the trailing slash.

# Pagination, sorting and search

Methods that return lists return them in pages of up to 20 records. The page number and the total number of records come in the response headers, and the body is a plain JSON array.

## Page parameters

| Parameter   | What it sets                                                                                                          |
| ----------- | --------------------------------------------------------------------------------------------------------------------- |
| `page`      | Page number, starting from 1. Defaults to 1.                                                                          |
| `page_size` | Records per page, from 1 to 20. Defaults to 20. The API replaces a value above 20 with 20 without returning an error. |

A page past the last one returns an empty array, `[]`.

## Response headers

```bash
curl -i -H "Authorization: Bearer {API-key}" \
  "https://api.tuna.am/v1/monitors?page=2&page_size=20"

```

Output

```text
HTTP/2 200
content-type: application/json
x-page: 2
x-per-page: 20
x-total: 47
x-total-pages: 3
x-prev-page: 1
x-next-page: 3
…

```

| Header          | Contents                                                 |
| --------------- | -------------------------------------------------------- |
| `X-Page`        | Current page number.                                     |
| `X-Per-Page`    | Page size.                                               |
| `X-Total`       | Total number of records.                                 |
| `X-Total-Pages` | Total number of pages. Absent when there are no records. |
| `X-Prev-Page`   | Previous page number. Absent on the first page.          |
| `X-Next-Page`   | Next page number. Absent on the last page.               |

## Fetching the whole list

Request pages one by one while the response has `X-Next-Page`:

```python
import requests

headers = {"Authorization": "Bearer {API-key}"}
page = 1
while page:
    r = requests.get(
        "https://api.tuna.am/v1/monitors",
        headers=headers,
        params={"page": page, "page_size": 20},
    )
    r.raise_for_status()
    for monitor in r.json():
        print(monitor["name"])
    page = int(r.headers.get("X-Next-Page", 0))

```

If the list changes while you are going through it, a record may appear on two pages or on none. If completeness matters, compare the number of records you received with `X-Total`.

## Sorting

The `order_by` parameter sets the sort field. The allowed values, the default order and the direction for each value are listed in the parameter description on the method page. For example, in the [monitor list](https://tuna.am/en/docs/api/monitor-list.md), `last_fail_at` puts monitors with a recent failure first.

Each value has one fixed direction that cannot be changed. An unknown value returns `400` with the text `Invalid order_by`.

## Search

The `search` parameter is case-insensitive. The fields it searches are listed on the method page: for example, monitors are searched by name, address and host. Other filters (`project`, `status` and others) are also described on the method page.

```bash
curl -H "Authorization: Bearer {API-key}" \
  "https://api.tuna.am/v1/monitors?search=prod&order_by=last_fail_at"

```
