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

# Streaming

Streaming allows your application to display the AI's response to the user as it is being generated, rather than waiting for\
the entire response to finish. This significantly improves the perceived speed and user experience.

The Draftify API uses standard Server-Sent Events (SSE) for streaming, making it fully compatible with most OpenAI-compatible\
SDKs.

## Enabling Streaming

To enable streaming, simply include `"stream": true` in the root of your JSON request body.

### cURL Example

```bash theme={null}
curl -X POST https://www.draftify.site/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_DRAFTIFY_API_KEY" \
  -d '{
    "model": "sonnet-4.6-level",
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Write a long story about a robot."
      }
    ]
  }'

### Node.js Example

const response = await fetch('https://www.draftify.site/api/v1/chat/completions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.DRAFTIFY_API_KEY}`,
  },
  body: JSON.stringify({
    model: 'sonnet-4.6-level',
    stream: true,
    messages: [{ role: 'user', content: 'Write a long story about a robot.' }]
  }),
});

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, { stream: true });
  console.log(chunk);
}
──────
## Understanding the Stream Format

When streaming is enabled, the API will respond with a continuous stream of text chunks. Each event begins with  data:   and        
contains a stringified JSON object.

A typical stream chunk looks like this:

data: {"choices": [{"index": 0, "delta": {"content": "Hello"}}]}

The very last event in the stream will always be:

data: [DONE]
──────
## Vercel AI SDK Integration

Because the Draftify API strictly follows the standard OpenAI Chat Completions format, it works out-of-the-box with popular
frameworks like the Vercel AI SDK.

You can use the  @ai-sdk/openai  provider and just override the  baseURL  to point to Draftify:

import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';

const draftify = createOpenAI({
  baseURL: 'https://www.draftify.site/api/v1',
  apiKey: process.env.DRAFTIFY_API_KEY,
});

export async function POST(req) {
  const { messages } = await req.json();

  const result = await streamText({
    model: draftify('opus-4.8-level'),
    messages,
  });

  return result.toDataStreamResponse();
}
```
