# Pagination (https://docs.getbeel.com/en/api/pagination)

Lists use cursor pagination with limit, cursor, has_more and next_cursor.

Endpoints that return lists (`GET /invoices`, `GET /received-invoices`) are sorted by `invoice_date`, newest first, and return one page at a time:

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

| Parameter | Description                            |
| --------- | -------------------------------------- |
| `limit`   | Items per page, 1 to 100. Default 20   |
| `cursor`  | The `next_cursor` of the previous page |

When `has_more` is `false`, `next_cursor` is `null` and you have read everything. The cursor is opaque: don't build it yourself. Repeat filters (for example `status`, `date_from`, `date_to`) on every page.

## Reading every page [#reading-every-page]

```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);
}
```
