# 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.

- Date: July 19, 2026
- Category: guides
- HTML: https://anyrouter.dev/blog/ai-sdk-anyrouter

## 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.

```bash
npm install @anyr/ai-sdk-provider ai zod
```

```ts
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.

```ts
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 id | What it routes to |
| --- | --- |
| z-ai/glm-4.7-flash | Z.AI GLM-4.7 Flash |
| anthropic/claude-opus-4-5 | Anthropic Claude Opus 4.5 |
| anyrouter/free | The 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.

```ts
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.

```ts
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](https://dash.anyrouter.dev/keys)
