Authentication
Fern supports two ways to configure authentication:
- In your OpenAPI spec using
securitySchemes— the standard approach that keeps auth configuration portable and works with other OpenAPI tools. - In
generators.ymlusingauth-schemes— use this to customize parameter names and environment variables, override what’s defined in your spec, or configure OAuth (which isn’t available in OpenAPI).
Your authentication configuration applies across generated SDKs and the API Explorer. All SDKs support both direct configuration and environment variables for credentials. If you define the same scheme in both places, generators.yml takes precedence.
Configure authentication in your spec
Define your schemes in components.securitySchemes, then apply them globally or per-endpoint using the security property.
Generated SDK usage:
Bearer
To customize parameter names and environment variables, add x-fern-bearer:
Basic
To customize parameter names and environment variables, add x-fern-basic:
API key
To customize parameter names and environment variables, add x-fern-header:
The prefix option automatically prepends a string to API keys, useful when your API expects formats like "Bearer abc123" or "Token abc123".
Multiple auth schemes
Configure endpoints to support multiple authentication schemes or combinations. In the security section, multiple top-level items are OR options, while schemes within a single item are combined with AND.
In this example, users can authenticate with either a bearer token OR with both basic auth and an API key together.
When using OAuth client credentials with multiple schemes, ensure the scheme name in your OpenAPI spec’s security section matches the name defined in generators.yml.
Customize or override authentication in generators.yml
Define your scheme in auth-schemes, then apply it as the default across all endpoints with api.auth:
For complete configuration options, see the auth-schemes reference. You can also override authentication settings for a specific SDK.
Generated SDK usage:
Bearer
Basic
API key
OAuth client credentials
Pro and Enterprise feature
This feature is available on Pro and Enterprise plans. Contact support@buildwithfern.com to get started.
The endpoint values reference paths in your OpenAPI spec. When expires-in is returned, the SDK automatically refreshes tokens before they expire.