> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/technical-reference/apis-and-mcp/pagination-filtering-and-sorting.md).

# Pagination, Filtering, and Sorting

Public list endpoints expose their own pagination and filtering fields. Use the endpoint’s response to determine whether more records remain.

![Read every page without inventing cursors](https://1957915746-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSQ1Sgy933krTyK0S4JpP%2Fuploads%2FXvaAOJs43g6svdlqDXKo%2Fpagination-filtering-and-sorting.png?alt=media)

Python pattern with an authenticated requests session and your own process function. Keep page settings unchanged and pass next\_cursor back exactly as returned.

<details>

<summary>Copy the example</summary>

```python
params = {"page_size": 50, "direction": "next"}
while True:
    response = session.get(
        f"{base_url}/v2/knowledge-bases", params=params
    )
    response.raise_for_status()
    page = response.json()
    process(page["data"])
    cursor = page.get("next_cursor")
    if not cursor:
        break
    params["cursor"] = cursor
```

</details>

## Before you begin

The operation’s public request and response schema.

## Key concepts

| Term       | Definition                                                                                                                                                |
| ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Pagination | Retrieval of a collection through multiple pages. Use the continuation method exposed by the selected operation.                                          |
| Cursor     | A continuation value returned for the next page on cursor-based endpoints. Treat it as opaque; do not manufacture it from a display name or record count. |
| Offset     | A numeric starting position used by some list operations. Keep filtering and ordering consistent while moving through pages.                              |

## Configuration options

| Option    | Purpose                                                           | Behavior                                                               |
| --------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------- |
| Page size | The maximum number of records requested or processed in one page. | It is not a guarantee that all records in the collection are included. |
| Filter    | A condition that narrows the records eligible for an operation.   | A filter can exclude relevant content before ranking occurs.           |

## Request a filtered page of results

1. Set supported filters and page-size fields.
2. Read the returned collection and continuation information.
3. Request further pages using the documented method and stop at completion.

The client retrieves the intended records without relying on a single page.

## Behavior and limitations

Not all endpoints support the same filters, sorting, or cursor format. A limit is usually a page limit rather than a complete result count. Keep filters consistent between pages. Handle records changing during collection and avoid assuming an unspecified sort order is stable.

## Related resources

[API Versions and Compatibility](https://docs.stackai.com/technical-reference/apis-and-mcp/api-versions-and-compatibility.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/technical-reference/apis-and-mcp/pagination-filtering-and-sorting.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
