> 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.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiI3Nzc3ZDAzYS1jOGEwLTQ3ZDYtYmIwNi1iYjM5YzhkYTk1NWIiLCJleHAiOjE3Nzg0OTMxMTAsImlhdCI6MTc3ODQ5MjgxMH0.1-Io8V3LhLDM2lwp_eDooVJAx2S_5FaOYnFKyJWaNTU
>
> 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.

# SDK 变量

> 使用 `x-fern-sdk-variables` 在所有请求中设置通用路径参数

`x-fern-sdk-variables` 扩展允许您定义在 SDK 客户端初始化时设置一次的变量，并在所有端点调用的路径参数中自动使用。这对于在许多端点路径中出现的通用参数（如租户 ID、组织 ID 或环境标识符）非常有用。

<Note>
  SDK 变量在 TypeScript (v2.6.3+)、Python (v4.24.0+) 和 Java (v3.6.3+) 中受支持。仅支持字符串类型。
</Note>

## 配置

使用 `x-fern-sdk-variables` 在文档级别定义变量，然后使用 `x-fern-sdk-variable` 将路径参数标记为变量：

```yaml {1-7,17,23} title="openapi.yml"
x-fern-sdk-variables:
  gardenId:
    type: string
    description: The unique identifier for your garden
  zoneId:
    type: string
    description: The zone within the garden

paths:
  /gardens/{gardenId}/zones/{zoneId}/plants:
    get:
      operationId: list_plants_in_zone
      parameters:
        - name: gardenId
          in: path
          required: true
          x-fern-sdk-variable: gardenId
          schema:
            type: string
        - name: zoneId
          in: path
          required: true
          x-fern-sdk-variable: zoneId
          schema:
            type: string
```

## SDK 使用

变量成为必需的构造函数参数，而不是传递给单个方法调用：

<CodeBlocks>
  ```typescript {2-3}
  const client = new PlantClient({
    gardenId: "garden_123",
    zoneId: "zone_456",
    apiKey: "your-api-key"
  });

  const plants = await client.listPlantsInZone();
  ```

  ```python {2-3}
  client = PlantClient(
      garden_id="garden_123",
      zone_id="zone_456",
      api_key="your-api-key"
  )

  plants = client.list_plants_in_zone()
  ```

  ```java {2-3}
  PlantClient client = PlantClient.builder()
      .gardenId("garden_123")
      .zoneId("zone_456")
      .apiKey("your-api-key")
      .build();

  List<Plant> plants = client.listPlantsInZone();
  ```
</CodeBlocks>