data: and the stream ends with data: [DONE].
Streaming
Parse the SSE stream in real time. Each chunk contains adelta 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);
}
}
import requests
import json
response = requests.post(
"https://api.tinytalk.ai/v1/chat/completions",
headers={
"Content-Type": "application/json",
"api-key": "tiny_sk_your_key_here"
},
json={
"botId": "your-agent-id",
"messages": [
{"role": "user", "content": "What are your business hours?"}
]
},
stream=True
)
for line in response.iter_lines():
if not line:
continue
decoded = line.decode("utf-8")
if not decoded.startswith("data: "):
continue
data = decoded.removeprefix("data: ")
if data == "[DONE]":
break
parsed = json.loads(data)
content = parsed["choices"][0]["delta"].get("content", "")
if content:
print(content, end="", flush=True)
Non-streaming
Setstream 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);
import requests
response = requests.post(
"https://api.tinytalk.ai/v1/chat/completions",
headers={
"Content-Type": "application/json",
"api-key": "tiny_sk_your_key_here"
},
json={
"botId": "your-agent-id",
"stream": False,
"messages": [
{"role": "user", "content": "What are your business hours?"}
]
}
)
data = response.json()
print(data["choices"][0]["message"]["content"])