Skip to content

AnyRouter SDK

Official TypeScript SDK for the AnyRouter API — one key and one base URL for chat, embeddings, the Responses API, and the Anthropic Messages API, with a typed client and a Vercel AI SDK provider.

The official TypeScript SDK for the AnyRouter API. One key, one base URL, every model — with typed requests, typed responses, a typed error envelope, and automatic retries.

Packages

PackageUse it for
@anyr/sdkThe direct API client — chat, embeddings, models, credits, keys, presets, the Responses API, and the Anthropic Messages API
@anyr/ai-sdk-providerUsing the Vercel AI SDK? Call every AnyRouter model through createAnyRouter

Quickstart

Install the SDK

npm install @anyr/sdk

Set your key

The client reads ANYROUTER_API_KEY from the environment when you omit it. Get a key at anyrouter.dev/dashboard/keys.

export ANYROUTER_API_KEY=sk-ar-v1-...

Make a request

import { AnyRouter } from "@anyr/sdk"

// Reads the key from ANYROUTER_API_KEY when omitted.
const client = new AnyRouter()

const completion = await client.chat.completions.create({
  model: "z-ai/glm-4.7-flash",
  messages: [{ role: "user", content: "Hello!" }],
})

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

Streaming

Pass stream: true to receive tokens as they are generated:

const stream = await client.chat.completions.create({
  model: "z-ai/glm-4.7-flash",
  messages: [{ role: "user", content: "Write a haiku about TypeScript." }],
  stream: true,
})

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

Reliability

Every request automatically retries transient failures — connection errors, timeouts, and 429 / 5xx responses — with exponential backoff that honors the server's Retry-After headers. A per-request timeout bounds the wait for the first response without aborting a running stream, and you can pass an AbortSignal, timeout, maxRetries, or one-off headers per call:

const controller = new AbortController()

const stream = await client.chat.completions.create(
  { model: "z-ai/glm-4.7-flash", messages, stream: true },
  { signal: controller.signal, timeout: 30_000 },
)

Vercel AI SDK

Prefer the Vercel AI SDK? Install @anyr/ai-sdk-provider and point any model id at AnyRouter through createAnyRouter:

import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateText } from "ai"

const anyrouter = createAnyRouter() // reads ANYROUTER_API_KEY

const { text } = await generateText({
  model: anyrouter("z-ai/glm-4.7-flash"),
  prompt: "What is AnyRouter?",
})