dev/notes

Spot a mistake? Highlight any text in a post and click Report — it goes straight to the author.

intermediate · AI · August 17, 2026 · 2 min read

How to Set Up OpenRouter as Your AI Provider

Set up OpenRouter as your AI provider: API keys, the OpenAI-compatible endpoint, and model selection for your apps and agents.

One endpoint, many models

OpenRouter is an OpenAI-compatible gateway to hundreds of models — Anthropic, OpenAI, and open-source models behind a single API key and base URL. Swap the model string and your code stays identical.

Step 1 — Create an API key

  1. Sign up at openrouter.ai and open Settings → Keys.
  2. Create a key and store it in your environment:
OPENROUTER_API_KEY="sk-or-v1-..."

Treat it like a password: in env vars, never in source control.

Step 2 — Use the OpenAI-compatible endpoint

Because OpenRouter speaks the OpenAI protocol, you point any OpenAI SDK at it:

export OPENAI_BASE_URL="https://openrouter.ai/api/v1"
export OPENAI_API_KEY="sk-or-v1-..."

Then standard SDK code works unchanged:

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://openrouter.ai/api/v1",
  apiKey: process.env.OPENROUTER_API_KEY,
});

const completion = await client.chat.completions.create({
  model: "openai/gpt-oss-20b:free",
  messages: [{ role: "user", content: "Explain Web Workers in one sentence." }],
});

console.log(completion.choices[0]?.message.content);

Step 3 — Choose a model

Model IDs are provider/model. List what’s available (and free) via:

curl https://openrouter.ai/api/v1/models

Common patterns:

  • openai/gpt-oss-20b:free — free tier
  • anthropic/claude-sonnet-4 — strong reasoning
  • meta-llama/llama-4-maverick — open weights

Pin a specific model in production; :free models can change availability.

Step 4 — Handle failures

LLM APIs are flaky and rate-limited. Wrap calls with retry + timeout:

const completion = await client.chat.completions.create(
  { model, messages },
  { timeout: 30_000, maxRetries: 3 }
);

Check the response for choices being empty (a filter refusal) before reading [0], so a rejected completion doesn’t throw a confusing error.

Troubleshooting

  • 401 Unauthorized — the key is wrong, revoked, or missing the Bearer prefix.
  • model not found — verify the full provider/model id; case matters.
  • Empty choices — the model refused or the content filter fired; inspect the full response object.

Summary

You now have one key and one endpoint that reach hundreds of models, with the same code working across providers. Next: wire it into an agent, or build an MCP server around your tools.

Related posts

Comments

One comment per thread every 30 minutes · edits are unlimited.