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

# Pagination

> Paginate list endpoints in the Marble API with the limit and page query parameters, response metadata, async iteration, and error handling examples.

The Marble API uses offset-based pagination for endpoints that return multiple items. Pagination metadata is included in every response to help you navigate through large datasets efficiently.

## How Pagination Works

When you request a list of resources (like posts), the API automatically includes pagination information in the response. This allows you to:

* Control how many items are returned per request
* Navigate to specific pages
* Understand the total size of the dataset
* Build pagination UI components

## Query Parameters

**limit** `integer` (default: 10)

The maximum number of items to return per page. **Range:** 1-200 items per
page

**page** `integer` (default: 1)

The page number to retrieve. Pages start at 1. **Minimum:** 1

## Pagination Response

Every paginated response includes a `pagination` object with the following fields:

**pagination** `object`

Metadata about the current page and navigation options.

**Pagination properties**

**limit** `integer`

The number of items requested per page (matches your query parameter).

**currentPage** `integer`

The current page number being returned.

**nextPage** `integer | null`

The next page number, or `null` if you're on the last page.

**previousPage** `integer | null`

The previous page number, or `null` if you're on the first page.

**totalPages** `integer`

The total number of pages available.

**totalItems** `integer`

The total number of items across all pages.

## Request Examples

> [tab] Default Pagination
>
> Get the first page with default limit (10 items):
>
> ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
> curl -H "Authorization: YOUR_API_KEY" \
>   "https://api.marblecms.com/v1/posts"
> ```

> [tab] Custom Limit
>
> Get 5 items per page:
>
> ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
> curl -H "Authorization: YOUR_API_KEY" \
>   "https://api.marblecms.com/v1/posts?limit=5"
> ```

> [tab] Specific Page
>
> Get page 2 with 5 items per page:
>
> ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
> curl -H "Authorization: YOUR_API_KEY" \
>   "https://api.marblecms.com/v1/posts?page=2&limit=5"
> ```

## SDK Examples

> [tab] Basic Pagination
>
> Fetch posts with pagination options:
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> import { Marble } from "@usemarble/sdk";
>
> const marble = new Marble({
>   apiKey: process.env["MARBLE_API_KEY"] ?? "",
> });
>
> const result = await marble.posts.list({
>   limit: 10,
>   page: 1,
> });
> ```

> [tab] Iterate All Pages
>
> The SDK returns an async iterable for paginated endpoints:
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const result = await marble.posts.list({ limit: 10 });
>
> for await (const page of result) {
>   console.log(page.posts);
>   console.log(page.pagination.currentPage);
> }
> ```

> [tab] Pagination Controls
>
> Use the pagination response to build navigation UI:
>
> ```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
> const result = await marble.posts.list({ limit: 10, page: 1 });
>
> for await (const page of result) {
>   const { pagination } = page;
>
>   // Use these for your pagination UI
>   const canGoBack = pagination.previousPage !== null;
>   const canGoForward = pagination.nextPage !== null;
>   const pageInfo = `Page ${pagination.currentPage} of ${pagination.totalPages}`;
>   const itemCount = `${pagination.totalItems} total items`;
> }
> ```

> [!TIP]
>
> The SDK's async iterable automatically handles fetching subsequent pages. If
> you only need a single page (e.g., for pagination controls), iterate once and
> break.

```json Success Response theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "posts": [
    {
      "id": "post_abc123def456",
      "slug": "getting-started-with-cms-integration",
      "title": "Getting Started with CMS Integration",
      "content": "<p>Learn how to integrate your content management system...</p>",
      "coverImage": "https://cdn.marblecms.com/images/cms-guide.webp",
      "description": "A comprehensive guide to integrating your CMS...",
      "publishedAt": "2024-01-15T10:30:00.000Z",
      "updatedAt": "2024-01-20T14:22:33.456Z",
      "authors": [
        {
          "id": "author_xyz789",
          "name": "John Smith",
          "image": "https://cdn.marblecms.com/avatars/john.jpg"
        }
      ],
      "category": {
        "id": "cat_tutorials123",
        "name": "Tutorials",
        "slug": "tutorials"
      },
      "tags": [
        {
          "id": "tag_integration456",
          "name": "Integration",
          "slug": "integration"
        }
      ]
    }
  ],
  "pagination": {
    "limit": 5,
    "currentPage": 1,
    "nextPage": null,
    "previousPage": null,
    "totalPages": 1,
    "totalItems": 2
  }
}
```

```json Empty Result Set theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "posts": [],
  "pagination": {
    "limit": 10,
    "currentPage": 1,
    "nextPage": null,
    "previousPage": null,
    "totalPages": 0,
    "totalItems": 0
  }
}
```

```json Error - Invalid Page theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "Invalid page number",
  "details": {
    "message": "Page 2 does not exist.",
    "totalPages": 1,
    "requestedPage": 2
  }
}
```

## Paginated Endpoints

The following endpoints support pagination:

> [card] [Posts](https://docs.marblecms.com/api-reference/posts/list-posts.md)
>
> Get all published posts with pagination support.

> [card] [Categories](https://docs.marblecms.com/api-reference/categories/list-categories.md)
>
> Browse all content categories with pagination.

> [card] [Authors](https://docs.marblecms.com/api-reference/authors/list-authors.md)
>
> List all authors with their associated content.

> [card] [Tags](https://docs.marblecms.com/api-reference/tags/list-tags.md)
>
> Retrieve all content tags with pagination.

> [card] [Media](https://docs.marblecms.com/api-reference/media/list-media-assets.md)
>
> Browse workspace media assets with pagination.

## Best Practices

> [!TIP]
>
> **Optimize Performance**: Use smaller page sizes (5-20 items) for better
> performance, especially on mobile devices.

> [!TIP]
>
> **Handle Empty Results**: Always check if the data array is empty and handle
> the empty state in your UI.

> [!NOTE]
>
> **Navigation Logic**: Use `nextPage` and `previousPage` values to build
> navigation controls. These fields will be `null` when navigation in that
> direction isn't possible.

> [!TIP]
>
> **Building Pagination UI**: The `pagination` object in every response gives
> you everything needed for pagination controls: current page, total pages, and
> next/previous page numbers.

## Error Handling

> [accordion] Invalid page number
>
> When you request a page that doesn't exist, the API returns a structured error response:
>
> ```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
> {
>   "error": "Invalid page number",
>   "details": {
>     "message": "Page 2 does not exist.",
>     "totalPages": 1,
>     "requestedPage": 2
>   }
> }
> ```
>
> Always check for the `error` field in your response before processing pagination data.

> [accordion] Invalid limit value
>
> * Values below 1 will default to 1
> * Values above 200 will be capped at 200
> * Non-numeric values will default to 10

> [accordion] Empty datasets
>
> When there are no items, `totalPages` will be 0 and `totalItems` will be 0. The data array will be empty.


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