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 togetherx-fern-sdk-method-name
which is used as the method name in the SDK
The OpenAPI below will client.users.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()
:
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 selects 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:
In a Python SDK for example, the above configuration would produce a client that looks like:
Similarly, for basic authentication the configuration is:
And for a custom auth header configuration:
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:
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:
yields the following client:
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.
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
would generate
Schema names
OpenAPI allows you to define inlined schemas that do not have names.
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.
If you want to override the generated name, you can use the extension x-fern-type-name
.
This would replace CastItem
with Person
and the generated code would read more idiomatically:
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
.
Server names
The x-fern-server-name
extension is used to name your servers.
In a generated TypeScript SDK, you’d see:
Audiences
The x-fern-audiences
extension is used to mark endpoints with a particular audience
so that you can filter your SDKs and Docs as you would like in Fern.
In the example below, we have marked the POST /users
endpoint with the public
audience.
Audiences with SDKs
To filter to the public audience when generating code, update your generators.yml
configuration to include:
Audiences with Docs
If generating Fern Docs, you’ll need to update your docs.yml
configuration to include:
Streaming
The x-fern-streaming
extension is used to specify that an endpoint’s response is streamed.
Simply include the x-fern-streaming
extension within your endpoint like so:
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:
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 endpoint or schemas, you can use the
x-fern-ignore
extension.
To skip an endpoint, you must add x-fern-ignore: true
at the operation
level.
To skip a schema, you msut add x-fern-ignore: true
at the schema level.
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 use the x-fern-overrides-filepath
extension.
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.
Request + Response Examples
While OpenAPI has several fields for examples, there is no easy way to associate a request with a response. This is expecially 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.
Code Samples
If you’d like to specify custom code samples for your example,
use code-samples
.
If you’re on the Fern Starter plan for SDKs you won’t have to worry about manually adding code samples! Our generators will do that for you.
If there’s an extension you want that doesn’t already exist, file an issue to start a discussion about it.