EN ▾
Get API key

OpenRouter API Key Not Working: Checks and Fallback

If your openrouter api key not working error persists, the HTTP status code points to the cause: 401 is the key or URL, 402 is credit, 429 is speed. Work through the checks below, then add a second endpoint so one bad key stops being an outage.

Updated

A key stuck in a lock beside an open second doorway, representing an API key error and a fallback endpoint
Glowing key on a pedestal before a row of gates, representing a small trial credit instead of a free tier

No card needed. After sign-up, claim $0.50 of free test credit with one click (once per person).

Get API key

Key points

Start with the status code

When a key problem shows up, the HTTP status code tells you which layer is failing. Do not change anything until you have read it. Log the status, the response body and any request ID the response includes.

StatusUsual meaningFirst thing to try
401The key is missing, malformed, revoked or sent to the wrong URLRe-copy the key and check the header
402No credit left on the accountCheck the balance and top up
403The key is valid but not allowed to do thisCheck key restrictions and the model you requested
404Wrong path or model nameCheck the base URL and the model ID
429Too many requests, or the upstream is busySlow down and retry with a delay
5xxA server or upstream problemRetry later, then use a fallback

These are the common meanings of the codes in OpenAI-style APIs. For the exact wording of a provider's messages, read that provider's own documentation, since details differ.

Fixing a 401 step by step

A 401 means the server did not accept who you are. Work through the causes from the cheapest to check to the most annoying.

  1. Hidden characters. A trailing space or newline pasted with the key is the single most common cause. Print the key length in your code and compare it to what the dashboard shows.
  2. Wrong header. The format is Authorization: Bearer YOUR_KEY. Some HTTP clients need the word Bearer typed in full and with one space.
  3. Another key winning. The OpenAI SDK reads OPENAI_API_KEY from the environment if you do not pass a key. An old value there can silently override what you think you set. Pass api_key explicitly while you debug.
  4. Wrong base URL. A key from one service sent to another service's URL is rejected. Check that the base URL and the key belong together.
  5. Key was revoked or deleted. If you rotated keys recently, an old deployment may still hold the dead one. Check your secret store and any CI variables.

A tiny test removes your application from the question. Run it with curl outside your project, and replace the placeholders with your own values:

curl -i "$PRIMARY_BASE_URL/models" \
  -H "Authorization: Bearer $PRIMARY_KEY"

If this works and your app does not, the key is fine and the bug is in your code or configuration. If this fails too, the key or URL is the problem.

A 402 or a 429 is not a broken key

Both errors are often reported as a "key not working", but the key is accepted. A 402 means the account has no usable credit for the model you asked for. Check the account balance and, if you use paid models, add credit. If you were using a model with a free tier, remember that free usage is limited, so a project that outgrows it will start failing without any change on your side. The article on free tier limits goes through what to expect.

A 429 means you are going too fast, or the model's upstream host is busy. Retrying immediately makes it worse. Use a growing delay, cap the number of retries and add a small random jitter, so that many workers do not all retry in the same second.

import random, time

def with_backoff(call, retries=4):
    for i in range(retries):
        try:
            return call()
        except Exception as e:
            status = getattr(e, "status_code", None)
            if status not in (429, 500, 502, 503) or i == retries - 1:
                raise
            time.sleep(2 ** i + random.random())

Check the integration code, not only the key

Many "key" problems are really configuration problems. Look at these before you open a support ticket.

If you set the service up through an app that has its own settings screen, such as a chat front end, confirm which field holds the key and which holds the base URL. People often paste the key into the URL field. Our guide to OpenRouter API integration shows how the pieces fit together for a typical OpenAI-compatible client.

Add a second endpoint so a bad key stops being an outage

Keys expire, balances run out and providers have bad hours. If your product depends on one of them, add a second OpenAI-compatible endpoint as a fallback. This site offers one: the base URL is https://api.openrouteruncensored.com/v1 and the model ID is uncensored. It is a separate service with a separate key, run by an independent provider, with a single open-weight model tuned to answer without content refusals for lawful adult use. It is not a router and not the official API of any model vendor.

import os
from openai import OpenAI

backup = OpenAI(
    base_url="https://api.openrouteruncensored.com/v1",
    api_key=os.environ["BACKUP_KEY"],
)

def chat_backup(messages):
    r = backup.chat.completions.create(model="uncensored", messages=messages)
    return r.choices[0].message.content

In Node the same setup looks like this:

import OpenAI from "openai";

const backup = new OpenAI({
  baseURL: "https://api.openrouteruncensored.com/v1",
  apiKey: process.env.BACKUP_KEY,
});

const r = await backup.chat.completions.create({
  model: "uncensored",
  messages: [{ role: "user", content: "ping" }],
});
console.log(r.choices[0].message.content);

Only switch to the backup after your retries are exhausted, and log every switch so you notice when it happens. Remember the backup is one model, so output style will not match whatever model your primary route uses.

How this backup behaves under load

Know its limits before you rely on it. A full key allows 8 concurrent requests and 300 requests per minute. A trial key allows 2 and 60. Over a limit you get HTTP 429, with the error code rate_limited for requests per minute or concurrency for too many at once. Temporary overload also returns 429, with Retry-After: 5. A streaming request holds its concurrency slot until the stream finishes.

The context window is 100,000 tokens for prompt plus completion, the maximum body is 8 MB, and responses include X-Request-Id and X-Balance-USD so you can see the balance after each call. The service is text only. For parameters and error codes, see the docs.

Price is $0.15 per 1M input tokens and $0.50 per 1M output tokens, prepaid, with no subscription and no expiry. A fallback that handles 2,000 requests of 2,000 input and 500 output tokens during an outage costs 4M x $0.15 = $0.60 for input plus 1M x $0.50 = $0.50 for output, so $1.10. See the pricing page for top-ups.

Get a fallback key before you need it

Create the backup account while everything works. Sign up with Google, or with an email and a password, then open the key page and claim the $0.50 trial credit. It takes one click, needs no card, is valid for 7 days and is limited to one per person. That is enough to run your failover code once against a real endpoint and confirm it works. Trial keys are limited to 2 concurrent requests, so do not use one as a production fallback; top up the account instead.

Questions and answers

Why do I get 401 with a key I just created?

Usually a pasted trailing space, a missing Bearer prefix, or another key in the OPENAI_API_KEY environment variable overriding yours. Pass the key explicitly and test with curl.

What does a 402 error mean?

The account has no usable credit for the request. Check the balance and add credit, or switch to a model that your current balance covers.

Should I retry on 429?

Yes, after a delay that grows with each attempt, and only a few times. For this API, a 429 with Retry-After: 5 means wait five seconds.

Is the backup endpoint the same model?

No. It is a single open-weight model with the ID uncensored, so output style can differ from the model your primary route uses.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Get API key