OpenAPI Specification

OpenAPI extensions for SDKs

Fern supports different OpenAPI extensions so that you can generate higher-quality SDKs.

SDK method names

By default, Fern uses the tag and operationId fields to generate the SDK method. So an endpoint with a tag users and operationId users_create will generate an SDK that reads client.users.create().

To explicitly set the SDK method you can leverage the extensions:

  • x-fern-sdk-group-name which groups SDK methods together
  • x-fern-sdk-method-name which is used as the method name in the SDK

The OpenAPI below will generate client.users.create():

openapi.yaml
1paths:
2 /users:
3 post:
4 x-fern-sdk-group-name: users
5 x-fern-sdk-method-name: create

If you omit the x-fern-sdk-group-name extension, then the generated SDK method will live at the root. For example, the following OpenAPI will generate client.create_user():

openapi.yaml
1paths:
2 /users:
3 post:
4 x-fern-sdk-method-name: create_user

If you add more than one x-fern-sdk-group-name extension, then the generated SDK will nested group names. The order of the group names is preserved in the generated SDK method.

For example, the following OpenAPI will generate client.admin.users.create():

openapi.yaml
1paths:
2 /users:
3 post:
4 x-fern-sdk-group-name:
5 - admin
6 - users
7 x-fern-sdk-method-name: create

API version

You can define your API version scheme, such as a X-API-Version header. The supported versions and default value are specified like so:

openapi.yaml
1x-fern-version:
2 version:
3 header: X-API-Version
4 default: "2.0.0"
5 values:
6 - "1.0.0"
7 - "2.0.0"
8 - "latest"
9paths:
10 ...

Authentication overrides

For authentication within your SDK, you may want to customize the:

  • Names of the parameters a user will populate to provide their credentials
  • Any environment variable the SDK can scan for to automatically provide the user’s credentials automatically

By default, Fern uses names for authentication parameters like api_key, username and password, etc. Note that this configuration is optional, and even when provided name or env may be provided alone, both are not required.

For bearer authentication:

openapi.yaml
1components:
2 securitySchemes:
3 BearerAuth:
4 type: http
5 scheme: bearer
6 x-fern-bearer:
7 name: apiKey
8 env: FERN_API_KEY

In a Python SDK for example, the above configuration would produce a client that looks like:

1import os
2
3class Client:
4
5 def __init__(self, *, apiKey: str = os.getenv("FERN_API_KEY"))

Similarly, for basic authentication the configuration is:

openapi.yaml
1components:
2 securitySchemes:
3 BasicAuth:
4 type: http
5 scheme: basic
6 x-fern-basic:
7 username:
8 name: clientId
9 env: MY_CLIENT_ID
10 password:
11 name: clientSecret
12 env: MY_CLIENT_SECRET

And for a custom auth header configuration:

openapi.yaml
1components:
2 securitySchemes:
3 ApiKeyAuth:
4 type: apiKey
5 in: header
6 name: X-API-Key
7 x-fern-header:
8 name: header
9 env: MY_HEADER_ENVVAR
10 prefix: "Token " # Optional

This adds a header of the form X-API-Key: Token <value> to the request.

Global headers

At times, your API will leverage certain headers for every endpoint, or the majority of them, we call these “global headers”. For convenience, generated Fern SDKs expose “global headers” to easily be updated on API calls. Take for example an API key, if we declare the API key as a global header, a user will be able to plug theirs in easily:

1import os
2
3class Client:
4
5 def __init__(self, *, apiKey: str):

To configure global headers, Fern will automatically pull out headers that are used in every request, or the majority of requests, and mark them as global. In order to label additional headers as global, or to alias the names of global headers, you can leverage the x-fern-global-headers extension:

openapi.yml
1x-fern-global-headers:
2 - header: custom_api_key
3 name: api_key
4 - header: userpool_id
5 optional: true

yields the following client:

1import os
2
3class Client:
4
5 def __init__(self, *, apiKey: str, userpoolId: typing.Optional[str])

Enum descriptions and names

OpenAPI doesn’t natively support adding descriptions to enum values. To do this in Fern you can use the x-fern-enum extension.

In the example below, we’ve added some descriptions to enum values. These descriptions will propagate into the generated SDK and docs website.

openapi.yml
1components:
2 schemas:
3 CardSuit:
4 enum:
5 - clubs
6 - diamonds
7 - hearts
8 - spades
9 x-fern-enum:
10 clubs:
11 description: Some docs about clubs
12 spades:
13 description: Some docs about spades

x-fern-enum also supports a name field that allows you to customize the name of the enum in code. This is particularly useful when you have enums that rely on symbolic characters that would otherwise cause generated code not to compile.

For example, the following OpenAPI

openapi.yml
1components:
2 schemas:
3 Operand:
4 enum:
5 - >
6 - <
7 x-fern-enum:
8 >:
9 name: GreaterThan
10 description: Checks if value is greater than
11 <:
12 name: LessThan
13 description: Checks if value is less than

would generate

