# Vercel AI SDK

> Use the Vercel AI SDK with AnyRouter by pointing the OpenAI provider at one endpoint.


# Use the Vercel AI SDK with AnyRouter

Configure the `@ai-sdk/openai` provider with AnyRouter's base URL and an API key (`sk-ar-...`, from the [dashboard](https://anyrouter.dev/dashboard/keys)); everything else in the [Vercel AI SDK](https://sdk.vercel.ai/docs) stays the same.

```bash
npm install ai @ai-sdk/openai
export ANYROUTER_API_KEY="sk-ar-your-key-here"
```

```typescript
import { createOpenAI } from "@ai-sdk/openai"
import { generateText, streamText } from "ai"

const anyrouter = createOpenAI({
  baseURL: "https://anyrouter.dev/api/v1",
  apiKey: process.env.ANYROUTER_API_KEY,
})

const { text, response } = await generateText({
  model: anyrouter("anyrouter/free"),
  prompt: "Write a two-line poem about routers.",
})
console.log(response.modelId) // the model that served the request
console.log(text)

// Streaming
const result = streamText({
  model: anyrouter("anyrouter/free"),
  prompt: "Explain routing tables in two sentences.",
})
for await (const part of result.textStream) process.stdout.write(part)
```

Run it as an ES module (for example `npx tsx index.ts`). Requests appear in [Logs](https://anyrouter.dev/dashboard/logs).

`anyrouter("model-id")` uses the Responses API (`/api/v1/responses`). `anyrouter.chat("model-id")` uses Chat Completions.

## Models

- `anyrouter/free`: a healthy free model, no credits used. Needs the Go plan ($2/mo, or donate one working provider key). See [Free Tier](/features/free-tier).
- `anyrouter/auto`: AnyRouter picks from the full catalog.
- Any `provider/model` id from the [catalog](https://anyrouter.dev/models).

To use your own provider keys, see [BYOK](/features/byok).

## Troubleshooting

| Symptom | Fix |
|---|---|
| `401 Unauthorized` | `apiKey` must hold a key starting with `sk-ar-`. |
| `AI_APICallError: Invalid JSON response` | Update `ai` and `@ai-sdk/openai`. If it persists, use `anyrouter.chat("model-id")` and include the request id when you contact support. |
| `404 model not found` | Pass a full id such as `anyrouter/free` or `z-ai/glm-4.7-flash`. |


## Related

- [OpenAI SDK](/docs/guides/openai-sdk.md)
- [Streaming](/docs/guides/streaming.md)
- [LangChain](/docs/guides/langchain.md)
- [Chat Completions API](/docs/api-reference/chat-completions.md)
