AI Gateway OIDC Authentication
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.
Before you can use the OIDC token during local development, ensure that you link your application to a Vercel project:
terminalvercel linkPull the environment variables from Vercel to get the OIDC token:
terminalvercel env pullWith OIDC authentication, you can directly use the gateway provider without needing to obtain an API key or set it in an environment variable:
The AI SDK resolves OIDC automatically when
AI_GATEWAY_API_KEYisn't set. For the Python beta, install the Vercel integration withuv add "ai[vercel]". The HTTP client examples below readVERCEL_OIDC_TOKENfrom the environment. For long-running Node.js processes on Vercel, usegetVercelOidcToken()from@vercel/oidcat request time to obtain the current token.See the AI SDK OIDC reference for SDK configuration and usage.
oidc.tsimport { generateText } from 'ai'; const { text } = await generateText({ model: "anthropic/claude-sonnet-5", prompt: "Why is the sky blue?", }); console.log(text);oidc_ai.pyimport asyncio import ai async def main(): model = ai.get_model("anthropic/claude-sonnet-5") messages = [ai.user_message("Why is the sky blue?")] async with ai.stream(model, messages) as stream: async for event in stream: if isinstance(event, ai.events.TextDelta): print(event.chunk, end="", flush=True) print() asyncio.run(main())oidc-chat.tsimport OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.VERCEL_OIDC_TOKEN, baseURL: 'https://ai-gateway.vercel.sh/v1', }); const response = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5", messages: [{ "role": "user", "content": "Why is the sky blue?" }], }); console.log(response.choices[0]?.message.content);oidc_chat.pyimport os from openai import OpenAI client = OpenAI( api_key=os.environ["VERCEL_OIDC_TOKEN"], base_url="https://ai-gateway.vercel.sh/v1", ) response = client.chat.completions.create( model="anthropic/claude-sonnet-5", messages=[{"role": "user", "content": "Why is the sky blue?"}], ) print(response.choices[0].message.content)oidc-chat.shcurl --fail-with-body https://ai-gateway.vercel.sh/v1/chat/completions \ -H "Authorization: Bearer $VERCEL_OIDC_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "anthropic/claude-sonnet-5", "messages": [ { "role": "user", "content": "Why is the sky blue?" } ] }'oidc-messages.tsimport Anthropic from '@anthropic-ai/sdk'; const client = new Anthropic({ apiKey: process.env.VERCEL_OIDC_TOKEN, baseURL: 'https://ai-gateway.vercel.sh', }); const response = await client.messages.create({ model: "anthropic/claude-sonnet-5", messages: [{ "role": "user", "content": "Why is the sky blue?" }], max_tokens: 1024, }); for (const block of response.content) { if (block.type === 'text') console.log(block.text); }oidc_messages.pyimport os from anthropic import Anthropic client = Anthropic( api_key=os.environ["VERCEL_OIDC_TOKEN"], base_url="https://ai-gateway.vercel.sh", ) response = client.messages.create( model="anthropic/claude-sonnet-5", messages=[{"role": "user", "content": "Why is the sky blue?"}], max_tokens=1024, ) for block in response.content: if block.type == "text": print(block.text)oidc-messages.shcurl --fail-with-body https://ai-gateway.vercel.sh/v1/messages \ -H "Authorization: Bearer $VERCEL_OIDC_TOKEN" \ -H "Content-Type: application/json" \ -H "anthropic-version: 2023-06-01" \ -d '{ "model": "anthropic/claude-sonnet-5", "messages": [ { "role": "user", "content": "Why is the sky blue?" } ], "max_tokens": 1024 }'oidc-responses.tsimport OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.VERCEL_OIDC_TOKEN, baseURL: 'https://ai-gateway.vercel.sh/v1', }); const response = await client.responses.create({ model: "anthropic/claude-sonnet-5", input: "Why is the sky blue?", }); console.log(response.output_text);oidc_responses.pyimport os from openai import OpenAI client = OpenAI( api_key=os.environ["VERCEL_OIDC_TOKEN"], base_url="https://ai-gateway.vercel.sh/v1", ) response = client.responses.create( model="anthropic/claude-sonnet-5", input="Why is the sky blue?", ) print(response.output_text)oidc-responses.shcurl --fail-with-body https://ai-gateway.vercel.sh/v1/responses \ -H "Authorization: Bearer $VERCEL_OIDC_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "model": "anthropic/claude-sonnet-5", "input": "Why is the sky blue?" }'
Was this helpful?