# Pagination

> Page through long lists of clients, members or tasks in the Quenza API with offset and limit, and follow the next link until the last page.

Source: https://quenza.com/help/api/pagination/

[List clients](https://quenza.com/help/api/clients/list-clients/), [List members](https://quenza.com/help/api/members/list-members/) and [List tasks](https://quenza.com/help/api/tasks/list-tasks/) return their records in pages. You choose the page with two query parameters:

- `limit`: how many records you get, from 1 to 100. The default is 20. A larger number counts as 100.
- `offset`: how many records to skip from the start. The default is 0.

So `?limit=50&offset=100` returns records 101 to 150.

## What a page looks like

Every list answers with the same three parts:

```json
{
  "data": [ ... ],
  "meta": { "limit": 50, "offset": 100, "total": 212 },
  "links": {
    "next": "https://developers.quenza.com/v1/clients?limit=50&offset=150",
    "prev": "https://developers.quenza.com/v1/clients?limit=50&offset=50"
  }
}
```

- `data` holds the records of this page.
- `meta.total` is the number of records in all pages together.
- `links.next` is the full URL of the next page, or `null` on the last page. `links.prev` is the page before, or `null` on the first page.

## Get every record

Follow `links.next` until it is `null`. This reads all clients of the workspace, 100 at a time:

**JavaScript**

```js
async function allClients() {
  const clients = [];
  let url = 'https://developers.quenza.com/v1/clients?limit=100';
  while (url) {
    const response = await fetch(url, {
      headers: { Authorization: `Bearer ${process.env.QUENZA_API_TOKEN}`, Accept: 'application/json' },
    });
    if (!response.ok) throw new Error(`Quenza API error ${response.status}`);
    const { data, links } = await response.json();
    clients.push(...data);
    url = links.next;
  }
  return clients;
}
```

**Python**

```python
import os
import requests

def all_clients():
    clients = []
    url = "https://developers.quenza.com/v1/clients?limit=100"
    headers = {"Authorization": f"Bearer {os.environ['QUENZA_API_TOKEN']}", "Accept": "application/json"}
    while url:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        page = response.json()
        clients += page["data"]
        url = page["links"]["next"]
    return clients
```

**PHP**

```php
<?php

require 'vendor/autoload.php';

function allClients(): array
{
    $http = new GuzzleHttp\Client(['headers' => [
        'Authorization' => 'Bearer ' . getenv('QUENZA_API_TOKEN'),
        'Accept' => 'application/json',
    ]]);
    $clients = [];
    $url = 'https://developers.quenza.com/v1/clients?limit=100';
    while ($url !== null) {
        $page = json_decode($http->get($url)->getBody(), true);
        $clients = array_merge($clients, $page['data']);
        $url = $page['links']['next'];
    }
    return $clients;
}
```


## Order of the records

Clients and members come sorted by last name, then first name. Tasks come newest first. When someone adds a client while you page through the list, the records after it move one place; read the whole list again if you need an exact copy.
