# Pagination

> How hoshime list endpoints page through results with an opaque cursor and a limit, and how to read every page until hasMore is false.

A list endpoint returns the page's items as `data` and says where the list goes on in `meta.pagination`:

```json
{
  "data": [{ "id": "…" }, { "id": "…" }],
  "meta": {
    "requestId": "req_…",
    "platform": "youtube",
    "endpoint": "search",
    "cached": false,
    "fetchedAt": "2026-11-06T15:41:53Z",
    "units": 1,
    "pagination": { "nextCursor": "EpcDEgV…", "hasMore": true, "total": null }
  }
}
```

- Pass `meta.pagination.nextCursor` as the `cursor` parameter to get the next page.
- On the last page, `hasMore` is false and `nextCursor` is empty.
- Cursors are opaque. Never build or parse one: pass back what you were given.
- `limit` sets the page size where the source allows it, from 1 to the endpoint's maximum, with a default. An endpoint whose source has a fixed page size takes no `limit`.
- `total` counts the items of every page when the source says, else it is null.
- Only list endpoints take `cursor` and `limit`. No endpoint pages with `page`, `offset` or `perPage`.

## Read every page

```js
async function* all(path, params = {}) {
  let cursor = "";
  do {
    const q = new URLSearchParams({ ...params, ...(cursor && { cursor }) });
    const res = await fetch(`https://api.hoshime.com${path}?${q}`, {
      headers: { Authorization: `Bearer ${process.env.HOSHIME_TOKEN}` },
    });
    const { data, meta } = await res.json();
    yield* data;
    cursor = meta.pagination.nextCursor;
  } while (cursor);
}

for await (const comment of all("/v1/youtube/comments", { id: "dQw4w9WgXcQ" })) {
  console.log(comment.text);
}
```

Each page is one call and bills its own units.

## Export every page

To collect a whole list, such as every comment of a post, let hoshime read the pages for you. Name the endpoint and its parameters, without `cursor` or `limit`:

```sh
curl -X POST https://api.hoshime.com/v1/exports \
  -H "Authorization: Bearer $HOSHIME_TOKEN" \
  -d '{"export": {"platform": "tiktok", "endpoint": "comments", "params": {"id": "7300000000000000000"}, "maxItems": 5000}}'
```

The export runs in the background. `GET /v1/exports/:jobId` shows its `status` and `exportResult` (items and pages read, whether the list was read to its end, units billed). When it has succeeded, download the items once each, in order:

```sh
curl -o comments.csv "https://api.hoshime.com/v1/exports/$JOB_ID/download?format=csv" \
  -H "Authorization: Bearer $HOSHIME_TOKEN"
```

`format=json` (the default) gives one JSON array. CSV has a column per field, nested fields as `author.username`, and lists as JSON. Each page bills what calling the endpoint bills, a cached page nothing; the download is free. A failed export keeps the items it read, and they stay billed and downloadable.