> 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.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiIwNGM1MTVlYy1jZTQ0LTQ3NTgtOGE1Mi0xZjc0Yjg3M2EyNWQiLCJleHAiOjE3NzgzODM1MjQsImlhdCI6MTc3ODM4MzIyNH0.qYT1y449k6Unc4kmAMKEkHa1jX4GNHIxYJDQTO7v-BE
>
> 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.

# 配置全局请求头

> 了解如何在 Fern SDK 中配置全局请求头。将 API 请求头设置为构造函数参数，自动包含在所有请求中。

如果您的 API 在每个请求（或大多数请求）中都需要特定的请求头，您可以将它们配置为**全局请求头**。Fern 将生成一个 SDK，在客户端构造函数中接受这些请求头一次，并自动将它们包含在所有 API 调用中。

## 生成的 SDK 行为

一旦配置了全局请求头，Fern 会生成一个 SDK，将请求头值作为构造函数参数接受：

```python
import os

class Client:

  def __init__(self, *, apiKey: str):
```

SDK 用户只需提供一次值，生成的 SDK 会自动在所有请求中包含该请求头。

## 设置全局请求头

Fern 会自动识别在每个请求或大多数请求中使用的请求头，并将它们标记为全局。您可以在 API 定义中手动配置额外的全局请求头：

<Tabs>
  <Tab title="OpenAPI">
    ```yaml title="openapi.yml"
    x-fern-global-headers:
      - header: custom_api_key
        name: api_key
      - header: userpool_id
        optional: true
    ```
  </Tab>

  <Tab title="Fern Definition">
    ```yaml title="api.yml"
    name: api
    headers:
      X-App-Id: string
    ```
  </Tab>
</Tabs>

您还可以在全局请求头上设置[默认值](/learn/api-definitions/openapi/extensions/default-values)，这样当调用者没有提供值时，SDK 会自动发送它。

有关完整的配置详细信息，请参阅您的 API 定义格式的文档：

<CardGroup cols={2}>
  <Card title="OpenAPI" href="/learn/api-definitions/openapi/extensions/global-headers">
    使用 `x-fern-global-headers` 扩展注释请求头。
  </Card>

  <Card title="Fern Definition" href="/learn/api-definitions/ferndef/api-yml/global-headers">
    在 `api.yml` 中配置全局请求头、路径参数和幂等性请求头
  </Card>
</CardGroup>