# Pagination

> Read long lists page by page with limit and next_cursor.

Source: https://imario.ai/docs/api/pagination

## The list object

Every endpoint that lists objects answers with the same shape:

```json
{
  "object": "list",
  "data": [{ "object": "study", "id": "c41d7e2a-9b3f-4a5c-8e6d-0f1a2b3c4d5e" }],
  "has_more": true,
  "next_cursor": "eyJ0IjoiMjAyNi0xMS0yNFQxMDowMjoxMSswMDowMCIsImkiOiJjNDFkN2UyYSJ9"
}
```

Lists are newest first unless the endpoint says otherwise. A study's responses, for example, come in question order.

## Parameters

| Parameter | Meaning |
|---|---|
| `limit` | How many objects to return, from 1 to 100. Default 20. A value outside that is refused with 400 `parameter_invalid` |
| `cursor` | The `next_cursor` of the page before. Leave it out for the first page |

`has_more` is `true` when there is another page, and then `next_cursor` holds the cursor for it. On the last page `has_more` is `false` and `next_cursor` is `null`.

A cursor is opaque: pass it back exactly as you received it, with the same filters as the first page. A cursor the API did not issue is refused with 400 `cursor_invalid` (one longer than 200 characters with 400 `parameter_invalid`). Two lists come whole, in one page, and ignore `limit` and `cursor`: a study's segments and a connection's items, whose `has_more` is always `false`. Paging never skips or repeats an object when new ones are created between two pages.

## Read every page

```python
import os
import requests

API = "https://api.imario.ai/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['IMARIO_API_KEY']}"}

def every(path, **params):
    params = {"limit": 100, **params}
    while True:
        page = requests.get(f"{API}{path}", headers=HEADERS, params=params, timeout=30).json()
        yield from page["data"]
        if not page["has_more"]:
            return
        params["cursor"] = page["next_cursor"]

for study in every("/studies", status="completed"):
    print(study["id"], study["question"])
```
