> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.fabricgateway.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.fabricgateway.ai/_mcp/server.

# Use the Vercel AI SDK with Fabric AI Gateway

> Point the Vercel AI SDK's OpenAI and Anthropic providers at a Fabric AI Gateway route and send your gateway key in the fg-api-key header.

You can send Vercel AI SDK traffic through Fabric AI Gateway by creating a provider instance with a gateway route as its base URL and your gateway key as a header. The Vercel AI SDK is a TypeScript library, so this topic has TypeScript examples only. It shows you how to configure the OpenAI and Anthropic providers and confirm that requests arrive.

## Prerequisites

Before you begin, make sure you have the following:

* The `ai` package, and `@ai-sdk/openai` or `@ai-sdk/anthropic`.
* A route for an OpenAI or Anthropic connection. For the steps, see [Connect an LLM provider](/configure/connections/llm-providers).
* A gateway key for the route. Send it in the `fg-api-key` header, which every route accepts, unless its Key Auth instance is an older one that sets a `headers` list. In that case, send the key in one of the listed headers, and ask your admin which. For more information, see [Authenticate and send a request](/use/authenticate).
* The base URL of the route. In the console, select **LLMs** > **Routes**, find the route, and then select **Copy LLM base URL**.

Keep the gateway key in an environment variable:

```bash
export FABRIC_GATEWAY_KEY="GATEWAY_KEY"
```

## Configure the OpenAI provider

Create an OpenAI provider with the route's base URL followed by `/v1`:

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

const fabric = createOpenAI({
  baseURL: "BASE_URL/v1",
  apiKey: "unused",
  headers: { "fg-api-key": process.env.FABRIC_GATEWAY_KEY! },
});

const { text } = await generateText({
  model: fabric.chat("gpt-4o-mini"),
  prompt: "Hello from Fabric Gateway!",
});
console.log(text);
```

`fabric.chat()` uses the Chat Completions endpoint, `/v1/chat/completions`.

## Configure the Anthropic provider

The Anthropic provider's base URL includes `/v1`, so use the route's base URL followed by `/v1`:

```typescript
import { createAnthropic } from "@ai-sdk/anthropic";
import { generateText } from "ai";

const fabric = createAnthropic({
  baseURL: "BASE_URL/v1",
  apiKey: "unused",
  headers: { "fg-api-key": process.env.FABRIC_GATEWAY_KEY! },
});

const { text } = await generateText({
  model: fabric("claude-sonnet-4-5"),
  prompt: "Hello world!",
});
console.log(text);
```

In both examples, replace `BASE_URL` with the URL that you copied. Use a model that the connection allows.

The providers require an API key, but you don't need a real provider key. When it forwards the request, the gateway adds the key saved on the connection in place of the provider's key header, so any placeholder value works.

> **Warning**
>
> Treat the gateway key as a secret. Don't commit it to source control.

## Confirm that requests pass through the gateway

To confirm that the SDK uses the gateway, do the following:

1. Run your code. A successful request prints the model's reply.
2. In the Fabric console, select **Traces**. To see traces from every namespace, select **Admin**, and then select **Traces**.
3. Find the request on the route.

If the request returns `401 Unauthorized`, check that the `fg-api-key` header carries a key for this route. For more information, see [Troubleshoot authentication errors](/configure/authentication/key-auth#troubleshoot-authentication-errors).

## Next steps

* To use the OpenAI SDK directly, see [Use the OpenAI SDK with Fabric AI Gateway](/guides/sdks/openai).
* To use the Anthropic SDK directly, see [Use the Anthropic SDK with Fabric AI Gateway](/guides/sdks/anthropic).
* To review token usage by model, see [Review analytics](/use/monitor/analytics).