> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiIxMDFiNjM0NS1jNzc5LTRhZjYtYjVkOS03ZGFjZDQxYWQ1ZTMiLCJleHAiOjE3Nzg0OTMwMDUsImlhdCI6MTc3ODQ5MjcwNX0.Ug0iT75aaA-Ff7ykk8mw7S5P3qD_PPEYQl235ebzPO4
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 幂等性

> 使用 x-fern-idempotency-headers 和 x-fern-idempotent 扩展配置幂等性，实现安全的请求重试

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

Fern 通过两个协同工作的扩展支持幂等性配置：`x-fern-idempotency-headers` 配置您的 API 使用的幂等性头部，`x-fern-idempotent` 将单个端点标记为幂等的。配置后，Fern 生成的 SDK 允许用户指定幂等性头部以进行安全的请求重试。

## 配置幂等性头部

在 OpenAPI 规范的根级别添加 `x-fern-idempotency-headers` 扩展，以定义您的 API 用于幂等性的头部：

```yaml title="openapi.yml"
x-fern-idempotency-headers:
  - header: Idempotency-Key
    name: idempotency_key
  - header: Idempotency-Expiration
    name: idempotency_expiration
```

每个幂等性头部支持以下属性：

| 属性       | 描述                                     |
| -------- | -------------------------------------- |
| `header` | HTTP 头部名称（例如：`Idempotency-Key`）        |
| `name`   | 生成的 SDK 中使用的参数名称（例如：`idempotency_key`） |

## 将端点标记为幂等的

在单个端点上添加 `x-fern-idempotent` 扩展并将其设置为 `true`，以将它们标记为幂等的：

```yaml title="openapi.yml" {1-3,8}
x-fern-idempotency-headers:
  - header: Idempotency-Key
    name: idempotency_key

paths:
  /plants:
    post:
      x-fern-idempotent: true
      operationId: create_plant
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                species:
                  type: string
      responses:
        '201':
          description: Plant created successfully
```