How to use an OpenRouter API key
Published 2026-10-03 · Updated 2026-10-25
OpenRouter is the fastest way to try a model that lives behind a provider you don’t have an account with. One key, one base URL, every model in the catalog. Here’s how to put a key to work.
The pieces you need
- A key. OpenRouter keys start with
sk-or-v1-followed by 64 hex characters. - The base URL:
https://openrouter.ai/api/v1 - A model id in the form
vendor/model, likeanthropic/claude-sonnet-5orz-ai/glm-5.1.
Set the environment variable
export OPENROUTER_API_KEY="sk-or-v1-..."
In a .env file for a Node or Python project, same line without the export.
Call it with curl
curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-6.1-mini",
"messages": [{"role": "user", "content": "Say hello"}]
}'
A 200 response means the key is good. A 401 means the key is dead; a 402 means it’s out of credit.
With the OpenAI SDK
OpenRouter speaks the OpenAI API, so the standard SDK works, you only change the base URL.
from openai import OpenAI
import os
client = OpenAI(
api_key=os.environ["OPENROUTER_API_KEY"],
base_url="https://openrouter.ai/api/v1",
)
resp = client.chat.completions.create(
model="openai/gpt-6.1-mini",
messages=[{"role": "user", "content": "Say hello"}],
)
print(resp.choices[0].message.content)
The same trick works in Node (new OpenAI({ baseURL: ... })), in LangChain, and in coding tools like Cursor or Cline that accept an “OpenAI-compatible” provider.
Useful extras
GET /api/v1/auth/keyreturns the key’s credit balance and limits, handy for checking a key without spending a token.GET /api/v1/modelslists every model with pricing, in OpenAI’s format.- You can send
HTTP-RefererandX-Titleheaders so OpenRouter’s leaderboard attributes your app.
When a key fails
Shared keys burn through quota fast. If you get 402s, grab another key, that’s the whole point of keeping a list. If everything returns 401, the key was revoked; move on rather than retrying.