Server-sent events and streaming APIs
Server-sent events and streaming APIs
Use the x-fern-streaming extension to model streaming endpoints
The x-fern-streaming extension allows you to represent endpoints that are streaming.
JSON streaming
If your API returns a series of JSON chunks as seen below
then simply add the x-fern-streaming: true to your OpenAPI operation.
Server-sent events
Team and Enterprise feature
This feature is available only for the Team and Enterprise plans. To get started, reach out to support@buildwithfern.com.
If your API returns server-sent-events (SSE), with the data and event keys as seen below
then make sure to include format: sse.
Generated SDKs expose each event’s metadata — event ID, event type, and retry interval to your end users.
If your SSE endpoint uses the event field to discriminate between different event types in a oneOf, Fern auto-infers that the discriminator is at the protocol level. Use x-fern-discriminator-context to override the inferred value when needed.
Terminator message
Some SSE APIs send a standalone terminator message to signal that the stream is complete. For example,
OpenAI’s API sends [DONE] as a final message. You can specify this with the terminator field:
Resumable streams
Set resumable: true to opt an SSE endpoint into automatic reconnection. When the connection drops mid-stream, the generated SDK reconnects and resends the last event ID in the Last-Event-ID header, so a server that supports that header resumes where the stream left off.
Configure a terminator alongside resumable. The terminator marks a stream as complete, letting the SDK distinguish a finished stream from a dropped connection so it reconnects only on genuine drops.
resumable is inheritable. Set it at the document level to apply to every SSE endpoint, and override it on individual operations:
Stream parameter
It has become common practice for endpoints to have a stream parameter that
controls whether the response is streamed or not. Fern supports this pattern in a first
class way.
Simply specify the stream-condition as well as the ordinary response and the streaming response: