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

# Page through lists

> Lists come in four shapes: all at once, numbered pages with links and meta, numbered pages with a pagination object, and a cursor. An example of each, and a loop that reads every page.

Each list endpoint answers in one of four shapes:

| Shape | Lists | Page size |
| - | - | - |
| [All at once](#all-at-once) | `GET /accounts`, `GET /workspaces`, `GET /labels`, `GET /campaigns`, `GET /folders`, `GET /snippets`, `GET /webhooks`, `GET /posts/{id}/people` | Everything |
| [Numbered pages, with `links` and `meta`](#numbered-pages-with-links-and-meta) | `GET /posts`, `GET /media`, `GET /webhooks/{id}/deliveries` | Posts: `per_page`, 100 by default. Media: `per_page`, 50 by default. Deliveries: 25. |
| [Numbered pages, with `pagination`](#numbered-pages-with-pagination) | `GET /comments`, `GET /reviews` | `per_page`, 20 by default |
| [A cursor](#a-cursor) | `GET /posts/{id}/history` | Up to 20 entries |

## All at once

These lists have no pages, and answer with a bare array. `GET /workspaces`:

```json 200 theme={null}
[
  {
    "id": 7,
    "name": "Halden Coffee"
  }
]
```

`GET /posts/{id}/people` is the one that puts its list in `data`, with nothing else:

```json 200 theme={null}
{
  "data": [
    {
      "id": 31,
      "name": "Halden Coffee team",
      "avatar": null
    }
  ]
}
```

Items on this page are cut down to a few fields. Each endpoint's reference page has them all.

## Numbered pages, with `links` and `meta`

Send `page` (1 by default) and, on posts and media, `per_page`. `per_page` above 100 gives 100. On posts and media, a `per_page` or `page` below 1, or that isn't a number, answers `422`. Deliveries read such a `page` as page 1.

`GET /posts?page=2&per_page=2`, with 5 posts in all:

```json 200 theme={null}
{
  "data": [
    { "id": 1203, "status": "SCHEDULED" },
    { "id": 1202, "status": "DRAFT" }
  ],
  "links": {
    "first": "https://postsyncer.com/api/v1/posts?page=1",
    "last": "https://postsyncer.com/api/v1/posts?page=3",
    "prev": "https://postsyncer.com/api/v1/posts?page=1",
    "next": "https://postsyncer.com/api/v1/posts?page=3"
  },
  "meta": {
    "current_page": 2,
    "from": 3,
    "last_page": 3,
    "links": [
      { "url": "https://postsyncer.com/api/v1/posts?page=1", "label": "&laquo; Previous", "page": 1, "active": false },
      { "url": "https://postsyncer.com/api/v1/posts?page=1", "label": "1", "page": 1, "active": false },
      { "url": "https://postsyncer.com/api/v1/posts?page=2", "label": "2", "page": 2, "active": true },
      { "url": "https://postsyncer.com/api/v1/posts?page=3", "label": "3", "page": 3, "active": false },
      { "url": "https://postsyncer.com/api/v1/posts?page=3", "label": "Next &raquo;", "page": 3, "active": false }
    ],
    "path": "https://postsyncer.com/api/v1/posts",
    "per_page": 2,
    "to": 4,
    "total": 5
  }
}
```

* `meta.last_page` is the last page, and `meta.total` the number of items in all. On the last page, `links.next` is `null`.
* `meta.links` has one entry for each page button, for showing pages to people.
* **`links` keep only `page`.** They drop `per_page` and every filter, such as `workspace_id` or `folder_id` on media. Build the next request yourself: the same query, with `page` one higher.

Posts come newest first, media newest first, deliveries newest first.

## Numbered pages, with `pagination`

Send `page` and `per_page` (1 to 100; above 100 answers `422`).

`GET /comments?post_id=1203&per_page=20`:

```json 200 theme={null}
{
  "data": [
    { "id": 5810, "content": "Is the oat flat white back?" }
  ],
  "pagination": {
    "current_page": 1,
    "last_page": 1,
    "per_page": 20,
    "total": 1,
    "has_more_pages": false
  }
}
```

Read on while `has_more_pages` is `true`. Comments come oldest first. Reviews come unanswered first, then newest, and their answer also has `meta.unanswered`: how many reviews have no reply.

## A cursor

`GET /posts/{id}/history` gives up to 20 entries, newest first, and `next`:

```json 200 theme={null}
{
  "data": [
    { "id": 98412, "kind": "scheduled" }
  ],
  "next": 98377
}
```

To read on, send `next` back as `before`: `GET /posts/1203/history?before=98377`. `next` is `null` on the last page.

## Read every page

This reads every post, 100 at a time. It sends each page through `send()` from [Rate limits](https://docs.postsyncer.com/rate-limits.md#when-you-go-over), so a `429` waits and tries again. The same loop works for media, with any filters added to the query.

```javascript JavaScript theme={null}
// send() is the helper from Rate limits.
async function allPosts() {
  const posts = new Map();

  for (let page = 1; ; page++) {
    const query = new URLSearchParams({ page, per_page: 100 });
    const response = await send(`https://postsyncer.com/api/v1/posts?${query}`);

    if (!response.ok) {
      throw new Error(`Page ${page} answered ${response.status}`);
    }

    const body = await response.json();

    // Keep each post once, by id.
    for (const post of body.data) {
      posts.set(post.id, post);
    }

    if (body.meta.current_page >= body.meta.last_page) {
      return [...posts.values()];
    }
  }
}
```

```python Python theme={null}
# send() is the helper from Rate limits.
def all_posts():
    posts = {}
    page = 1

    while True:
        response = send(
            "GET",
            "https://postsyncer.com/api/v1/posts",
            params={"page": page, "per_page": 100},
        )
        response.raise_for_status()
        body = response.json()

        # Keep each post once, by id.
        for post in body["data"]:
            posts[post["id"]] = post

        if body["meta"]["current_page"] >= body["meta"]["last_page"]:
            return list(posts.values())

        page += 1
```

For comments and reviews, stop when `pagination.has_more_pages` is `false`. For history, stop when `next` is `null`.

Each page is counted when you ask for it, so the list can move while you read. A post made or deleted during the read shifts the others: one can show up twice, which the loop keeps once, and one can be missed. For an exact copy, read every page again. Each page is one request toward your [60 a minute](https://docs.postsyncer.com/rate-limits.md).

## Related

> [card] [Rate limits](https://docs.postsyncer.com/rate-limits.md)
>
> 60 requests a minute, and how to wait out a `429`.

> [card] [Error handling](https://docs.postsyncer.com/essentials/error-handling.md)
>
> Every status and `error` code, and when to try again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.