How to use OpenRouter: API keys through GPT, Claude, and Gemini

 ·  ~12 min read  ·  AI Development

OpenRouter API 与 GPT、Claude、Gemini 调用示意

Кратко: OpenRouter часто называют «одним ключом на все модели», но на старте обычно спотыкаются о регистрацию, баланс, id моделей и правильный base URL as “one key for every model,” yet first-time setup usually burns time on signup, credits, model ids, and the right API base URL. This walkthrough follows that order: account and API key, then GPT via the OpenAI-compatible API, Claude, Gemini, and finally billing plus common errors.

1
OpenAI-compatible base URL
3
Families: GPT, Claude, Gemini
$0
Signup is free

What OpenRouter is for

OpenRouter is an API gateway that aggregates multiple model providers. You send one request; OpenRouter routes it from the model field to OpenAI, Anthropic, Google, and others. Spend lands on your OpenRouter balance instead of separate vendor consoles.

For individuals and small teams the wins are usually: one key to compare models, published per-token prices, and routing fallbacks (see current docs for behavior). OpenRouter does not host weights—inference still runs at the upstream provider.

If you wire Claude Code through OpenRouter, that is a different Anthropic-shaped base URL. This article starts with generic HTTP / OpenAI SDK, then notes Claude-specific paths.

Sign up, credits, and keys

  1. Register at openrouter.ai.
  2. Add credits under Settings. Trial balance may exist; set a monthly cap before unattended scripts run.
  3. Create keys on the Keys page—split by use case (local-dev, ci-bot) and attach spending limits per key.
  4. Store the key in the environment: export OPENROUTER_API_KEY="sk-or-…"—never commit it.

Finding and reading model ids

Ids look like provider/model, e.g. openai/gpt-5.6-sol-medium, anthropic/claude-sonnet-5.5, google/gemini-4-argon-medium. Browse the site or list via API:

curl -s https://openrouter.ai/api/v1/models \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" | head -c 2000

Pin concrete ids in production; latest aliases can change price or context overnight. For this week’s usage and rates see OpenRouter rankings.

Calling GPT (OpenAI-compatible)

curl https://openrouter.ai/api/v1/chat/completions \
  -H "Authorization: Bearer $OPENROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -H "HTTP-Referer: https://your-app.example" \
  -H "X-Title: My Dev App" \
  -d '{"model":"openai/gpt-5.6-sol-medium","messages":[{"role":"user","content":"Explain OpenRouter in three sentences"}]}'

Calling Claude

Use the same chat endpoint with anthropic/claude-sonnet-5.5 (or another Claude id). Native Anthropic clients need base https://openrouter.ai/api without a trailing /v1—details in the Claude Code + OpenRouter guide.

Calling Gemini

Use google/gemini-4-argon-medium (or the id shown on the model page). Vision and tools depend on that model’s card—do not assume every Gemini id is multimodal.

Switching base_url in code

from openai import OpenAI
import os
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"])
print(client.chat.completions.create(
    model="openai/gpt-5.6-sol-medium",
    messages=[{"role": "user", "content": "Hello"}],
).choices[0].message.content)

Spend limits and Activity

Billing is per catalog token price. See token pricing basics. Use Activity to see per-key spend and set hard caps on experimental keys.

FAQ

Does OpenRouter merge with direct OpenAI bills? No—balances are separate.

401 errors? Check Bearer token, revoked keys, and positive credits; confirm /api/v1 path.

404 model? Id typo or retired model—refresh from the Models list.

ZavCloud Developer Infrastructure

After token spend, keep the build machine steady

Long agent sessions and Xcode builds share the same Mac memory.

Dedicated Mac nodes are a separate monthly line item from tokens.

Configure your dedicated Mac node
New Arrival View M4 Plans