Skip to main content

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​

ParameterWhat it sets
pagePage number, starting from 1. Defaults to 1.
page_sizeRecords 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​

curl -i -H "Authorization: Bearer {API-key}" \
"https://api.tuna.am/v1/monitors?page=2&page_size=20"
Output
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
…
HeaderContents
X-PageCurrent page number.
X-Per-PagePage size.
X-TotalTotal number of records.
X-Total-PagesTotal number of pages. Absent when there are no records.
X-Prev-PagePrevious page number. Absent on the first page.
X-Next-PageNext page number. Absent on the last page.

Fetching the whole list​

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

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, 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.

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.

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