Your first request from code

Two values change, and that's all. By the end you'll have a real answer and seen its trace in the panel.

8 minLesson 2 of 80 of 3 steps

If you've used OpenAI's API, you know nearly all of this already. IranRouter speaks the same API, so just two values change: the address and the key.

Text
base_url: https://iranrouter.com/v1
api_key:  ir-...

Any SDK, library or tool that works with OpenAI reaches every model with those two values.

With curl

The fastest test, since nothing needs installing:

Shell
curl https://iranrouter.com/v1/chat/completions \
  -H "Authorization: Bearer ir-..." \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek/deepseek-v4-flash",
    "messages": [{"role": "user", "content": "Hello! Introduce yourself in one sentence."}],
    "max_tokens": 100
  }'

In the response, the text is in choices[0].message.content. The usage part has the token counts and cost_rial, the final charge for this request.

With Python

Python
from openai import OpenAI

client = OpenAI(
    api_key="ir-...",
    base_url="https://iranrouter.com/v1",
)

resp = client.chat.completions.create(
    model="deepseek/deepseek-v4-flash",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=100,
)
print(resp.choices[0].message.content)

With Node.js

Node.js
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.IRANROUTER_KEY,
  baseURL: "https://iranrouter.com/v1",
});

const resp = await client.chat.completions.create({
  model: "deepseek/deepseek-v4-flash",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(resp.choices[0].message.content);
Always write the full model id: maker, slash, name. Some short names work too, but for real code the full id is more stable.

Send a real request0 of 3 steps

  1. Run one of the three samples above with your key.
  2. If you get a 401, look for a stray space or newline in the key.
  3. If you get a 402, top up the wallet or pick a free model.

Make sure it went through IranRouter

Go to Usage & logs. That request should be there with the model, tokens and cost. If it isn't, it didn't pass through here; most likely base_url didn't change.

You can also ask for the balance without opening the panel:

Shell
curl https://iranrouter.com/v1/credits -H "Authorization: Bearer ir-..."

If your tool only reads environment variables

Most tools know these two variables, so sometimes you touch no code at all:

Shell
export OPENAI_BASE_URL="https://iranrouter.com/v1"
export OPENAI_API_KEY="ir-..."
Some older libraries look for OPENAI_API_BASE instead of OPENAI_BASE_URL. If it won't connect, set both.

What you learned

  • Only the address and key change.
  • usage.cost_rial is the charge for that request.
  • Next: choosing a model by the numbers.
Choosing a model with your eyes openThe dearest model isn't always the right one; sometimes you pay ten times as much for the same job.Done, next lesson