# Paginazione (https://docs.getbeel.com/it/api/pagination)

Gli elenchi usano una paginazione a cursore con limit, cursor, has_more e next_cursor.

Gli endpoint che restituiscono elenchi (`GET /invoices`, `GET /received-invoices`) sono ordinati per `invoice_date`, dalla più recente, e restituiscono una pagina alla volta:

```json
{
  "object": "list",
  "data": [{ "object": "invoice", "id": "fe_3f1c…" }],
  "has_more": true,
  "next_cursor": "WyIyMDI2LTEwLTE1IiwiZmVfM2YxYyJd"
}
```

| Parametro | Descrizione                                     |
| --------- | ----------------------------------------------- |
| `limit`   | Elementi per pagina, da 1 a 100. Predefinito 20 |
| `cursor`  | Il `next_cursor` della pagina precedente        |

Quando `has_more` è `false`, `next_cursor` è `null` e hai letto tutto. Il cursore è opaco: non costruirlo a mano. I filtri (per esempio `status`, `date_from`, `date_to`) vanno ripetuti su ogni pagina.

## Leggere tutte le pagine [#leggere-tutte-le-pagine]

```js
async function* allInvoices(params = {}) {
  let cursor;

  do {
    const query = new URLSearchParams({
      ...params,
      limit: '100',
      ...(cursor ? { cursor } : {}),
    });
    const res = await fetch(
      `https://www.getbeel.com/api/v1/invoices?${query}`,
      {
        headers: { Authorization: `Bearer ${process.env.GETBEEL_API_KEY}` },
      },
    );
    const page = await res.json();

    yield* page.data;
    cursor = page.has_more ? page.next_cursor : undefined;
  } while (cursor);
}

for await (const invoice of allInvoices({ status: 'rejected' })) {
  console.log(invoice.number, invoice.sdi.error_message);
}
```
