Streaming chat output (TypeScript)
Set stream to true and iterate over response chunks. Individual chunks may contain no text, so check each delta before using it. Close or handle errors in your application.
This guide uses the official OpenAI client with TheRouter; it is not a TheRouter-owned SDK. The therouter and @therouter/sdk packages previously named here are not currently published in the public package registries.
Install and run
bash
npm install openaiSet THEROUTER_API_KEY on your server, then run:
typescript
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.THEROUTER_API_KEY, baseURL: "https://api.therouter.ai/v1" });
const stream = await client.chat.completions.create({ model: "openai/gpt-4o-mini", messages: [{ role: "user", content: "Hello!" }], max_tokens: 32, stream: true });
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta.content ?? "");