Streaming (SSE)

Stream chat responses in real time using Server-Sent Events.


Enabling Streaming

To receive a streaming response instead of a JSON payload, set the Accept header to text/event-stream on your POST /api/v1/chats/{chatId}/messages request.

Request with streaming

curl -X POST https://api.wondercat.ai/api/v1/chats/chat_abc/messages \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Accept: text/event-stream" \
  -d '{ "message": "Generate a product video" }'

Response Format

The stream returns a Vercel AI SDK UIMessageStream. Each event is a newline-delimited SSE chunk that can be parsed with the @ai-sdk/ui-utils package.

Example SSE stream

data: {"type":"text-delta","textDelta":"Here's"}
data: {"type":"text-delta","textDelta":" your"}
data: {"type":"text-delta","textDelta":" video!"}
data: {"type":"tool-call","toolCallId":"tc_1","toolName":"generateVideo","args":{"prompt":"..."}}
data: {"type":"tool-result","toolCallId":"tc_1","result":{"mediaId":"media_out_1"}}
data: {"type":"finish","finishReason":"stop"}

Client-Side Parsing

Use the @ai-sdk/ui-utils package to parse the stream into structured messages.

Parsing with @ai-sdk/ui-utils

import { parseUIMessageStreamPart } from "@ai-sdk/ui-utils";

const response = await fetch(
  "https://api.wondercat.ai/api/v1/chats/chat_abc/messages",
  {
    method: "POST",
    headers: {
      Authorization: "Bearer YOUR_API_KEY",
      "Content-Type": "application/json",
      Accept: "text/event-stream",
    },
    body: JSON.stringify({ message: "Generate a product video" }),
  },
);

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 });
  const lines = chunk.split("\n").filter((l) => l.startsWith("data: "));

  for (const line of lines) {
    const part = parseUIMessageStreamPart(line.slice(6));
    (() => {})(part.type, part);
  }
}