Skip to content
AZRouter.dev

Guides

OpenAI SDK for TypeScript

Call AZRouter from the official openai npm package.

Tested: openai 7.34.0 (Node 24), 2026-10-16

Shell
npm install openai
TypeScript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.azrouter.dev/v1",
  apiKey: process.env.AZROUTER_API_KEY,
});

const completion = await client.chat.completions.create({
  model: "qwen/qwen3.8-flash",
  messages: [{ role: "user", content: "Reply with exactly: Hello from AZRouter" }],
  max_tokens: 200,
});
console.log(completion.choices[0].message.content);
console.log(completion.usage);

Save it as hello.mts and run npx tsx hello.mts. Top-level await needs an ES module, so a plain .ts file in a default (CommonJS) npm project fails with "Top-level await is currently not supported with the "cjs" output format". Setting "type": "module" in package.json works too.

Streaming

TypeScript
const stream = await client.chat.completions.create({
  model: "qwen/qwen3.8-flash",
  messages: [{ role: "user", content: "Reply with exactly: Hello from AZRouter" }],
  max_tokens: 200,
  stream: true,
});
for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
console.log();

For the usage chunk at the end of a stream, see Streaming.