Make a request
Send a chat completion request, swap models by changing the model slug, and keep your API key out of your code.
You’re building a support assistant for a small online shop. The first thing it needs is one working request: send a customer’s question to a model and get an answer back.
Most chat inference requests use POST https://openrouter.ai/api/v1/chat/completions (OpenRouter also supports the OpenAI Responses API, Anthropic’s Messages API, and modality endpoints like embeddings, image generation, and audio). For that endpoint, the body names a model and a list of messages: the conversation so far, where each message has a role (such as user) and its content. Pass your API key in the Authorization header and treat it like a password: anyone with the key can spend your credits.
Load OPENROUTER_API_KEY from a .env file or secret manager so the key never ends up in your code or shell history.
# Prompt for the key silently so it isn't saved in shell history:
read -s OPENROUTER_API_KEY && export OPENROUTER_API_KEY
curl https://openrouter.ai/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d '{
"model": "openrouter/free",
"messages": [
{ "role": "user", "content": "Do you ship to Canada?" }
]
}'const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openrouter/free',
messages: [{ role: 'user', content: 'Do you ship to Canada?' }],
}),
});
console.log(await response.json());import os
import requests
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers={"Authorization": f"Bearer {os.environ['OPENROUTER_API_KEY']}"},
json={
"model": "openrouter/free",
"messages": [{"role": "user", "content": "Do you ship to Canada?"}],
},
)
print(response.json())Check your understanding
You tested your request with openrouter/free and now want to switch to google/gemini-3.8-flash. What do you need to change in your request?
These examples use openrouter/free, the Free Models Router, which picks an available free model so you can test the request without spending credits. When you’re ready for a specific model, swap in any slug from the model catalog. Paid models require credits on your account, so check the model’s pricing before calling it in a loop.
Apply it
You find your key in a commit that was pushed to a public repository an hour ago. You delete the commit. Are you done?
No. The key was public for an hour, and deleting the commit doesn’t take it back from anyone who copied it. Delete the key on the keys page, create a new one, and update your environment. Then check your activity for requests you didn’t make.
Setting a credit limit on each key caps how much a leaked key can spend. The authentication docs cover limits and what to do when a key is exposed.
Takeaway
A request is a model slug, a list of messages, and your API key in the Authorization header. Load the key from an environment variable or secret manager so it stays out of your code.