OpenRouter API Key: Create a Key and Send Your First Request

Create an OpenRouter API key, set a spending limit, and send your first request with curl or the OpenAI Python SDK. See the documented reply and error fixes.

Published

OpenRouter API Key: Create a Key and Send Your First Request

You can make your first OpenRouter API call with 0 purchased credits. Create an OpenRouter API key, keep it in your terminal, and send “Hello!” to openrouter/free. The reply tells you which model answered, so you can check the whole connection before choosing a paid model.

Create an OpenRouter API key

Start with Get API key on OpenRouter’s home page. Think of the key as a backstage pass for your application. Whoever holds it can send requests in your name. Give a local experiment its own pass so you can retire it without interrupting an app people use.

OpenRouter’s public home page with the Get API key entry point
OpenRouter
  1. Create your account

    Select Sign up from the sign-in screen. On the email registration form, enter your email address and a password, review the terms, then select Continue and finish the prompts. If you already have an account, sign in with it.

  2. Name and limit the key

    Open your workspace’s API keys page and start a new key. Give it a name you will recognize later, such as local-demo. Set its credit limit to the most you would let this experiment spend. A limit is a ceiling on the key’s use of your balance. Setting it adds no funds.

  3. Copy the secret while it is on screen

    Copy the full value before closing the dialog. Save it in your password manager. For a local call, put it in an environment variable that your terminal can pass to curl or Python.

In Bash, run these lines in the terminal you’ll use for the request. Paste the key at the prompt and press Enter. The hidden prompt keeps the value off the screen and out of the command you save in shell history.

Bash
read -rsp 'OpenRouter API key: ' OPENROUTER_API_KEY
printf '\n'
export OPENROUTER_API_KEY

For a web app, put the same variable in your server’s secret settings. The browser sends the user’s message to your server, which authenticates the user, checks that they can make the request, and adds the key on the call to OpenRouter. A key bundled into browser JavaScript is visible to anyone who downloads it, even if your build tool originally read it from an environment variable.

The browser hands a message to the server, where a private pass is added before it goes to OpenRouter.
The message can originate in the browser. The credential joins it on the server.

Call the OpenRouter API endpoint with curl

Use the Responses API for this example. The address identifies the service, the authorization header carries your key, and the JSON body names the model and supplies your message. This is the short request in OpenRouter’s free-router guide, with the response headers displayed so you can see the HTTP status.

Bash
curl -i https://openrouter.ai/api/v1/responses \
  -H "Authorization: Bearer ${OPENROUTER_API_KEY:?Set the key in this shell first}" \
  -H 'Content-Type: application/json' \
  -d '{"model":"openrouter/free","input":"Hello!"}'

What comes back? OpenRouter’s published example reply begins “Hello! I'm here and ready to help!” and reports xingchen-agi/xing4.0-29b-a4b:free as the model used. That is an excerpt from the documentation, checked December 5, 2026. Your reply and model can differ.

In the raw JSON, look for status equal to completed. In output, find the assistant’s message, then the content part with type output_text. Its text is the answer. The top-level model field identifies the model that served the request. An HTTP success status alone tells you less than those fields do.

Set the OpenRouter API base URL in the OpenAI SDK

The OpenAI Python SDK is a client library. Its destination is configurable, so it can send the same message to OpenRouter using the key you just created. Install the library in your Python environment with python -m pip install openai. Run this script from the shell where you exported the key.

Python
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=os.environ["OPENROUTER_API_KEY"],
)
reply = client.responses.create(model="openrouter/free", input="Hello!")
if reply.status != "completed" or not reply.output_text:
    raise RuntimeError(f"No completed text reply: {reply.status}, {reply.error}")
print(reply.output_text)
print("Model used:", reply.model)

The OpenRouter authentication guide shows this client setup. Give the SDK the base URL ending in /api/v1. Its responses.create method adds /responses for you. If curl works and Python fails, check those two constructor arguments first. A Python KeyError on OPENROUTER_API_KEY means the script stopped locally before it sent anything.

