翻篇收录说明
翻篇网页快照

AI Gateway OIDC Authentication

https://vercel.com/docs/ai-gateway/authentication-and-byok/oidc

最近抓取:2026-12-04 02:59:39(北京时间)

最近内容变化:2026-12-04 02:59:39(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Authenticate AI Gateway requests from Vercel deployments with OIDC tokens. Configure the AI SDK or send bearer tokens directly to the API.

正文摘录

Copy pageCopy pageAI Gateway OIDC AuthenticationCopy pageCopy pageThe 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. How OIDC works on Vercel deployments Vercel passes your project's OIDC token to every Vercel Function invocation in the x-vercel-oidc-token request header. AI Gateway accepts that token as a bearer credential in place of an API key. How your code gets the token depends on the client you use. With the AI SDK The AI SDK finds and sends the OIDC token for you. Deployed code doesn't need an API key, an environment variable, or vercel env pull. The AI SDK picks a credential for each request in this order: The apiKey option passed to createGateway(). The AI_GATEWAY_API_KEY environment variable. The Vercel OIDC token, which the AI SDK obtains for each request and refreshes for you. A non-empty API key always takes priority over OIDC, even if the key is invalid. To use OIDC on a deployment, remove AI_GATEWAY_API_KEY from the project's environment variables. If no API key is set and no OIDC token is available, requests fail with a GatewayAuthenticationError. With the OpenAI SDK, Anthropic SDK, or HTTP requests Chat Completions, Messages API, and Responses clients don't discover the OIDC token. Pass it yourself, as the client's apiKey or as a bearer token in the Authorization header. These clients have no precedence order or fallback, so AI Gateway authenticates with whatever credential you pass. Nothing refreshes the token for these clients. Vercel reuses a Function's token for less than an hour before rotating it, so get the current token inside your request handler on every request, not once at startup. In Node.js, call getVercelOidcToken() from @vercel/oidc. On Vercel, it reads the current request's x-vercel-oidc-token header. Locally, it reads the VERCEL_OIDC_TOKEN value that vercel env pull writes: app/api/chat/route.tsimport OpenAI from 'openai'; import { getVercelOidcToken } from '@vercel/oidc'; export async function POST(request: Request) { const client = new OpenAI({ apiKey: await getVercelOidcToken(), baseURL: 'https://ai-gateway.vercel.sh/v1', }); const { messages } = await request.json(); const response = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5", messages, }); return Response.json(response); } In Python, read the token from the x-vercel-oidc-token request header in a deployed function, or from VERCEL_OIDC_TOKEN in local development. 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. Setting up OIDC authentication Link to a Vercel projectBefore you can use the OIDC token during local development, ensure that you link your application to a Vercel project:terminalvercel linkPull environment variablesPull the environment variables from Vercel to get the OIDC token:terminalvercel env pullUse OIDC authentication in your codeWith 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_KEY isn't set. For the Python beta, install the Vercel integration with uv add "ai[vercel]". The TypeScript examples for Chat Completions, Messages API, and Responses are request handlers that call getVercelOidcToken() on every request, which works on Vercel deployments and locally. The Python and cURL examples read VERCEL_OIDC_TOKEN, which vercel env pull writes for local development. In a deployed function, use the x-vercel-oidc-token request header instead.AI SDKChat CompletionsMessages APIResponses / OpenResponsesTypeScriptPython (beta)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())TypeScriptPythoncURLoidc-chat.tsimport OpenAI from 'openai'; import { getVercelOidcToken } from '@vercel/oidc'; export async function POST() { const client = new OpenAI({ apiKey: await getVercelOidcToken(), 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?" }], }); return Response.json({ text: 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?" } ] }'TypeScriptPythoncURLoidc-messages.tsimport Anthropic from '@anthropic-ai/sdk'; import { getVercelOidcToken } from '@vercel/oidc'; export async function POST() { const client = new Anthropic({ apiKey: await getVercelOidcToken(), 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, }); const text = response.content .map((block) => (block.type === 'text' ? block.text : '')) .join(''); return Response.json({ 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 }'TypeScriptPythoncURLoidc-responses.tsimport OpenAI from 'openai'; import { getVercelOidcToken } from '@vercel/oidc'; export async function POST() { const client = new OpenAI({ apiKey: await getVercelOidcToken(), baseURL: 'https://ai-gateway.vercel.sh/v1', }); const response = await client.responses.create({ model: "anthropic/claude-sonnet-5", input: "Why is the sky blue?", }); return Response.json({ text: 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?" }'Last updated November 8, 2026PreviousAPI KeysNextBYOKWas this helpful?