Global headers

View as Markdown

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):

Fern automatically pulls out headers that are used in every request, or the majority of requests, and marks them as global.

In your OpenAPI spec

To label additional headers as global, or to alias the names of global headers, use 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

When you define global headers using x-fern-global-headers, you must include them in your x-fern-examples.

Default values

Set a client-side default value on a global header using x-fern-default. The generated SDK makes the header optional and sends the default when the caller omits it:

openapi.yml
1x-fern-global-headers:
2 - header: X-API-Version
3 name: version
4 x-fern-default: "2024-02-08"

In generators.yml

Alternatively, you can add headers to the api block in your generators.yml file:

generators.yml
1api:
2 - openapi: ./path/to/openapi
3 headers:
4 custom_api_key:
5 name: api_key
6 type: string
7 userpool_id:
8 name: userpool_id
9 type: optional<string>

Generated SDK behavior

Both configurations yield the following client code:

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