Skip to content

Commit 60f92f8

Browse files
docs: add documentation for x-fern-sdk-variables extension
Co-Authored-By: [email protected] <[email protected]>
1 parent 8c7052f commit 60f92f8

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

fern/products/api-def/openapi-pages/extensions/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The table below shows all available extensions and links to detailed documentati
2626
| [`x-fern-ignore`](./ignoring-elements) | Skip reading specific endpoints or schemas |
2727
| [`x-fern-sdk-method-name`](./method-names) | Customize SDK method names |
2828
| [`x-fern-sdk-group-name`](./method-names) | Organize methods into SDK groups |
29+
| [`x-fern-sdk-variables`](./sdk-variables) | Set common path parameters across all requests |
2930
| [`x-fern-parameter-name`](./parameter-names) | Customize parameter variable names |
3031
| [`x-fern-property-name`](./property-names) | Customize object property variable names |
3132
| [`x-fern-type-name`](./schema-names) | Override auto-generated names for inline schemas |
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 particularly useful for common path parameters like tenant IDs, organization IDs, or environment identifiers that appear in many endpoint paths.
7+
8+
## How it works
9+
10+
SDK variables are defined at the document level using `x-fern-sdk-variables` and then referenced in path parameters using `x-fern-sdk-variable`. When a path parameter is marked as a variable, it becomes a required parameter in the SDK client constructor instead of being passed to individual endpoint methods.
11+
12+
## Configuration
13+
14+
### Define variables
15+
16+
At the document level, define your variables with their types:
17+
18+
```yaml title="openapi.yml"
19+
openapi: 3.0.0
20+
info:
21+
title: Plant API
22+
version: 1.0.0
23+
24+
x-fern-sdk-variables:
25+
gardenId:
26+
type: string
27+
description: The unique identifier for your garden
28+
```
29+
30+
### Reference variables in path parameters
31+
32+
Mark path parameters as variables using `x-fern-sdk-variable`:
33+
34+
```yaml {9}
35+
paths:
36+
/gardens/{gardenId}/plants:
37+
get:
38+
operationId: list_plants
39+
parameters:
40+
- name: gardenId
41+
in: path
42+
required: true
43+
x-fern-sdk-variable: gardenId
44+
schema:
45+
type: string
46+
responses:
47+
'200':
48+
description: List of plants
49+
```
50+
51+
## Generated SDK behavior
52+
53+
### TypeScript
54+
55+
With SDK variables configured, the TypeScript SDK requires the variable in the client constructor:
56+
57+
```typescript
58+
import { PlantClient } from "@acme/plant-api";
59+
60+
const client = new PlantClient({
61+
gardenId: "garden_123",
62+
apiKey: "your-api-key"
63+
});
64+
65+
// gardenId is automatically included in the request
66+
const plants = await client.listPlants();
67+
```
68+
69+
Without SDK variables, the parameter would be required on each method call:
70+
71+
```typescript
72+
// Without x-fern-sdk-variables
73+
const plants = await client.listPlants("garden_123");
74+
```
75+
76+
### Python
77+
78+
In Python SDKs, variables are required constructor parameters:
79+
80+
```python
81+
from acme import PlantClient
82+
83+
client = PlantClient(
84+
garden_id="garden_123",
85+
api_key="your-api-key"
86+
)
87+
88+
# garden_id is automatically included in the request
89+
plants = client.list_plants()
90+
```
91+
92+
### Java
93+
94+
In Java SDKs, variables are set using builder methods:
95+
96+
```java
97+
PlantClient client = PlantClient.builder()
98+
.gardenId("garden_123")
99+
.apiKey("your-api-key")
100+
.build();
101+
102+
// gardenId is automatically included in the request
103+
List<Plant> plants = client.listPlants();
104+
```
105+
106+
## Multiple variables
107+
108+
You can define multiple SDK variables for complex path hierarchies:
109+
110+
```yaml title="openapi.yml"
111+
x-fern-sdk-variables:
112+
gardenId:
113+
type: string
114+
description: The unique identifier for your garden
115+
zoneId:
116+
type: string
117+
description: The zone within the garden
118+
119+
paths:
120+
/gardens/{gardenId}/zones/{zoneId}/plants:
121+
get:
122+
operationId: list_plants_in_zone
123+
parameters:
124+
- name: gardenId
125+
in: path
126+
required: true
127+
x-fern-sdk-variable: gardenId
128+
schema:
129+
type: string
130+
- name: zoneId
131+
in: path
132+
required: true
133+
x-fern-sdk-variable: zoneId
134+
schema:
135+
type: string
136+
```
137+
138+
This generates an SDK where both variables are set during initialization:
139+
140+
```typescript
141+
const client = new PlantClient({
142+
gardenId: "garden_123",
143+
zoneId: "zone_456",
144+
apiKey: "your-api-key"
145+
});
146+
147+
const plants = await client.listPlantsInZone();
148+
```
149+
150+
## Use cases
151+
152+
SDK variables are ideal for:
153+
154+
- **Multi-tenant APIs**: Set the tenant or organization ID once during client initialization
155+
- **Environment-specific paths**: Configure environment identifiers (production, staging) at the client level
156+
- **Hierarchical resources**: Set parent resource IDs that are used across many child resource endpoints
157+
- **Regional APIs**: Configure region identifiers that appear in all endpoint paths
158+
159+
## Supported languages
160+
161+
SDK variables are supported in the following Fern SDK generators:
162+
163+
- TypeScript (v3.13.0+)
164+
- Python (v4.32.1+)
165+
- Java (v3.10.1+)
166+
167+
<Note>
168+
SDK variables only support string types. If you need more complex types, consider using global headers with `x-fern-global-headers` instead.
169+
</Note>

0 commit comments

Comments
 (0)