OpenRouter API key setup: one key, many models, a spending cap

Create an OpenRouter API key with a credit limit, call it with the OpenAI SDK from your server, and check what the key has left before users find out.

Simon, Founder of Shipyard

By · Founder of Shipyard

3 minute read

Why use OpenRouter at all

OpenRouter puts models from many providers behind one API and one balance. You add credits once, create a key, and switch between models by changing a name in your settings instead of opening accounts with every lab. For a new product that is still deciding which model is good enough and cheap enough, that flexibility is the main reason to start here.

The API is compatible with OpenAI's, so the code you would write for OpenAI mostly works unchanged. You point the client at OpenRouter's address and give it an OpenRouter key. The rest of this page is about doing that safely: the key on the server, a cap on what it can spend, and a check that tells you when the cap is close.

Create the key with a credit limit

Keys are created on the keys page at openrouter.ai/keys. OpenRouter asks for a name and a credit limit, and the limit can be left empty for no cap. Do not leave it empty. A per-key credit limit is an optional spending cap on that one key, and it is the simplest protection you have: if the key leaks or your app gets stuck in a loop, the damage stops at the number you chose.

Give each product its own key and its own limit, and name the key after the product. When something misbehaves you can tell which product it is and switch off one key without touching the others. OpenRouter's documentation describes its keys as more powerful than a plain model key precisely because they carry these limits.

Call it from the server with the OpenAI SDK

Install openai and create a client with OpenRouter's base address, https://openrouter.ai/api/v1, and the key from the environment. Store the key on your host as OPENROUTER_API_KEY. Unlike the OpenAI SDK's own default, this client will not find the key by itself under that name, so pass it explicitly.

Model names on OpenRouter include the provider, for example a name that starts with the lab's name, a slash and then the model. Read the model from a variable you set on your host, OPENROUTER_MODEL in the example, so trying a cheaper model is a settings change. Keep the route behind a signed-in user and a rate limit, exactly as you would with a direct provider key.

TypeScript
// app/api/ask/route.ts
import OpenAI from 'openai'

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

export async function POST(req: Request) {
  // Check the session and a per-user rate limit here.
  const { question } = await req.json()
  const completion = await client.chat.completions.create({
    model: process.env.OPENROUTER_MODEL!, // set on your host
    messages: [{ role: 'user', content: question }],
    max_tokens: 800,
  })
  return Response.json({ text: completion.choices[0]?.message?.content ?? '' })
}

Check what the key has left

OpenRouter has an endpoint that describes the key you call it with: GET https://openrouter.ai/api/v1/key. The answer includes the key's limit, how much of it remains and when it resets. That makes it the right thing for a health check, and the right thing to alert on before the cap is reached rather than after users start seeing errors.

When a limit is hit, requests fail with a payment-required error, and OpenRouter's limits page explains how to tell which limit was the cause: the account balance, the key's own cap, or a budget for requests still in flight. Handle that error in your app with a clear message instead of a generic failure, and raise the cap or add credit deliberately.

Terminal
curl https://openrouter.ai/api/v1/key \
  -H "Authorization: Bearer $OPENROUTER_API_KEY"

Two keys you should not mix up

OpenRouter also has management keys, which can create, change and delete other keys through the API. They are for scripts that hand out keys automatically, not for calling models. Your app needs an ordinary key with a credit limit; a management key in a web server is a much bigger risk than it needs to be. If you never automate key creation, you never need one.

What Shipyard does here

When you choose OpenRouter as the AI provider, a launch writes your key into the host as OPENROUTER_API_KEY, on the server side only, and the build prompt tells your coding agent to call models from a server route. Creating the key and choosing its credit limit stay with you on OpenRouter's site, since that is the number that decides what a leak could cost.

Sources

Launch in an hour, not a weekend

Shipyard connects your accounts once, then sets up the domain, hosting, database, email and payments for every new product.

Get started See how it works

Read next