---
title: "Vercel AI SDK"
description: "The Vercel AI SDK (TypeScript) calls pirouter through createOpenAI from @ai-sdk/openai, with baseURL set to https://api.pirouter.ai/v1, and works with generateText and streamText. This page covers installing, configuring and verifying the SDK, and what to do about common errors."
canonical: "https://pirouter.ai/docs/integrations/vercel-ai-sdk"
last-updated: "2026-10-17"
lang: "en"
source: "content/docs/en/integrations/vercel-ai-sdk.mdx"
---

# Vercel AI SDK

The Vercel AI SDK (TypeScript) calls pirouter through createOpenAI from @ai-sdk/openai, with baseURL set to https://api.pirouter.ai/v1, and works with generateText and streamText. This page covers installing, configuring and verifying the SDK, and what to do about common errors.

The Vercel AI SDK calls pirouter through its OpenAI provider (`@ai-sdk/openai`): create a provider that points at pirouter with `createOpenAI`, then use `generateText` and `streamText` as usual.

## Prerequisites

- A pirouter API key. If you do not have one, create it with the [quickstart](https://pirouter.ai/docs/quickstart.md) and store it in an environment variable:

```bash
export PIROUTER_API_KEY="sk-pi-..."
```

- Node.js 22 or later.
- The SDK and its OpenAI provider:

```bash
pnpm add ai @ai-sdk/openai zod
```

The samples use top-level `await`: save the file as `.mjs`, or set `"type": "module"` in `package.json`.

## Configure

Pass two settings to `createOpenAI`: `baseURL` is `https://api.pirouter.ai/v1`, and `apiKey` is your pirouter key. The provider sends the key in the `Authorization: Bearer` header, which is where the gateway reads it.

```ts
import { createOpenAI } from '@ai-sdk/openai'
import { generateText } from 'ai'

const pirouter = createOpenAI({
  baseURL: 'https://api.pirouter.ai/v1',
  apiKey: process.env.PIROUTER_API_KEY,
})

const { text } = await generateText({
  model: pirouter.chat('openai/gpt-6-sol'),
  prompt: 'Hello!',
})
console.log(text)
```

The sample picks the model with `pirouter.chat(...)`, which sends the request to Chat Completions. A bare `pirouter(...)` sends it to Responses instead, the provider's default. The gateway serves both endpoints: `POST /v1/chat/completions` and `POST /v1/responses`.

Write the full `vendor/model` ID. The models you can use are the same as with the OpenAI SDK: see the OpenAI column of the protocol table in [Models and protocols](https://pirouter.ai/docs/models.md#which-protocols-each-model-accepts).

### Stream the response

Use `streamText` and read the text from `textStream` as it arrives:

```ts
import { createOpenAI } from '@ai-sdk/openai'
import { streamText } from 'ai'

const pirouter = createOpenAI({
  baseURL: 'https://api.pirouter.ai/v1',
  apiKey: process.env.PIROUTER_API_KEY,
})

const result = streamText({
  model: pirouter.chat('openai/gpt-6-sol'),
  prompt: 'Hello!',
})
for await (const text of result.textStream) {
  process.stdout.write(text)
}
console.log()
```

You should see the reply printed piece by piece.

## Verify

Save the `generateText` sample above as `main.mjs`, then run it:

```bash
node main.mjs
```

You should see a one-line reply from the model.

Open **Logs** in the console. A few seconds after the request, it appears in the list with `/v1/chat/completions` under **Endpoint**. The **Trace ID** in the log details is the same value as the response's `X-Pi-Trace-Id` header.

## Troubleshooting

### 401 invalid\_api\_key

A `401` with the error code `invalid_api_key` and the `message` "You didn't provide an API key. Provide it in an Authorization header using Bearer auth (Authorization: Bearer YOUR\_KEY)." means the request carried no key, or carried something other than a pirouter key starting with `sk-pi-`. Check that `PIROUTER_API_KEY` is set in the terminal that runs the code. Without `apiKey`, the provider falls back to `OPENAI_API_KEY`, which usually holds an OpenAI key.

### 400 model is required

A `400` with the error code `invalid_request` and the `message` `model is required` usually means `baseURL` is `https://api.pirouter.ai`, without `/v1`. The request path is then not an endpoint the gateway serves, and the gateway cannot find a model in it. Set `baseURL` to `https://api.pirouter.ai/v1`.

### 422 validation\_failed

A `422` with the error code `validation_failed` and a `message` starting "This model is not served on the endpoint you called." means the model is not served on the endpoint you called, for example an image model. The rest of the `message` lists the endpoints it is served on; pick a model that generates text over the OpenAI protocol.

## Not verified yet

- Multi-turn conversations through `pirouter(...)` (the Responses endpoint) have not been verified. For a conversation, use `pirouter.chat(...)` and send the full message history on every turn.
- Pointing the Anthropic provider (`@ai-sdk/anthropic`) or the Google provider (`@ai-sdk/google`) at pirouter has not been verified, so this page gives no setup for them. For Claude or Gemini models, use the OpenAI provider above, or the [Anthropic SDK](https://pirouter.ai/docs/integrations/anthropic-sdk.md) or [Google GenAI SDK](https://pirouter.ai/docs/integrations/google-genai-sdk.md).
