TypeScript / Node.js SDK
The official nRouter Node.js / TypeScript SDK with automatic metadata parsing, streaming, prompt templates, and multi-turn memory.
Last updated
The official nRouter Node.js SDK (@nrouter_ai/sdk) provides first-class TypeScript types, automatic x-nr-* header parsing, client-side conversation memory, server-side prompt templating, and full compatibility with both OpenAI and Anthropic models.
Installation
Install the official package from npm:
npm install @nrouter_ai/sdkOr with pnpm / yarn / bun:
pnpm add @nrouter_ai/sdk
# or
yarn add @nrouter_ai/sdk
# or
bun add @nrouter_ai/sdkAuthentication & Setup
Export your virtual API key in your environment:
export NROUTER_API_KEY="sk-nrouter-your-key-here"Initialize the client with automatic environment resolution:
import { nRouter } from "@nrouter_ai/sdk";
const client = new nRouter(); // reads NROUTER_API_KEY automatically
async function main() {
const response = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages: [{ role: "user", content: "Hello from TypeScript!" }],
});
console.log(response.choices[0].message.content);
// Inspect auto-captured response metadata
if (client.lastResponse) {
console.log(`Cost: $${client.lastResponse.cost}`);
console.log(`Request ID: ${client.lastResponse.requestId}`);
}
}
main();Core Capabilities & Examples
| Topic | Example File | Description |
|---|---|---|
| Quickstart | quickstart.ts | First-run TypeScript starter |
| Complete Showcase | node.ts | Multi-turn memory, prompt templates, conflict resolution |
| Vercel AI SDK | vercel_ai.ts | Integration with Vercel AI SDK (@ai-sdk/openai) |
| E2E Suite | demo_e2e_suite.js | Certified end-to-end multi-language test |
1. Streaming Responses
Stream tokens in real-time with Server-Sent Events (SSE):
import { nRouter } from "@nrouter_ai/sdk";
const client = new nRouter();
const stream = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages: [{ role: "user", content: "Write a short poem about distributed systems." }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
process.stdout.write(content);
}
console.log();2. Multi-Turn Conversation Memory
Use createMemory() to maintain client-side message history without header leaks:
import { nRouter, createMemory } from "@nrouter_ai/sdk";
const client = new nRouter();
const memory = createMemory({ maxMessages: 10 });
memory.add("user", "My name is Alice and I am building an AI agent.");
const first = await client.nr.chat({
model: "gpt-5.4-mini",
messages: memory.messages(),
});
memory.add("assistant", client.nr.text(first));
memory.add("user", "What is my name and what am I building?");
const second = await client.nr.chat({
model: "gpt-5.4-mini",
messages: memory.messages(),
});
console.log(client.nr.text(second));3. Server-Side Prompt Templates
Combine dashboard prompt templates with user variables:
import { nRouter, promptTemplate } from "@nrouter_ai/sdk";
const client = new nRouter();
const response = await client.nr.chat(
promptTemplate("tmpl_onboarding_v1", { user_role: "Tech Lead", plan: "Enterprise" }, {
model: "gpt-5.4-mini",
messages: [{ role: "user", content: "Show me my workspace dashboard." }],
})
);
console.log(client.nr.text(response));4. Plain OpenAI Client Drop-in
If you prefer standard openai package without additional wrappers:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.NROUTER_API_KEY,
baseURL: "https://api.nrouter.ai/v1",
});
const response = await client.chat.completions.create({
model: "gpt-5.4-mini",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);Python SDK
The official nRouter Python SDK with automatic metadata extraction, real-time cost tracking, Anthropic messages, and context management.
Go SDK
Integrate Go applications with nRouter using our official Go SDK or OpenAI Go client. Access typed helpers, automatic cost tracking, and resilient routing.