How do I parse a Model API stream with fetch?
Without the SDK, read server-sent events yourself. Each event is a data: line; the stream ends with data: [DONE]. Check every event for an error.
const res = await fetch("https://api.gateway.clatterlabs.xyz/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.CLATTER_API_KEY}`,
"X-Clatter-Account": process.env.CLATTER_ACCOUNT_ID,
"Content-Type": "application/json",
},
body: JSON.stringify({ model: "supercosmos-flash", stream: true,
messages: [{ role: "user", content: "Count to five." }] }),
});
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = "";
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buf += dec.decode(value, { stream: true });
let i;
while ((i = buf.indexOf("\n")) >= 0) {
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
if (!line.startsWith("data:")) continue;
const payload = line.slice(5).trim();
if (payload === "[DONE]") break;
const ev = JSON.parse(payload);
if (ev.error) throw new Error(ev.error.code);
const t = ev.choices?.[0]?.delta?.content;
if (t) process.stdout.write(t);
}
}
A failure partway sends a data: {"error": ...} event and then data: [DONE].
Every JavaScript example uses the same client (base URL, key and the X-Clatter-Account header); see Set up the JavaScript SDK. Full parameter list: Create a chat completion.
Still need help?#
- General support: support@clatterlabs.com or team@clatterlabs.xyz
- Infrastructure questions, or permanent deletion of your data (your whole account or one service): infrastructure@clatterlabs.xyz
We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Related articles
Still need help?
Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyzFor deleting your whole account or the data of one service.