# 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

```json
{ "text": "Hi, I am a" }
{ "text": "chatbot. Do you have any"}
{ "text": "questions for me"}
```

then simply add the `x-fern-streaming: true` to your OpenAPI operation.

```yaml title="openapi.yml" {4}
paths:
  /logs:
    post:
      x-fern-streaming: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Chat"
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
```

## Server-sent events

<Warning title="Pro and Enterprise feature">
  This feature is available only for the [Pro and Enterprise plans](https://buildwithfern.com/pricing). To get started, reach out to [support@buildwithfern.com](mailto:support@buildwithfern.com).
</Warning>

If your API returns server-sent-events, with the `data` and `event` keys as seen below

```json
data: { "text": "Hi, I am a" }
data: { "text": "chatbot. Do you have any"}
data: { "text": "questions for me"}
```

then make sure to include `format: sse`.

```yaml title="openapi.yml" {4-5}
paths:
  /logs:
    post:
      x-fern-streaming: 
        format: sse
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Chat"
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
```

## `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:

```yaml title="openapi.yml" {4-10}
paths:
  /logs:
    post:
      x-fern-streaming: 
        format: sse
        stream-condition: $request.stream
        response: 
          $ref: '#/components/schemas/Chat'
        response-stream: 
          $ref: '#/components/schemas/ChatChunk'
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
        tokens: 
          type: number
    ChatChunk: 
      type: object
      properties: 
        text: 
          type: string
```