# Streamable HTTP

The Streamable HTTP protocol allows you to consume AI app responses in real-time. Instead of waiting for the entire response to generate (which can take seconds for complex Generative AI tasks), this API streams the response chunks as they become available.

### When to use this API

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><i class="fa-brain-circuit">:brain-circuit:</i></td><td><strong>Generative AI experiences</strong></td><td>When your AI app generates long-form content, streaming reduces the "Time to First Byte" perception, making the app feel faster</td></tr><tr><td><i class="fa-timer">:timer:</i></td><td><strong>Real-time UIs</strong></td><td>For typing indicators or "ghost text" effects, similar to ChatGPT</td></tr></tbody></table>

### How it works

This protocol utilizes Server-Sent Events (SSE). You use the exact same endpoint and authentication as the standard REST API, but you enable the `stream` flag in your request.

#### Endpoint configuration

* *URL*: `https://apps.nlx.ai/c/{deploymentKey}/{channelKey}-{languageCode}`
* *Header*: `nlx-api-key: YOUR_KEY`
* *Content-type*: `application/json`

{% hint style="info" %}
Ensure your URL ends with the language code (e.g., `-en-US`).
{% endhint %}

#### Enabling the Stream

Add `"stream": true` to the root of your JSON body.

```bash
curl -N -X POST "https://apps.nlx.ai/c/xxxx/xxxx-en-US" \
  -H "nlx-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "stream": true,
    "request": {
      "unstructured": {
        "text": "Write a short poem about coding."
      }
    }
  }'
```

### Consuming the stream

The server will respond with `Content-type: text/event-stream`. Data is sent in chunks prefixed with `data:`.

#### JavaScript client cxample

In a browser or Node.js environment, you handle the stream by reading the response reader.

```javascript
const response = await fetch('https://apps.nlx.ai/c/xxxx/xxxx-en-US', {
  method: 'POST',
  headers: {
    'nlx-api-key': 'YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    stream: true,
    request: { unstructured: { text: "Hello" } }
  })
});

const reader = response.body.getReader();
const decoder = new TextDecoder();

while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  
  const chunk = decoder.decode(value);
  console.log("Received chunk:", chunk);
  // Parse 'data: {...}' lines here
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nlx.ai/platform/developers/conversation-api/streamable-http.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
