> This page is for SDK, version Current (default).
> For other versions, use one of these documentation indexes:
> - Current (default): https://buildwithfern.com/learn/sdks/current/llms.txt
> - Legacy: https://buildwithfern.com/learn/sdks/legacy/llms.txt

> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt.

# 配置自动分页

> 通过偏移量、游标、URI 和路径分页方式对 API 响应进行分页。

> **企业功能**
>
> 此功能仅适用于[企业计划](https://buildwithfern.com/pricing)。如需开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。

Fern 的自动分页支持偏移量、游标、URI 和基于路径的分页方案，为 SDK 用户提供简单的迭代器来循环遍历所有结果，而不需要手动管理分页复杂性。

## 生成的 SDK 行为

#### TypeScript

一旦为端点配置了分页，Fern 会生成一个返回 `Page<T>` 的 TypeScript SDK 方法，其中 `T` 是底层数据类型。`Page<T>` 实现了 `AsyncIterable` 接口，允许用户在 `for await` 循环中使用它。

```typescript
const response = await client.users.list();
for await (const user of response) {
  console.log(user);
}
```

#### Python

一旦为端点配置了分页，Fern 会生成一个返回 `Pager[T]` 的 Python SDK 方法，其中 `T` 是底层数据类型。`Pager[T]` 实现了 `Generator` 接口，允许用户在 `for ... in` 循环中使用它。

```python
for user in client.users.list(page=1, page_size=10):
  print(user)
```

或者使用异步客户端：

```python
async for user in await client.users.list(page=1, page_size=10):
  print(user)
```

## 设置自动分页

定义你的分页方案并指定结果的位置：

**`Offset`**

```yaml Offset {4-6}
...
paths:
  /plants:
    x-fern-pagination:
      offset: $request.page_number
      results: $response.results
...
```

**`Cursor`**

```yaml Cursor {5-8}
...
paths:
  /plants:
    get:
      x-fern-pagination:
        cursor: $request.cursor
        next_cursor: $response.next
        results: $response.results
...
```

**`URI`**

```yaml URI {5-7}
...
paths:
  /plants:
    get:
      x-fern-pagination:
        next_uri: $response.next_page_url
        results: $response.results
...
```

**`Path`**

```yaml Path {5-7}
...
paths:
  /plants:
    get:
      x-fern-pagination:
        next_path: $response.next_page_path
        results: $response.results
...
```

有关完整配置详细信息，请参阅 [OpenAPI 中的分页](/learn/zh/api-definitions/openapi/extensions/pagination)。