|
| 1 | +--- |
| 2 | +title: Other Extensions |
| 3 | +description: Documentation for various custom OpenAPI extensions used in Fern |
| 4 | +--- |
| 5 | + |
| 6 | +## X-Fern-SDK-Variables |
| 7 | + |
| 8 | +The `x-fern-sdk-variables` extension allows you to define variables that can be injected into paths across your OpenAPI spec. This enables cleaner path definitions and avoids repetition. |
| 9 | + |
| 10 | +### Example Usage |
| 11 | + |
| 12 | +```yaml |
| 13 | +openapi: 3.0.0 |
| 14 | +x-fern-sdk-variables: |
| 15 | + project_id: |
| 16 | + type: string |
| 17 | + description: "The project ID" |
| 18 | + pattern: "^proj_[a-zA-Z0-9]+$" |
| 19 | +paths: |
| 20 | + /v1/projects/{project_id}/widgets: |
| 21 | + parameters: |
| 22 | + - name: project_id |
| 23 | + in: path |
| 24 | + required: true |
| 25 | + schema: |
| 26 | + type: string |
| 27 | + x-fern-sdk-variable: project_id |
| 28 | +``` |
| 29 | +
|
| 30 | +When using a generated SDK, the variable will be set in the client constructor rather than passed to individual methods: |
| 31 | +
|
| 32 | +```typescript |
| 33 | +const client = new Client({ |
| 34 | + projectId: "proj_123" // Set once in constructor |
| 35 | +}); |
| 36 | + |
| 37 | +// projectId automatically injected into path |
| 38 | +await client.widgets.list(); |
| 39 | +``` |
| 40 | + |
| 41 | +### Configuration Options |
| 42 | + |
| 43 | +<ParamField query="type" type="string" required> |
| 44 | + The type of the variable (string, integer, etc) |
| 45 | +</ParamField> |
| 46 | + |
| 47 | +<ParamField query="description" type="string"> |
| 48 | + Description of what the variable represents |
| 49 | +</ParamField> |
| 50 | + |
| 51 | +<ParamField query="pattern" type="string"> |
| 52 | + Optional regex pattern the variable must match |
| 53 | +</ParamField> |
| 54 | + |
| 55 | +### Parameter Setup |
| 56 | + |
| 57 | +To use a defined variable in a path parameter: |
| 58 | + |
| 59 | +1. Define the variable in `x-fern-sdk-variables` |
| 60 | +2. Add the path parameter normally in your OpenAPI spec |
| 61 | +3. Add `x-fern-sdk-variable: variable_name` to the parameter definition |
| 62 | + |
| 63 | +### Generated SDK Behavior |
| 64 | + |
| 65 | +- Variables are passed via the SDK client constructor |
| 66 | +- The SDK injects variable values automatically into paths |
| 67 | +- Type checking and validation happen at the SDK level |
| 68 | +- Error handling for missing/invalid variables is built in |
| 69 | + |
| 70 | +The SDK will validate the variable values match any defined patterns or constraints before making requests. |
| 71 | + |
| 72 | +### Best Practices |
| 73 | + |
| 74 | +- Use consistent variable names across your API spec |
| 75 | +- Add clear descriptions for what each variable represents |
| 76 | +- Define patterns when variables must match specific formats |
| 77 | +- Consider SDK usability when choosing variable names |
0 commit comments