Node.js / TypeScript SDK

Planned Node SDK for AllRoutes. Until it ships, use the OpenAI Node client pointed at https://api.allroutes.ai/v1.

Not yet published. The native @allroutes/sdk package on this page is on the roadmap but is not on npm yet. Use the OpenAI Node client pointed at https://api.allroutes.ai/v1 for now -- see the snippet directly below. The rest of this document previews the surface the native SDK will ship with.

Use the OpenAI Client Today

npm install openai
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.ALLROUTES_API_KEY,
  baseURL: "https://api.allroutes.ai/v1",
});

const completion = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(completion.choices[0].message.content);

Installation (planned)

npm install @allroutes/sdk   # not yet published
# or
yarn add @allroutes/sdk
# or
pnpm add @allroutes/sdk

Requires Node.js 18+ or any modern runtime with fetch support (Bun, Deno).

Quick Start

import AllRoutes from "@allroutes/sdk";

const client = new AllRoutes({
  apiKey: "allroutes_sk_...",
});

const completion = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Explain quantum computing in one paragraph." }],
});

console.log(completion.choices[0].message.content);

OpenAI SDK Compatibility

The AllRoutes SDK extends the official OpenAI SDK. All OpenAI methods and types work out of the box -- just swap the import:

// Before (OpenAI)
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "sk-..." });

// After (AllRoutes)
import AllRoutes from "@allroutes/sdk";
const client = new AllRoutes({ apiKey: "allroutes_sk_..." });

// The rest of your code stays the same
const completion = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
});

Access any provider by changing the model name:

// Anthropic
const response = await client.chat.completions.create({
  model: "claude-sonnet-4-20250514",
  messages: [{ role: "user", content: "Hello from Claude!" }],
});

// Google
const response = await client.chat.completions.create({
  model: "gemini-2.0-flash",
  messages: [{ role: "user", content: "Hello from Gemini!" }],
});

Streaming

const stream = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Write a short story about a robot." }],
  stream: true,
});

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content;
  if (content) {
    process.stdout.write(content);
  }
}

Streaming with Abort Controller

const controller = new AbortController();

const stream = await client.chat.completions.create(
  {
    model: "gpt-4o",
    messages: [{ role: "user", content: "Write a long essay." }],
    stream: true,
  },
  { signal: controller.signal }
);

// Cancel after 5 seconds
setTimeout(() => controller.abort(), 5000);

Credits

const credits = await client.credits.get();
console.log(`Balance: $${credits.balance_usd.toFixed(2)} USD`);
console.log(`Lifetime spend: $${credits.lifetime_spend_usd.toFixed(2)} USD`);
console.log(`Auto-replenish: ${credits.auto_replenish}`);

Presets

// Use a preset
const completion = await client.chat.completions.create({
  preset: "customer-support-v2",
  messages: [{ role: "user", content: "How do I reset my password?" }],
});

// List presets
const presets = await client.presets.list();
for (const preset of presets.data) {
  console.log(`${preset.name} -> ${preset.model}`);
}

Models

const models = await client.models.list();
for (const model of models.data) {
  console.log(`${model.id} by ${model.provider} — $${model.pricing.prompt}/1M tokens`);
}

Model Fallbacks

const completion = await client.chat.completions.create({
  models: ["claude-sonnet-4-20250514", "gpt-4o", "gemini-2.0-flash"],
  messages: [{ role: "user", content: "Summarize this document." }],
});

// Check which model was used
console.log(`Served by: ${completion.model}`);

Configuration

ParameterTypeDefaultDescription
apiKeystring(required)Your AllRoutes API key
baseURLstringhttps://api.allroutes.aiAPI base URL
timeoutnumber120000Request timeout in milliseconds
maxRetriesnumber2Max retries for failed requests
const client = new AllRoutes({
  apiKey: "allroutes_sk_...",
  baseURL: "https://custom-gateway.example.com",
  timeout: 60000,
  maxRetries: 3,
});

Error Handling

import AllRoutes, { APIError, RateLimitError, AuthenticationError } from "@allroutes/sdk";

const client = new AllRoutes({ apiKey: "allroutes_sk_..." });

try {
  const completion = await client.chat.completions.create({
    model: "gpt-4o",
    messages: [{ role: "user", content: "Hello!" }],
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error("Invalid API key");
  } else if (error instanceof RateLimitError) {
    console.error("Rate limit exceeded, retry after:", error.headers?.["retry-after"]);
  } else if (error instanceof APIError) {
    console.error(`API error ${error.status}: ${error.message}`);
  } else {
    console.error("Unexpected error:", error);
  }
}

The client automatically retries on transient errors (5xx and 429) with exponential backoff.

Environment Variable

The SDK reads ALLROUTES_API_KEY from the environment if no apiKey is passed:

export ALLROUTES_API_KEY="allroutes_sk_..."
const client = new AllRoutes(); // reads from environment

TypeScript Support

The SDK ships with full TypeScript definitions. All request and response types are exported:

import AllRoutes, {
  type ChatCompletionCreateParams,
  type ChatCompletion,
} from "@allroutes/sdk";

const params: ChatCompletionCreateParams = {
  model: "gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
  temperature: 0.7,
};

const completion: ChatCompletion = await client.chat.completions.create(params);

Edge Runtime Support

The SDK works in any runtime with fetch -- Node 18+, Bun, Deno, Cloudflare Workers, Vercel Edge Functions, and Next.js Edge runtime. No Node-specific APIs are imported.

// app/api/chat/route.ts (Next.js Edge runtime)
export const runtime = "edge";

import AllRoutes from "@allroutes/sdk";

export async function POST(req: Request) {
  const client = new AllRoutes({ apiKey: process.env.ALLROUTES_API_KEY });
  const { messages } = await req.json();

  const stream = await client.chat.completions.create({
    model: "gpt-4o",
    messages,
    stream: true,
  });

  return new Response(stream.toReadableStream());
}

See Also