Skip to content
GuidesJuly 19, 2026

Use AnyRouter with the Vercel AI SDK

@anyr/ai-sdk-provider wraps the AnyRouter gateway as a standard Vercel AI SDK provider. Call createAnyRouter(), pass a provider/model id, and generateText, streamText, and generateObject work unchanged — switching models is a one-string edit.

Install and create the provider

The provider is a thin wrapper over the AnyRouter API that implements the AI SDK's provider interface. Install it alongside the AI SDK itself, then call createAnyRouter() — it reads ANYROUTER_API_KEY from the environment when you omit the key.

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

const anyrouter = createAnyRouter()

const { text } = await generateText({
  model: anyrouter("z-ai/glm-4.7-flash"),
  prompt: "What is AnyRouter?",
})
console.log(text)
createAnyRouter() reads ANYROUTER_API_KEY when the key is omitted.

Stream, and swap the model id

Every AI SDK function takes the same model factory, so streamText is the same call with a different helper. And because model ids are plain provider/model strings, changing which model runs is a one-string edit — no new client, no new key.

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

const anyrouter = createAnyRouter()

const { textStream } = await streamText({
  // swap freely: "anthropic/claude-opus-4-5" or the free tier "anyrouter/free"
  model: anyrouter("z-ai/glm-4.7-flash"),
  prompt: "Write a haiku about the ocean.",
})

for await (const chunk of textStream) {
  process.stdout.write(chunk)
}
Model idWhat it routes to
z-ai/glm-4.7-flashZ.AI GLM-4.7 Flash
anthropic/claude-opus-4-5Anthropic Claude Opus 4.5
anyrouter/freeThe free tier — no credits spent

Structured output with generateObject

generateObject works the same way — pass a zod schema and the provider returns a typed object. This holds across models, so you can move a structured-output task to a cheaper or a stronger model without touching the schema.

import { createAnyRouter } from "@anyr/ai-sdk-provider"
import { generateObject } from "ai"
import { z } from "zod"

const anyrouter = createAnyRouter()

const { object } = await generateObject({
  model: anyrouter("z-ai/glm-4.7-flash"),
  schema: z.object({
    title: z.string(),
    summary: z.string(),
    tags: z.array(z.string()),
  }),
  prompt: "Summarise the latest AI news in a structured format.",
})

console.log(object.title, object.tags)

Not on the AI SDK? Use @anyr/sdk

If your app doesn't use the Vercel AI SDK, the plain TypeScript SDK talks to the same gateway with the same key. It exposes an OpenAI-shaped client — chat, embeddings, the Responses API, and the Anthropic Messages API — with typed requests and responses.

import { AnyRouter } from "@anyr/sdk"

// Reads 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)

Both packages read ANYROUTER_API_KEY — create one to get started.

Create a key

Route your first request in 2 minutes

Start free with your own keys, or top up and pay per token. Get $4/mo in credits and free models on Go — $2/mo, or free when you donate a provider key.

Start free