# Server-sent events and streaming APIs

> Use the `response-stream` key to model streaming endpoints

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

Specifying `response-stream` on an endpoints allows you to represent endpoint responses 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 specify the response under `response-stream` for your endpoint.

```yaml title="chat.yml" {4}
service:
  base-path: /chat
  endpoints:
    stream:
      method: POST
      path: ""
      response-stream: Chat

types:
  Chat:
    properties:
      text: string
```

## Server-sent events

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="chat.yml" {9}
service:
  base-path: /chat
  endpoints:
    stream:
      method: POST
      path: ""
      response-stream:
        type: Chat
        format: sse

types:
  Chat:
    properties:
      text: 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="chat.yml" {7}
service:
  base-path: /chat
  endpoints:
    stream:
      method: POST
      path: ""
      stream-condition: $request.stream
      request:
        name: StreamChatRequest
        body: 
          properties: 
            stream: boolean
      response: Chat
      response-stream:
        type: ChatChunk
        format: sse

types:
  Chat:
    properties:
      text: string
      tokens: integer
  ChatChunk:
    properties:
      text: string
```