Skip to main content
OpenRouter is wire-compatible with OpenAI’s REST API and official client SDKs (openai for TypeScript/JavaScript and Python). You can route model calls through OpenRouter by updating three settings: the base URL, the API key, and the model slug.

3-step quickstart

  1. Base URL: Point the client at https://openrouter.ai/api/v1.
  2. API Key: Supply an OpenRouter API key from the OPENROUTER_API_KEY environment variable.
  3. Model ID: Prefix the model slug with its author organization (e.g. openai/gpt-4o, anthropic/claude-3.5-sonnet, meta-llama/llama-3.3-70b-instruct).

TypeScript (npm i openai)

Python (pip install openai)

Important: the OPENAI_API_KEY fallback trap

When migrating an existing app from OpenAI to OpenRouter, watch out for client fallback behavior:
In both the TypeScript and Python openai SDKs, passing undefined or None to apiKey causes the constructor to read process.env.OPENAI_API_KEY from the environment. If your .env file still contains an old OpenAI API key, the SDK will send that OpenAI key (sk-proj-...) to openrouter.ai, and OpenRouter rejects it with 401 Unauthorized and the message Invalid API key: this is not an OpenRouter API key. See Troubleshooting 401 errors for the other 401 messages. Alternatively, if no key is found, the SDK throws "The OPENAI_API_KEY environment variable is missing", misleadingly naming OpenAI instead of OpenRouter. Best practice: Always validate OPENROUTER_API_KEY explicitly before instantiating the client:

Endpoint compatibility matrix

OpenRouter supports the majority of OpenAI SDK surfaces and methods:

Key differences from direct OpenAI API calls

While the wire protocol is identical, OpenRouter provides additional multi-provider capabilities and routing features:

1. Prefixed model identifiers

Model IDs must specify the provider or author prefix (e.g. openai/gpt-4o, anthropic/claude-3.5-sonnet, google/gemini-2.5-flash). Do not hardcode raw model names like gpt-4o without the openai/ namespace. Tip: Store the model slug in an environment variable (e.g. process.env.OPENROUTER_MODEL ?? 'openai/gpt-4o-mini') so your team can switch models or test alternatives without redeploying code.

2. Passing OpenRouter routing options and provider requirements

OpenRouter accepts optional routing configuration via the provider field. In the official Python SDK, pass custom fields using extra_body. In TypeScript, pass custom fields with // @ts-expect-error to preserve full strict type-checking on all standard parameters:
Setting provider: { require_parameters: true } ensures OpenRouter never routes requests with json_schema or tools to backend providers that don’t support those parameters: if no endpoint supports them, the request fails instead of falling back to one that would drop them. It doesn’t guarantee that the selected provider enforces strict: true exactly. Some providers translate the schema into their own format or treat it as a strong hint, so validate the parsed object against your schema before you use it. See Structured Outputs for details.

3. Streaming keepalive comments and mid-stream errors

When streaming completions from models that have a long time-to-first-token, OpenRouter sends periodic SSE comment lines to prevent intermediate proxies and cloud load balancers from timing out:
The official OpenAI SDKs parse and ignore SSE comment lines automatically. If you write custom raw SSE streaming parsers, ignore lines starting with a colon (:). If a backend provider experiences a hardware failure mid-stream after HTTP 200 OK headers have already been transmitted, OpenRouter emits an error chunk before terminating the stream:
Your streaming loop should verify whether incoming chunks contain an error key. See Errors and Debugging for full error schemas.

4. Zero Data Retention (ZDR)

If your application requires zero data retention, pass "zdr": true under provider to restrict routing to endpoints whose data policy does not retain prompts. Safety Retention endpoints, which may retain prompts and responses to prevent abuse, stay eligible if your privacy settings or guardrail allow Safety Retention; see Allowing or excluding Safety Retention to exclude them:
See the Zero Data Retention guide for eligible models and workspace-level defaults.

Automated codebase migration

To migrate an entire existing codebase (including multi-file projects, Vercel AI SDK apps, Mastra agents, or Anthropic SDK integrations) in a single step, hand the instructions in our cookbook recipe to your coding agent: 👉 Migrate to OpenRouter in One Prompt