Streaming
TokenSupernova supports server-sent events (SSE) for streaming responses. Set stream: true to receive tokens as they’re generated.
Python
Section titled “Python”from openai import OpenAI
client = OpenAI( api_key="tsn_live_xxx", base_url="https://api.tokensupernova.com/v1",)
stream = client.chat.completions.create( model="deepseek-chat", messages=[{"role": "user", "content": "Write a haiku about the ocean."}], stream=True,)
for chunk in stream: if chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="")Node.js
Section titled “Node.js”import OpenAI from "openai";
const client = new OpenAI({ apiKey: "tsn_live_xxx", baseURL: "https://api.tokensupernova.com/v1",});
const stream = await client.chat.completions.create({ model: "deepseek-chat", messages: [{ role: "user", content: "Write a haiku about the ocean." }], stream: true,});
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || "");}curl "https://api.tokensupernova.com/v1/chat/completions" \ -H "Authorization: Bearer tsn_live_xxx" \ -H "Content-Type: application/json" \ -d '{ "model": "deepseek-chat", "messages": [{"role": "user", "content": "Write a haiku about the ocean."}], "stream": true }'Streaming Chunk Format
Section titled “Streaming Chunk Format”Each chunk is a JSON object prefixed with data: :
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Deep"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" blue"},"finish_reason":null}]}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]The stream ends with data: [DONE].