> ## Documentation Index
> Fetch the complete documentation index at: https://api-docs.atlasflux.my/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming Responses

> Receive token-by-token responses in real-time.

## Enable streaming

Set `"stream": true` in your request:

```bash theme={null}
curl https://api.atlasflux.my/v1/chat/completions \
  -H "Authorization: Bearer $ATLASFLUX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Tell me a story"}],
    "stream": true
  }'
```

## Server-Sent Events format

The response is a `text/event-stream`. Each event is an SSE message:

```
data: {"choices":[{"delta":{"content":"Once"}}]}
data: {"choices":[{"delta":{"content":" upon"}}]}
data: {"choices":[{"delta":{"content":" a time"}}]}
data: [DONE]
```

## Processing in JavaScript

```javascript theme={null}
const response = await fetch("https://api.atlasflux.my/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.ATLASFLUX_API_KEY}`,
  },
  body: JSON.stringify({
    messages: [{ role: "user", content: "Tell me a story" }],
    stream: true,
  }),
});

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);
  const lines = chunk.split("\n");

  for (const line of lines) {
    if (line.startsWith("data: ") && line !== "data: [DONE]") {
      const data = JSON.parse(line.slice(6));
      const content = data.choices?.[0]?.delta?.content;
      if (content) process.stdout.write(content);
    }
  }
}
```

## Processing in Python

```python theme={null}
import requests
import os

response = requests.post(
    "https://api.atlasflux.my/v1/chat/completions",
    headers={"Authorization": f"Bearer {os.environ['ATLASFLUX_API_KEY']}"},
    json={
        "messages": [{"role": "user", "content": "Tell me a story"}],
        "stream": True,
    },
    stream=True,
)

for line in response.iter_lines():
    if line and line.startswith(b"data: "):
        payload = line[6:]
        if payload == b"[DONE]":
            break
        print(payload.decode(), end="")
```