operand.ts
1export enum Operand {
2 GreaterThan = ">",
3 LessThan = "<",
4}

Schema names

OpenAPI allows you to define inlined schemas that do not have names.

Inline type in openapi.yml
1components:
2 schemas:
3 Movie:
4 type: object
5 properties:
6 name:
7 type: string
8 cast:
9 type: array
10 items:
11 type: object
12 properties:
13 firstName:
14 type: string
15 lastName:
16 type: string
17 age:
18 type: integer

Fern automatically generates names for all the inlined schemas. For example, in this example, Fern would generate the name CastItem for the inlined array item schema.

Auto-generated name
1export interface Movie {
2 name?: string;
3 cast?: CastItem[]
4}
5
6export interface CastItem {
7 firstName?: string;
8 lastName?: string;
9 age?: integer;
10}

If you want to override the generated name, you can use the extension x-fern-type-name.

openapi.yml
1components:
2 schemas:
3 Movie:
4 type: object
5 properties:
6 name:
7 type: string
8 cast:
9 type: array
10 items:
11 type: object
12 x-fern-type-name: Person
13 properties:
14 firstName:
15 type: string
16 lastName:
17 type: string
18 age:
19 type: integer

This would replace CastItem with Person and the generated code would read more idiomatically:

Overridden name
1export interface Movie {
2 name?: string;
3 cast?: Person[]
4}
5
6export interface Person {
7 firstName?: string;
8 lastName?: string;
9 age?: integer;
10}

Parameter names

The x-fern-parameter-name extension allows you to customize the variable name of OpenAPI parameters.

For example, if you have a header X-API-Version, you may want the parameter in code to be called version.

Specify the parameter name in openapi.yml
1paths:
2 "/user":
3 get:
4 operationId: get_user
5 parameters:
6 - in: header
7 name: X-API-Version
8 x-fern-parameter-name: version
9 schema:
10 type: string
11 required: true
12 ...

Property Names

The x-fern-property-name extension allows you to customize the variable name for object properties.

For example, if you had a property called _metadata in your schema but you wanted the variable to be called data in your SDK you would do the following:

1components:
2 schemas:
3 MyUser:
4 _metadata:
5 type: object
6 x-fern-property-name: data

Server names

The x-fern-server-name extension is used to name your servers.

openapi.yml
1servers:
2- url: https://api.example.com
3 x-fern-server-name: Production
4- url: https://sandbox.example.com
5 x-fern-server-name: Sandbox

In a generated TypeScript SDK, you’d see:

environment.ts
1export const ExampleEnvironment = {
2 Production: "https://api.example.com",
3} as const;
4
5export type ExampleEnvironment = typeof ExampleEnvironment.Production;

Base path

The x-fern-base-path extension is used to configure the base path prepended to every endpoint.

In the example below, we have configured the /v1 base path so the full endpoint path is https://api.example.com/v1/users.

Set the base path in openapi.yml
1x-fern-base-path: /v1
2servers:
3- url: https://api.example.com
4paths:
5 /users:
6 ...

Audiences

Audiences are a useful tool for segmenting your API for different consumers. You can configure your Fern Docs to publish documentation specific to an Audience. You can use audiences in your Fern Definition, too.

Common examples of audiences include:

  • Internal consumers (e.g., frontend developers who use the API)
  • Beta testers
  • Customers

By default, if no audience is specified, it will be accessible to all consumers.

Audiences for Endpoints

To mark an endpoint with a particular audience, add the x-fern-audiences extension to the relevant endpoint.

In the example below, the POST /users/sendEmail endpoint is only available to internal consumers:

openapi.yml
1paths:
2 /users/sendEmail:
3 post:
4 x-fern-audiences:
5 - internal
6 operationId: send_email
7 ...

Audiences for Components

Components can be marked for different audiences, as well.

In this example, the Email type is available to both internal and beta customers, while the to property is available to beta customers only:

openapi.yml
1components:
2 schemas:
3 Email:
4 title: Email
5 type: object
6 properties:
7 subject:
8 type: string
9 body:
10 type: string
11 to:
12 type: string
13 x-fern-audiences: <-- property audience
14 - beta
15 x-fern-audiences: <-- type audience
16 - internal
17 - beta

Audiences for SDKs

In generators.yml, you can apply audience filters so that only certain endpoints are passed to the generators.

The following example configures the SDKs to filter for the customers audience:

generators.yml
1groups:
2 sdks:
3 audiences:
4 - customers
5 generators:
6 - name: fernapi/fern-tyepscript-node-sdk
7 version: 0.8.8
8 ...

Audiences for Docs

If generating Fern Docs, update your docs.yml configuration to include your audiences.

The following example shows how to configure your docs.yml to publish documentation for the customers audience:

docs.yml
1navigation:
2 - api: API Reference
3 audiences:
4 - customers

Streaming

The x-fern-streaming extension is used to specify that an endpoint’s response is streamed. Include the x-fern-streaming extension within your endpoint like so:

