Streaming (SSE)

Set "stream": true to receive responses token-by-token via Server-Sent Events. Each chunk is a JSON object with a content field. A final event carries the id, usage, and done flags, then the stream ends with data: [DONE].

Stream Format

data: {"content": "The "}
data: {"content": "meaning "}
data: {"content": "of "}
data: {"content": "life "}
data: {"content": "is "}
data: {"id": "req_abc123...", "usage": {"prompt_tokens": 12, "completion_tokens": 45, "total_tokens": 57}, "done": true}
data: [DONE]

cURL

curl -X POST https://api.aivorylabs.in/v1/chat \
  -H "Authorization: Bearer ax_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "groq/llama-3.3-70b-versatile",
    "messages": [
      {"role": "user", "content": "What is the meaning of life?"}
    ],
    "stream": true
  }'

Python

import requests
import json

resp = requests.post(
    "https://api.aivorylabs.in/v1/chat",
    headers={
        "Authorization": "Bearer ax_live_YOUR_API_KEY",
    },
    json={
        "model": "groq/llama-3.3-70b-versatile",
        "messages": [
            {"role": "user", "content": "What is the meaning of life?"}
        ],
        "stream": True
    },
    stream=True
)

for line in resp.iter_lines():
    if line:
        line = line.decode("utf-8")
        if line.startswith("data: "):
            data = line[6:]
            if data == "[DONE]":
                break
            chunk = json.loads(data)
            if "content" in chunk:
                print(chunk["content"], end="", flush=True)

Node.js

const resp = await fetch("https://api.aivorylabs.in/v1/chat", {
  method: "POST",
  headers: {
    "Authorization": "Bearer ax_live_YOUR_API_KEY",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "groq/llama-3.3-70b-versatile",
    messages: [
      { role: "user", content: "What is the meaning of life?" }
    ],
    stream: true
  })
});

const reader = resp.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");
  for (const line of lines) {
    if (line.startsWith("data: ")) {
      const data = line.slice(6);
      if (data === "[DONE]") break;
      const parsed = JSON.parse(data);
      if (parsed.content) process.stdout.write(parsed.content);
    }
  }
}