|
| 1 | +--- |
| 2 | +title: SDK variables |
| 3 | +description: Use `x-fern-sdk-variables` to set common path parameters across all requests |
| 4 | +--- |
| 5 | + |
| 6 | +The `x-fern-sdk-variables` extension allows you to define variables that are set once during SDK client initialization and automatically used in path parameters across all endpoint calls. This is useful for common parameters like tenant IDs, organization IDs, or environment identifiers that appear in many endpoint paths. |
| 7 | + |
| 8 | +<Note> |
| 9 | + SDK variables are supported in TypeScript (v2.6.3+), Python (v4.24.0+), and Java (v3.6.3+). Only string types are supported. |
| 10 | +</Note> |
| 11 | + |
| 12 | +## Configuration |
| 13 | + |
| 14 | +Define variables at the document level using `x-fern-sdk-variables`, then mark path parameters as variables using `x-fern-sdk-variable`: |
| 15 | + |
| 16 | +```yaml {1-7,17,23} title="openapi.yml" |
| 17 | +x-fern-sdk-variables: |
| 18 | + gardenId: |
| 19 | + type: string |
| 20 | + description: The unique identifier for your garden |
| 21 | + zoneId: |
| 22 | + type: string |
| 23 | + description: The zone within the garden |
| 24 | + |
| 25 | +paths: |
| 26 | + /gardens/{gardenId}/zones/{zoneId}/plants: |
| 27 | + get: |
| 28 | + operationId: list_plants_in_zone |
| 29 | + parameters: |
| 30 | + - name: gardenId |
| 31 | + in: path |
| 32 | + required: true |
| 33 | + x-fern-sdk-variable: gardenId |
| 34 | + schema: |
| 35 | + type: string |
| 36 | + - name: zoneId |
| 37 | + in: path |
| 38 | + required: true |
| 39 | + x-fern-sdk-variable: zoneId |
| 40 | + schema: |
| 41 | + type: string |
| 42 | +``` |
| 43 | +
|
| 44 | +## SDK usage |
| 45 | +
|
| 46 | +Variables become required constructor parameters instead of being passed to individual method calls: |
| 47 | +
|
| 48 | +<CodeBlocks> |
| 49 | +```typescript {2-3} |
| 50 | +const client = new PlantClient({ |
| 51 | + gardenId: "garden_123", |
| 52 | + zoneId: "zone_456", |
| 53 | + apiKey: "your-api-key" |
| 54 | +}); |
| 55 | + |
| 56 | +const plants = await client.listPlantsInZone(); |
| 57 | +``` |
| 58 | + |
| 59 | +```python {2-3} |
| 60 | +client = PlantClient( |
| 61 | + garden_id="garden_123", |
| 62 | + zone_id="zone_456", |
| 63 | + api_key="your-api-key" |
| 64 | +) |
| 65 | + |
| 66 | +plants = client.list_plants_in_zone() |
| 67 | +``` |
| 68 | + |
| 69 | +```java {2-3} |
| 70 | +PlantClient client = PlantClient.builder() |
| 71 | + .gardenId("garden_123") |
| 72 | + .zoneId("zone_456") |
| 73 | + .apiKey("your-api-key") |
| 74 | + .build(); |
| 75 | + |
| 76 | +List<Plant> plants = client.listPlantsInZone(); |
| 77 | +``` |
| 78 | +</CodeBlocks> |
0 commit comments