x-fern-streaming in openapi.yml
1paths:
2 /logs:
3 post:
4 x-fern-streaming: true
5 requestBody:
6 content:
7 application/json:
8 schema:
9 type: object
10 properties:
11 topic:
12 description: ""
13 type: string
14 responses:
15 "200":
16 content:
17 application/json:
18 schema:
19 $ref: "#/components/schemas/Log"
20components:
21 schemas:
22 Log:
23 type: object
24 properties:
25 topic:
26 type: string
27 message:
28 type: string

If you want to generate both a non-streaming and streaming SDK method variant for the same endpoint, you can use the extended x-fern-streaming syntax:

Extended x-fern-streaming syntax in openapi.yml
1paths:
2 /logs:
3 post:
4 x-fern-streaming:
5 stream-condition: $request.stream
6 response:
7 $ref: "#/components/schemas/Logs"
8 response-stream:
9 $ref: "#/components/schemas/Log"
10 requestBody:
11 content:
12 application/json:
13 schema:
14 type: object
15 properties:
16 topic:
17 description: ""
18 type: string
19 responses:
20 "200":
21 content:
22 application/json:
23 schema:
24 $ref: "#/components/schemas/Log"
25components:
26 schemas:
27 Log:
28 type: object
29 properties:
30 topic:
31 type: string
32 message:
33 type: string
34 Logs:
35 type: array
36 items:
37 - $ref: "#/components/schemas/Log"

The same endpoint path (/logs in the example above) is used for both of the generated SDK methods, and the stream-condition setting includes a property in the request body that is used to distinguish whether the response should be streamed.

In the example above, a property named stream is sent as true for the streaming variant, and sent as false for the non-streaming variant. Additionally, a stream of the Log type is returned in the streaming variant, whereas the Logs array is returned in the non-streaming variant.

Ignoring schemas or endpoints

If you want Fern to skip reading any endpoints or schemas, use the x-fern-ignore extension.

To skip an endpoint, add x-fern-ignore: true at the operation level.

x-fern-ignore at operation level in openapi.yml
1paths:
2 /users:
3 get:
4 x-fern-ignore: true
5 ...

To skip a schema, add x-fern-ignore: true at the schema level.

x-fern-ignore at schema level in openapi.yml
1components:
2 schemas:
3 SchemaToSkip:
4 x-fern-ignore: true
5 ...

Overlaying extensions

Because of the number of tools that use OpenAPI, it may be more convenient to “overlay” your fern specific OpenAPI extensions onto your original definition.
In order to do this you can specify your overrides file in generators.yml.

Below is an example of how someone can overlay the extensions x-fern-sdk-method-name and x-fern-sdk-group-name without polluting their original OpenAPI. The combined result is shown in the third tab.

1api:
2 path: ./openapi/openapi.yaml
3 overrides: ./openapi/overrides.yaml
4default-group: sdk
5groups:
6 sdk:
7 generators:
8 - name: fernapi/fern-python-sdk
9 version: 2.2.0

Embedding extensions

If instead of overlaying your extensions within an overrides file, as mentioned above. Certain frameworks that generate OpenAPI specs make it easy to embed extensions directly from code.

FastAPI

Please view our page on FastAPI for more information on how to extend your OpenAPI spec within FastAPI.

Request + response examples

While OpenAPI has several fields for examples, there is no easy way to associate a request with a response. This is especially useful when you want to show more than one example in your documentation.

x-fern-examples is an array of examples. Each element of the array can contain path-parameters, query-parameters, request and response examples values that are all associated.

openapi.yml
1paths:
2 /users/{userId}:
3 get:
4 x-fern-examples:
5 - path-parameters:
6 userId: user-1234
7 response:
8 body:
9 name: Foo
10 ssn: 1234
11 - path-parameters:
12 userId: user-4567
13 response:
14 body:
15 name: Foo
16 ssn: 4567
17components:
18 schemas:
19 User:
20 type: object
21 properties:
22 name:
23 type: string
24 ssn:
25 type: integer

Code samples

If you’d like to specify custom code samples for your example, use code-samples.

openapi.yml
1paths:
2 /users/{userId}:
3 get:
4 x-fern-examples:
5 - path-parameters:
6 userId: user-1234
7 response:
8 body:
9 name: Foo
10 ssn: 1234
11 code-samples:
12 - sdk: typescript
13 code: |
14 import { UserClient } from "...";
15
16 client.users.get("user-1234")

If you’re on the Fern Starter plan or higher for SDKs you won’t have to worry about manually adding code samples! Our generators do that for you.

Availability

The x-fern-availability extension is used to mark the availability of an endpoint. The availability information propagates into the generated Fern Docs website as visual tags.

The options are:

  • beta
  • generally-available
  • deprecated

The example below marks that the POST /pet endpoint is deprecated.

x-fern-availability in openapi.yml
1paths:
2 /pet:
3 post:
4 x-fern-availability: deprecated

This renders as:

Screenshot of API Reference endpoint with tag showing deprecated

Request new extensions

If there’s an extension you want that doesn’t already exist, file an issue to start a discussion about it.