Create an AI Gateway API key and save it in a .env file:
AI Gateway

Install TanStack AI and its AI Gateway adapter in your project:
Ecosystem

Use an existing JavaScript or TypeScript project with Node.js 22 or later and a Vercel account.
TanStack AI
Connect TanStack AI to AI Gateway using the @tanstack/ai-vercel-gateway adapter. You can switch models by changing the model ID in your application.
import { vercelGatewayText } from'@tanstack/ai-vercel-gateway';
{
messages: [
conststream=chat({
forawait (constchunkof stream) {
TypeScript

adapter:vercelGatewayText('anthropic/claude-opus-5'),
},
],
role:'user',
console.log(chunk);
import { chat } from'@tanstack/ai';
content:'Write a one-sentence bedtime story about a unicorn.',
Run the TypeScript example with tsx, loading your API key from .env:
pnpm dlxtsx--env-file=.envindex.mts
For JavaScript, run node --env-file=.env index.mjs. Your terminal displays stream events containing the generated story.
Cross-link map: TanStack AI with AI Gateway (/docs/ai-gateway/ecosystem/framework-integrations/tanstack-ai)From the Vercel docs graph (built 2026-09-21T05:26:59.511Z), spanning vercel.com docs + KB, nextjs.org, ai-sdk.dev, and other Vercel documentation sites. Full graph as JSON: https://vercel.com/docs/graph.jsonSemantically closest pagesUsing TanStack AI with Vercel AI Gateway — Connect TanStack AI to Vercel AI Gateway with the @tanstack/ai-vercel-gateway adapter to stream chat, route across proviTanStack AI with MCP — Connect TanStack AI to a Model Context Protocol server on Vercel and call its tools with models served through AI GatewaTanStack AI — Connect a TanStack AI app to OAuth-protected MCP servers with Vercel Connect, handle user consent, and manage MCP clientTanStack AI with Vercel Sandbox — Run TanStack AI coding agents in isolated Vercel Sandbox microVMs with the dedicated provider, workspace configuration,AI Gateway Embeddings — Generate vector embeddings for semantic search, similarity matching, and retrieval-augmented generation \(RAG\) throughPrerequisitesEcosystem — Use Vercel Sandbox with the agent frameworks, model SDKs, and coding agents you already work with.This page links to (11)Account Management — Learn how to manage your Vercel account and team members.Vercel AI Gateway: Models, Routing, and Observability — Call AI models from any infrastructure through a managed gateway. Centralize credentials, request logs, spend budgets, rAI Gateway API Keys — Create, view, and delete AI Gateway API keys, and set each key's budget and spend attribution, from the dashboard, CLI,AI Gateway OIDC Authentication — Authenticate AI Gateway requests from Vercel deployments with OIDC tokens. Configure the AI SDK or send bearer tokens diAI Gateway Model Fallbacks — Configure AI Gateway model fallbacks to try backup models when the primary model is unavailable. Set fallback order andAI Gateway Provider Filtering, Ordering, and Sorting — Control AI Gateway provider routing with order, only, and sort. Set preferences, restrict providers, and rank them by coOpenAI Chat Completions API with AI Gateway — Use OpenAI SDKs with the AI Gateway Chat Completions API. Configure the base URL and authentication for chat, streaming,OpenAI Responses API with AI Gateway — Use the OpenAI Responses API with AI Gateway to generate text, call tools, stream tokens, and more across any supportedTanStack AI — Connect a TanStack AI app to OAuth-protected MCP servers with Vercel Connect, handle user consent, and manage MCP clientTanStack AI with MCP — Connect TanStack AI to a Model Context Protocol server on Vercel and call its tools with models served through AI GatewaUsing TanStack AI with Vercel AI Gateway — Connect TanStack AI to Vercel AI Gateway with the @tanstack/ai-vercel-gateway adapter to stream chat, route across proviPages that link here (10)By site: chat-sdk (1) · vercel-kb (1) · vercel-docs (8)From chat-sdkTanStack AI — Feed thread history into TanStack AI's chat() and give it Chat SDK tools, with no runtime dependency on @tanstack/ai.From vercel-kbVercel AI SDK vs TanStack AI — Compare the Vercel AI SDK and TanStack AI for building AI-powered TypeScript applications and agents. Learn how AI SDK aFrom vercel-docsAI Gateway Ecosystem and Integrations — Connect frameworks, coding tools, and billing integrations to AI Gateway. Configure app attribution and explore integratAI Gateway Framework Integrations — Connect LangChain, LiteLLM, LlamaIndex, Mastra, Pydantic AI, TanStack AI, and other frameworks to Vercel AI Gateway withAI Gateway Embeddings — Generate vector embeddings for semantic search, similarity matching, and retrieval-augmented generation \(RAG\) throughAI Gateway Image Generation — Generate and edit images using AI models through Vercel AI Gateway with support for multiple providers and modalities.TanStack AI — Connect a TanStack AI app to OAuth-protected MCP servers with Vercel Connect, handle user consent, and manage MCP clientTanStack Start on Vercel — Deploy TanStack Start applications to Vercel with Nitro and configure Vercel Functions.TanStack AI with MCP — Connect TanStack AI to a Model Context Protocol server on Vercel and call its tools with models served through AI GatewaTanStack AI with Vercel Sandbox — Run TanStack AI coding agents in isolated Vercel Sandbox microVMs with the dedicated provider, workspace configuration,
Was this helpful?
The adapter uses the OpenAI Responses API by default. To use Chat Completions, pass { api: 'chat' } as the second argument to vercelGatewayText.