> ## Documentation Index
> Fetch the complete documentation index at: /llms.txt
> Use this file to discover all available pages before exploring further.

# Pagination

> How list endpoints page results with a cursor, and how to loop through every page.

List endpoints that can grow large return one page at a time.

```json
{
  "items": [ ... ],
  "nextCursor": "WyIyMDI2LTEyLTA1VDExOjIyOjAyLjI1NzQ0MFoi..."
}
```

* `items` is the page, newest first.
* `nextCursor` is an opaque string. Pass it back unchanged as the `cursor` query parameter to get the next page.
* On the last page `nextCursor` is `null`.

Do not build or parse cursors. Their format can change. A cursor you did not get from the API answers `400 invalid_cursor`.

## Which endpoints page

| Endpoint | Parameters |
| - | - |
| `GET /workspaces/{workspaceId}/posts` | `cursor`, `limit` (1 to 100, default 20), `status`, `tab` |
| `GET /workspaces/{workspaceId}/infographics` | `cursor`, `limit` (1 to 50, default 20) |

Other lists return everything in one answer as `{ "items": [...] }` with no `nextCursor`: carousel templates, content styles, and the post timeline (published posts of the last 90 days plus scheduled posts, up to 100).

## Filter posts

* `status` is one of `draft`, `pending_approval`, `approved`, `scheduled`, `publishing`, `published`, `failed`, `cancelled`.
* `tab` is one of `drafts`, `in_review`, `approved`, `scheduled`, `published`, `archive`. It groups statuses the way the app's Posts page does.

An unknown value answers `400 validation_error` with the allowed values in `details.issues`.

## Example

```bash
curl "https://api.postchampai.com/api/v1/workspaces/$WS/posts?status=draft&limit=2" \
  -H "x-api-key: $POSTCHAMP_API_KEY"
```

```json
{
  "items": [
    { "id": "6ffd723c-ea8d-4908-a4ff-6afc0874170d", "status": "draft", "content": "This is placeholder text from the test model." },
    { "id": "52293da2-8fcf-4387-a695-0e33c1180c5d", "status": "draft", "content": "This is placeholder text from the test model." }
  ],
  "nextCursor": "WyIyMDI2LTEyLTA1VDExOjIyOjAyLjI1NzQ0MFoiLCI1MjI5M2RhMi04ZmNmLTQzODctYTY5NS0wZTMzYzExODBjNWQiXQ"
}
```

Each item is the full post, shortened here. Next page:

```bash
curl "https://api.postchampai.com/api/v1/workspaces/$WS/posts?status=draft&limit=2&cursor=WyIyMDI2..." \
  -H "x-api-key: $POSTCHAMP_API_KEY"
```

## Loop through every page

```javascript JavaScript
async function* allPosts(workspaceId, params = {}) {
  let cursor = null;
  do {
    const qs = new URLSearchParams({ ...params, limit: "100" });
    if (cursor) qs.set("cursor", cursor);
    const res = await fetch(
      `https://api.postchampai.com/api/v1/workspaces/${workspaceId}/posts?${qs}`,
      { headers: { "x-api-key": process.env.POSTCHAMP_API_KEY } },
    );
    if (!res.ok) throw new Error(`${res.status} ${(await res.json()).error.code}`);
    const page = await res.json();
    yield* page.items;
    cursor = page.nextCursor;
  } while (cursor);
}

for await (const post of allPosts(workspaceId, { status: "scheduled" })) {
  console.log(post.id, post.scheduledAt);
}
```

```python Python
import os, requests

def all_posts(workspace_id, **params):
    cursor = None
    while True:
        query = {**params, "limit": 100}
        if cursor:
            query["cursor"] = cursor
        res = requests.get(
            f"https://api.postchampai.com/api/v1/workspaces/{workspace_id}/posts",
            headers={"x-api-key": os.environ["POSTCHAMP_API_KEY"]},
            params=query,
        )
        res.raise_for_status()
        page = res.json()
        yield from page["items"]
        cursor = page["nextCursor"]
        if not cursor:
            return

for post in all_posts(workspace_id, status="scheduled"):
    print(post["id"], post["scheduledAt"])
```

Each page is one request and counts against your [rate limits](/api/rate-limits.md). Use the largest `limit` you need to keep the number of requests low.

> [card] [Errors](/api/errors.md)
>
> Including invalid\_cursor.

> [card] [Rate limits](/api/rate-limits.md)
>
> Per-minute windows.
