API Reference
Stream chat responses token-by-token using Server-Sent Events (SSE) for real-time UX. Enable streaming by setting stream: true in your chat request.
Add stream: true to the request body:
bash
curl -X POST https://chatspark.io/api/v1/agents/{id}/chat \
-H "Authorization: Bearer cs_live_..." \
-H "Content-Type: application/json" \
-d '{"message":"Hello!","stream":true}'The response is a stream of SSE events. Each data line contains a JSON object:
json
data: {"delta":"Hi","conversationId":"conv_abc123"}
data: {"delta":"! ","conversationId":"conv_abc123"}
data: {"delta":"How","conversationId":"conv_abc123"}
data: {"delta":" can","conversationId":"conv_abc123"}
...
data: [DONE]Each event has a delta (the next token) and conversationId. The stream ends with data: [DONE].
const res = await fetch(`https://chatspark.io/api/v1/agents/${agentId}/chat`, {
method: "POST",
headers: {
"Authorization": "Bearer cs_live_...",
"Content-Type": "application/json",
},
body: JSON.stringify({ message: "Hello!", stream: true }),
});
if (!res.ok) throw new Error(`Request failed: ${res.status}`);
const reader = res.body.getReader();
const decoder = new TextDecoder();
let fullText = "";
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 data = line.slice(6);
if (data === "[DONE]") break;
try {
const parsed = JSON.parse(data);
if (parsed.delta) {
fullText += parsed.delta;
process.stdout.write(parsed.delta);
}
} catch {}
}
}
console.log("\n--- Full response ---\n", fullText);