If your intended destination is OpenAI’s API, use the direct OpenAI key setup. For the role the router plays between your application and a provider, see how OpenRouter routes requests.

Which OpenRouter free models can I call without credits?

The model you request determines whether you need credits. On a new account with no purchases, use openrouter/free for a connection check. It picks an available free model that supports the features you requested. If you need repeatable behavior from a particular model, copy its exact :free ID from the catalog while that variant is available. See which OpenRouter free models you can call.

One API key leads to a free-model path or a paid-model path with a credit drawer.
The same application key can take either path. Paid calls on shared capacity draw on your credit balance.

For an account with no credit purchases, OpenRouter lists 20 requests per minute and 50 per day for free variants under its standard limits. Creating another key does nothing to increase that allowance. A negative account balance can block even a free call. The limits reference describes both cases.

Use a paid model on OpenRouter’s shared capacity when you need that model specifically and your available credits cover the request. For the billing details, see how OpenRouter credits work. Keep a separate key for production so changing a demo’s budget leaves the production credential alone.

Bringing your own provider key (BYOK). If you already pay a provider directly, add its key in your workspace’s integration settings. Your app still sends its OpenRouter key. For calls using the provider key, the provider account supplies capacity and governs its own costs and limits. Check key priority and shared-capacity fallback before assuming every call will use that account. The BYOK guide explains the fallback choices and any OpenRouter charges.

Diagnose the first failed call

Read the response’s error message before replacing a key. An authentication failure calls for a different fix from a model outage. OpenRouter documents these statuses in its error reference.

HTTP statusWhat to check first
400A required parameter is missing or invalid. Check the JSON, especially the model ID and input.
401OpenRouter could not authenticate the request. Check the destination, the Bearer header and whether the key is active.
402Read error.metadata.limit_source, when present. Check the account balance, the key’s remaining credit limit, or the budget reserved by requests already in flight.
403Read the block reason. A permission rule, moderation filter or spending budget can reject an otherwise valid credential.
408The request timed out. Try the short greeting again before troubleshooting a longer workload.
429The platform or provider rate-limited the request. Honor Retry-After when supplied and check your free-model allowance.
502The chosen model is down or returned an invalid response. Try again later or select a different available model.
503No available provider meets the request’s routing requirements. Check provider restrictions and honor Retry-After if supplied.

To inspect the key without generating text, use the same terminal for this read-only request. Look at limit_remaining for the key’s cap and free_model_daily_requests for the daily allowance. A successful lookup narrows the problem to what the generation request is asking OpenRouter to do.

Bash
curl -sS https://openrouter.ai/api/v1/key \
  -H "Authorization: Bearer ${OPENROUTER_API_KEY:?Set the key in this shell first}"

Rotate a key or provision one for each app

For a planned rotation, create the replacement, update your server secret, restart or redeploy the process that reads it, and confirm the replacement works before deleting the old key. If a key has leaked, delete it immediately, then create and deploy its replacement. Removing a secret from a repository leaves any copies people already have usable until you revoke it.

For a single app, the Keys page is enough. If you need to issue and retire application keys automatically, OpenRouter calls the provisioning credential a management API key. Create it from Management API Keys, choose its scope and permissions, and keep it in the administrative service. That service can create an application key with POST /api/v1/keys. Give the application the resulting ordinary key. Management keys handle administration and cannot call completion endpoints. Management key documentation.

Do I need a separate free OpenRouter API key?

Use the ordinary key from your workspace. To keep a demo free, set the model explicitly to openrouter/free or to an available free variant. When you later add credits, the same credential can make a paid call within its spending limit.

Once you have a working call, the next decision is who should operate the routing layer. Compare LiteLLM and OpenRouter if you are considering running your own gateway, or explore alternatives for multi-model routing if you need different provider controls.

Get the next builder guide by email.

Published
Category
Build
Related Articles
Newsletter

One letter, every Sunday.Working systems, not hot takes.

Weekly. No spam. Unsubscribe anytime.