Docs navigation

SDKs

TypeScript SDK

The official tokenrouter package for Node and edge runtimes.

The tokenrouter package mirrors the OpenAI SDK’s ergonomics, so existing OpenAI code ports by changing the import.

Install

bash
npm install tokenrouter

Create a client

typescript
import TokenRouter from "tokenrouter";

const client = new TokenRouter({
  apiKey: process.env.TOKENROUTER_API_KEY, // defaults to this env var
  // baseURL: "https://api.tokenrouter.io/v1", // the default; override for testing
});

Chat completion

typescript
const response = await client.chat.completions.create({
  model: "openai/gpt-5-mini",
  messages: [
    { role: "system", content: "You are a concise assistant." },
    { role: "user", content: "What is an AI gateway?" },
  ],
});

console.log(response.choices[0].message.content);
console.log(response.usage); // token counts for this call

Streaming

typescript
const stream = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-4-5",
  stream: true,
  stream_options: { include_usage: true },
  messages: [{ role: "user", content: "Write a haiku about hard caps." }],
});

for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content;
  if (delta) process.stdout.write(delta);
  if (chunk.usage) console.log("\n", chunk.usage);
}

Also good to know

  • client.embeddings.create(...) and client.models.list() work the same as in the OpenAI SDK.
  • Errors throw typed exceptions with the gateway’s code field intact — see Errors.
  • Prefer the stock openai package? It works unchanged with baseURL: "https://api.tokenrouter.io/v1" and your tr_ key.