页面简介
API Authentication
正文摘录
You can cover model costs with OpenRouter API keys. Our API authenticates requests using Bearer tokens. This allows you to use curl or the OpenAI SDK directly with OpenRouter. API keys on OpenRouter are more powerful than keys used directly for model APIs.They allow users to set credit limits for apps, and they can be used in OAuth flows. Accepted credentials Send one credential per request in the Authorization header as Bearer <credential>. For clients built on the Anthropic SDK, you can also send an API key (not an access token) in the x-api-key header. OpenRouter accepts the following credential types: CredentialFormatHow you get itAPI keysk-or-v1- followed by 64 lowercase hexadecimal characters, for example sk-or-v1-0123…cdef (73 characters in total).Create one on the API keys page, or receive one through OAuth PKCE. Management keys use the same format.OpenRouter access tokenA signed JWT (three base64url segments separated by .) with typ: at+jwt, issuer https://openrouter.ai, and an inference or management scope. Signed with ES256; public keys are at https://openrouter.ai/api/v1/oauth/jwks. Valid for at most 15 minutes.Exchange a JWT from your own identity provider with workload identity federation.OpenRouter sign-in (OIDC) access tokenA signed JWT issued by https://clerk.openrouter.ai that carries the inference scope.Sign in to OpenRouter through an OIDC client, for example Claude Desktop with OpenRouter sign-in. Send the credential exactly as issued, with no extra whitespace or quotes. OpenRouter treats a bearer that starts with sk-or-v1- as an API key. It treats a JWT as an access token only when its issuer is https://openrouter.ai or https://clerk.openrouter.ai, and rejects any other bearer. OpenRouter also rejects an expired JWT and a JWT without the scope the endpoint requires. Some endpoints accept only an API key, so an access token with the inference scope doesn’t replace an API key everywhere. To use a token from your own identity provider, exchange it for an OpenRouter access token first; OpenRouter doesn’t accept third-party JWTs directly. Troubleshooting 401 errors A rejected credential returns 401 Unauthorized. The error message tells you what OpenRouter received: MessageCauseFixInvalid API key: this is not an OpenRouter API keyThe Authorization or x-api-key value is a key from another provider: it starts with sk- but not sk-or-. OpenAI and Anthropic SDKs fall back to OPENAI_API_KEY or ANTHROPIC_API_KEY when you don’t pass a key explicitly.Pass your OpenRouter key explicitly, for example apiKey: process.env.OPENROUTER_API_KEY, and check that OPENROUTER_API_KEY is set in the process that makes the request.No API key in the Authorization headerThe header is Bearer followed by nothing, undefined, or null. This usually means the environment variable holding your key is unset.Set the variable, then restart the shell or app that reads it.Missing Authentication headerThe Authorization header is malformed, for example it has extra spaces, or its value isn’t a recognized credential.Send exactly Authorization: Bearer <OPENROUTER_API_KEY>, with one space and no quotes.Malformed authentication credentialThe key contains characters that can’t be sent in an HTTP header, often invisible characters picked up during copy-paste.Copy the key again from the API keys page.API key expiredThe key passed its expiration date.Create a new key.Invalid API key (with nothing after it) or User not found.The key has the OpenRouter format but was deleted, disabled, or never existed.Check the key on the API keys page, or create a new one. A request with no credentials at all (no Authorization header, no x-api-key header, and no session cookie) returns No cookie auth credentials found on endpoints that also accept a signed-in session, such as /api/v1/chat/completions. Other endpoints may return Missing Authentication header instead. Using an API key To use an API key, first create your key. Give it a name and choose a credit limit (or no limit). Once a key has used its credit limit, requests with it are rejected until you raise the limit on the key’s page, or, for a daily, weekly, or monthly limit, until the limit resets. Keys created for you during onboarding get a $100 credit limit and expire after 180 days. We recommend setting a credit limit on every key. A key with no limit lets a leaked key or a runaway agent spend your entire balance, including any auto top-ups. If you need a key without a limit, keep it out of client-side code and rotate it promptly if you suspect it has been exposed. If you’re calling the OpenRouter API directly, set the Authorization header to a Bearer token with your API key. If you’re using the OpenAI Typescript SDK, set the api_base to https://openrouter.ai/api/v1 and the apiKey to your API key. TypeScript SDKPython (OpenAI SDK)TypeScript (OpenAI SDK)TypeScript (Raw API)cURLGoimport { OpenRouter } from '@openrouter/sdk'; const openRouter = new OpenRouter({ apiKey: '<OPENROUTER_API_KEY>', httpReferer: '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openrouter.ai. appTitle: '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openrouter.ai. }); const response = await openRouter.responses.send({ responsesRequest: { model: 'openai/gpt-5.2', input: 'Say this is a test', stream: false, }, }); if (!('output' in response)) { throw new Error('Expected a non-streaming response'); } console.log([...response.output].reverse().find((item) => item.type === 'message')); Output{ content: [ { annotations: [], logprobs: [], text: "This is a test.", type: "output_text", } ], id: "msg_tmp_j086jcdp5p", role: "assistant", status: "completed", type: "message", } To stream responses, see Streaming. If your key has been exposed You must protect your API keys and never commit them to public repositories. OpenRouter is a GitHub secret scanning partner, and has other methods to detect exposed keys. If we determine that your key has been compromised, you will receive an email notification. If you receive such a notification or suspect your key has been exposed, immediately visit your key settings page to delete the compromised key and create a new one. Using environment variables and keeping keys out of your codebase is strongly recommended.Was this page helpful?YesNo