Authentication
To use the AI Gateway, you need to authenticate your requests. There are two authentication methods available:
- API Key Authentication: Create and manage API keys through the Vercel Dashboard
- OIDC Token Authentication: Use Vercel's automatically generated OIDC tokens
API keys provide a secure way to authenticate your requests to the AI Gateway. You can create and manage multiple API keys through the Vercel Dashboard.
From the Vercel dashboard, click the AI Gateway tab to access the AI Gateway settings.
Click API keys on the left sidebar to view and manage your API keys.
Click Create key and proceed with Create key from the dialog to generate a new API key.
Once you have the API key, save it to
.env.local
at the root of your project (or in your preferred environment file):.env.localAI_GATEWAY_API_KEY=your_api_key_here
When you specify a model id as a plain string, the AI SDK will automatically use the Vercel AI Gateway provider to route the request. The AI Gateway provider looks for the API key in the AI_GATEWAY_API_KEY
environment variable by default.
import { generateText } from 'ai';
export async function GET() {
const result = await generateText({
model: 'xai/grok-3',
prompt: 'Why is the sky blue?',
});
return Response.json(result);
}
The Vercel OIDC token is a way to authenticate your requests to the AI Gateway without needing to manage an API key. Vercel automatically generates the OIDC token that it associates with your Vercel project.
Vercel OIDC tokens are only valid for 12 hours, so you will need to refresh
them periodically during local development. You can do this by running vercel env pull
again.
Before you can use the OIDC token during local development, ensure that you link your application to a Vercel project:
terminalvercel link
Pull the environment variables from Vercel to get the OIDC token:
terminalvercel env pull
With OIDC authentication, you can directly use the gateway provider without needing to obtain an API key or set it in an environment variable:
app/api/chat/route.tsimport { generateText } from 'ai'; export async function GET() { const result = await generateText({ model: 'xai/grok-3', prompt: 'Why is the sky blue?', }); return Response.json(result); }
Was this helpful?