# Paging and lists

## Paging with a cursor

Every list answers one page at a time:

```json
{
  "items": [ … ],
  "next_cursor": "…"
}
```

- `limit` sets the page size: from 1 to 200, 50 when you leave it out.
- When `next_cursor` is a string, there is another page: send it back as `cursor` with the same parameters.
- When `next_cursor` is `null`, that was the last page.

A cursor stays correct while items are added or changed, so paging through a project of thousands of tasks never skips or repeats one, the way page numbers can. The one exception is a list sorted by the time of change (`sort=-updated` or `ORDER BY updated`): a task changed while you page moves to the other end of the order, so it can be passed over or come twice. To keep a copy in step, page in the default order (or by `key`) and then ask for `updated_since`.

:::tabs

```sh title="cURL"
cursor=""
while :; do
  page=$(curl -sG "$PROJQO_API/orgs/$PROJQO_ORG/projects/$PROJECT/tasks" \
    -H "Authorization: Bearer $PROJQO_TOKEN" \
    --data-urlencode "limit=200" \
    ${cursor:+--data-urlencode "cursor=$cursor"})
  echo "$page" | jq -r '.items[].key'
  cursor=$(echo "$page" | jq -r '.next_cursor // empty')
  [ -z "$cursor" ] && break
done
```

```js title="JavaScript"
const api = process.env.PROJQO_API;
const headers = { Authorization: `Bearer ${process.env.PROJQO_TOKEN}` };

async function* allTasks(org, project) {
  let cursor = null;
  do {
    const url = new URL(`${api}/orgs/${org}/projects/${project}/tasks`);
    url.searchParams.set("limit", "200");
    if (cursor) url.searchParams.set("cursor", cursor);
    const page = await (await fetch(url, { headers })).json();
    yield* page.items;
    cursor = page.next_cursor;
  } while (cursor);
}
```

```py title="Python"
import os

import requests

api = os.environ["PROJQO_API"]
headers = {"Authorization": f"Bearer {os.environ['PROJQO_TOKEN']}"}

def all_tasks(org, project):
    params = {"limit": 200}
    while True:
        page = requests.get(f"{api}/orgs/{org}/projects/{project}/tasks", headers=headers, params=params).json()
        yield from page["items"]
        if not page["next_cursor"]:
            break
        params["cursor"] = page["next_cursor"]
```

:::

## Counting

- `count=true` adds `total`, the number of items in the whole list, to the answer.
- `/count` calls (such as `GET /tasks/count?q=…`) answer the number alone, without the items.

## Filtering

- **Tasks** are filtered with the query language of Advanced search, in `q`: `q=project = WEB AND status = "In review"`. See [the query language](/guides/query-language/).
- **Other lists** take plain parameters, such as `?project=`, `?status=` or `?updated_since=`. Each list's parameters are in the reference.
- **A saved filter**: `filter={id}` runs a saved filter the person may open.

## Sorting

`sort` takes one or more field keys, separated by commas; `-` in front sorts that key from the highest down:

```text
sort=-updated,key
```

For tasks you can also end `q` with `ORDER BY`: `q=project = WEB ORDER BY rank`.

## Choosing fields

- `fields=summary,status,assignee` returns only those fields of each item (with its id), which makes big lists smaller and faster.
- `expand=comments,watchers,changes` puts related lists inside each item: the first 20 of each, with a cursor for the rest.

## Keeping a copy in step

To keep your own copy of projqo's data up to date without reading everything again:

1. Read everything once, page by page.
2. Afterwards, ask only for what changed: every list takes `updated_since` (a date-time).
3. Ask for what was deleted: `GET /tasks/deleted?since=…` (and the same for Docs pages).

Or subscribe to [webhooks](/guides/webhooks/) and hear about each change as it happens.
