Autopopulate API keys
Pro and Enterprise feature
This feature is available only for the Pro and Enterprise plans. To get started, reach out to support@buildwithfern.com.
Fern can integrate with your authentication flow, allowing users to log in and have their API key automatically populated with the click of a button.
User credentials are stored only in browser cookies and never transmitted to Fern’s servers. Learn more in the Security overview.
The fern payload
API key injection works by reading a fern claim from a JWT stored in the user’s browser. The fern claim contains a playground object that tells the API Explorer what values to pre-fill.
The top-level structure is:
Auth types
The initial_state.auth object supports bearer token and basic authentication.
Custom headers and parameters
You can pre-fill headers, path parameters, and query parameters in addition to (or instead of) auth credentials. Values are applied to any matching fields in the API Explorer.
Multiple API keys
To provide multiple API keys (for example, one per application), set bearer_token to a JSON-encoded array of key-value objects. Each object maps an application name to its key.
The API Explorer displays a dropdown so the user can choose which application key to use for requests.
Per-environment keys
Use env_state to provide different credentials for each environment (for example, production vs. staging). Each key in env_state is matched against the currently selected environment URL using substring matching.
When the user selects an environment containing prod (such as https://api.prod.example.com), the API Explorer uses prod-token-abc123. The env_state values are merged on top of initial_state: auth is replaced entirely, while headers, path parameters, and query parameters are shallow-merged.
You can combine multiple API keys with per-environment keys:
Integrating with your auth flow
API key injection requires a JWT containing the fern payload described above to be stored in the user’s browser as a cookie called fern_token. There are two ways to set this up:
- JWT: You handle the entire auth flow and set the
fern_tokencookie yourself. - OAuth: You give Fern access, and Fern initiates the OAuth handshake process directly.
JWT
OAuth
- When a user clicks the Login button in the API Explorer, they are redirected to your authentication page.
- After successful authentication, your system sets a cookie called
fern_tokenin the user’s browser. - This token is a JWT signed with a secret key from Fern. The JWT payload contains the
fernclaim with the user’s API key.
Architecture diagram
Set up
To enable API key injection with JWT:
- Reach out to Fern to get your secret key.
- Send Fern the URL of your authentication page. This is where users are redirected after clicking the Login button in the API Explorer.
- Add logic to your service to set the
fern_tokencookie when a user logs in.
Example: Complete callback endpoint
This Next.js endpoint handles the callback from your authentication page. It reads the state parameter to determine where to redirect the user, mints a fern_token JWT using jose, sets it as a cookie, and redirects the user back to the docs.