# Pagination

> How Sofia Space API lists are paged with limit and cursor, which endpoints page and which return everything at once, and how to read all pages in a loop.

Source: https://docs.sofiaspace.uz/api/pagination/

Some Sofia Space API lists can grow without limit — tasks in a project, a task's comments, search results — so they come **a page at a time**. You ask for a page size with `limit` and continue with the `nextCursor` the previous page gave you. Smaller lists (projects, statuses, spaces, a space's pages) come back whole.

**Who this is for:** developers calling the [Sofia Space API](https://docs.sofiaspace.uz/api/overview.md).

## How paging works

1. Call the list without a cursor. Add `limit` to choose the page size, or leave it out for the default.
2. Read `hasMore` and `nextCursor` in the response.
3. While `hasMore` is `true`, call again with the same parameters plus `cursor=<nextCursor>`.

```json
{
  "items": [ { "id": "7c3e9a15-2d4f-4b6a-9e8c-1f3a5b7d9e20", "taskNumber": 42, "title": "Prepare quote for deal #1042" } ],
  "nextCursor": "eyJuIjo0Mn0",
  "hasMore": true
}
```

- `nextCursor` is `null` on the last page.
- A cursor is **opaque**: don't build or change it. A damaged cursor gives `400 CURSOR_INVALID`.
- Keep every other parameter the same while paging. A cursor remembers the sort it was made for: sending it with a different `sort` (or, for tasks, a different sort field) gives `400 INVALID_ARGUMENT` — start again from the first page. Changing a filter isn't detected: you just get a page of a different list, so don't.
- `limit` above the maximum is lowered to the maximum, not refused. `limit=0` or no `limit` means the default.

## Which endpoints page

| Endpoint | List key | Default | Maximum |
| --- | --- | --- | --- |
| [`GET /api/v1/projects/{projectId}/tasks`](https://docs.sofiaspace.uz/api/tasks.md#list-tasks-in-a-project) | `items` | 100 | 100 |
| [`GET /api/v1/tasks/{taskId}/comments`](https://docs.sofiaspace.uz/api/task-comments-links-blockers.md#list-a-tasks-comments) | `comments` | 50 | 100 |
| [`GET /api/v1/search`](https://docs.sofiaspace.uz/api/search.md) | `items` | 20 | 50 |

The task comments list keeps its older key, `comments`, instead of `items`; the paging fields are the same. A page of comments holds up to `limit` top-level comments **plus their replies**, so it can be longer than `limit` — always page on `nextCursor`, never on the count.

Every other list in this reference returns everything in one response, under a named key such as `projects`, `statuses`, `spaces` or `pages`.

## Read every page: example

```bash
#!/usr/bin/env bash
# Print every task in a project, 100 at a time.
BASE=https://acme.sofiaspace.uz/api/v1
PROJECT=9d2e7a10-4b6c-4e8f-a1d3-5c7e9b2f4a68
CURSOR=""
while : ; do
  PAGE=$(curl -s -G "$BASE/projects/$PROJECT/tasks" \
    -H "Authorization: Bearer $SOFIA_TOKEN" \
    --data-urlencode "limit=100" \
    --data-urlencode "description=false" \
    ${CURSOR:+--data-urlencode "cursor=$CURSOR"})
  echo "$PAGE" | jq -r '.items[] | "\(.taskNumber)\t\(.title)"'
  [ "$(echo "$PAGE" | jq -r .hasMore)" = "true" ] || break
  CURSOR=$(echo "$PAGE" | jq -r .nextCursor)
done
```

The same loop in Python:

```python
import os, requests

BASE = "https://acme.sofiaspace.uz/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['SOFIA_TOKEN']}"}

def all_tasks(project_id):
    params = {"limit": 100, "description": "false"}
    while True:
        page = requests.get(f"{BASE}/projects/{project_id}/tasks", headers=HEADERS, params=params, timeout=30)
        page.raise_for_status()
        body = page.json()
        yield from body["items"]
        if not body["hasMore"]:
            return
        params["cursor"] = body["nextCursor"]
```

`description=false` leaves task descriptions out of the list, which makes big exports much faster. Read one task's description with [`GET /api/v1/tasks/{taskId}`](https://docs.sofiaspace.uz/api/tasks.md#get-a-task).

## What you can't do

- You can't jump to page N or get a total count of tasks; only "next page".
- You can't page backwards.

## Related

- [Tasks](https://docs.sofiaspace.uz/api/tasks.md)
- [Search](https://docs.sofiaspace.uz/api/search.md)
- [Errors](https://docs.sofiaspace.uz/api/errors.md)
