Skip to main content
The Chat Completions endpoint streams responses by default using server-sent events (SSE). Each chunk is prefixed with data: and the stream ends with data: [DONE].

Streaming

Parse the SSE stream in real time. Each chunk contains a delta object with incremental content.
const response = await fetch("https://api.tinytalk.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "api-key": "tiny_sk_your_key_here"
  },
  body: JSON.stringify({
    botId: "your-agent-id",
    messages: [
      { role: "user", content: "What are your business hours?" }
    ]
  })
});

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").filter((line) => line.startsWith("data: "));

  for (const line of lines) {
    const data = line.replace("data: ", "");
    if (data === "[DONE]") break;

    const parsed = JSON.parse(data);
    const content = parsed.choices?.[0]?.delta?.content;
    if (content) process.stdout.write(content);
  }
}

Non-streaming

Set stream to false to receive the complete response in a single JSON payload.
const response = await fetch("https://api.tinytalk.ai/v1/chat/completions", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "api-key": "tiny_sk_your_key_here"
  },
  body: JSON.stringify({
    botId: "your-agent-id",
    stream: false,
    messages: [
      { role: "user", content: "What are your business hours?" }
    ]
  })
});

const data = await response.json();
console.log(data.choices[0].message.content);