> The index of all pages is at https://docs.afffast.com/llms.txt.

# Pagination and expand

Read long lists with cursors, and get related objects with expand[].

## The list envelope

Each list operation returns the same envelope:

```json
{
  "object": "list",
  "url": "/v1/conversions",
  "has_more": true,
  "data": [{ "id": "conv_...", "object": "conversion" }]
}
```

`data` has the objects, newest first. `has_more` is `true` when more objects follow this page.

## Cursors

The API uses cursors, not page numbers. A cursor is the ID of an object.

| Parameter        | Meaning                                                              |
| ---------------- | -------------------------------------------------------------------- |
| `limit`          | The size of the page. From 1 to 100. The default is 20.              |
| `starting_after` | The ID of an object. You get the objects that are **older** than it. |
| `ending_before`  | The ID of an object. You get the objects that are **newer** than it. |

Use only one of `starting_after` and `ending_before`. If you send both, or a `limit` outside the range, the API returns [`parameter_invalid`](https://docs.afffast.com/errors/parameter-invalid.md). The response also has a `Link` header for the next page.

To read all objects, use the ID of the last object of a page as `starting_after`. Stop when `has_more` is `false`.

```js
async function* listAll(path) {
  let startingAfter;
  while (true) {
    const url = new URL(`https://api.afffast.com${path}`);
    url.searchParams.set("limit", "100");
    if (startingAfter) url.searchParams.set("starting_after", startingAfter);
    const response = await fetch(url, {
      headers: { Authorization: `Bearer ${process.env.AFFFAST_API_KEY}` },
    });
    const page = await response.json();
    yield* page.data;
    if (!page.has_more) return;
    startingAfter = page.data.at(-1).id;
  }
}
```

A new object that comes while you read does not move the pages. A page does not repeat an object and does not miss one.

Some lists have a `sort` parameter. `GET /v1/conversions` sorts by `-sold_at` by default. Conversions with the same time keep a stable order.

## Statistics are not lists

`GET /v1/stats` and `GET /v1/stats/clicks` are not paginated. They return one object with a `rows` array. Use `group_by` and the date range to control the size.

## Expand

Many objects have a field with the ID of a related object, for example `program` on a conversion. Add `expand[]` to get the whole object in place of the ID.

```bash
curl -g "https://api.afffast.com/v1/conversions?expand[]=program&expand[]=click" \
  -H "Authorization: Bearer $AFFFAST_API_KEY"
```

- Use `expand[]` more than one time to expand more than one field.
- On a list, a path can start with `data.`, for example `data.program`.
- A path can have 3 levels at most.
- A field that you cannot expand returns [`expand_invalid`](https://docs.afffast.com/errors/expand-invalid.md). The message lists the fields that you can expand.
- An event has no field that you can expand.
