API Quick Start

This guide sends a basic request to the OpenAI-compatible Chat Completions endpoint.

Prerequisites

Set environment variables for the examples:

export GETROUTER_API_KEY="YOUR_API_KEY"
export GETROUTER_MODEL="MODEL_ID"

cURL

curl https://getrouter.ai/v1/chat/completions \
  -H "Authorization: Bearer $GETROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'"$GETROUTER_MODEL"'",
    "messages": [
      {"role": "user", "content": "Write one sentence about reliable APIs."}
    ]
  }'

The generated text is returned in:

choices[0].message.content

Python

Install the OpenAI SDK:

pip install openai
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["GETROUTER_API_KEY"],
    base_url="https://getrouter.ai/v1",
)

response = client.chat.completions.create(
    model=os.environ["GETROUTER_MODEL"],
    messages=[
        {"role": "user", "content": "Write one sentence about reliable APIs."}
    ],
)

print(response.choices[0].message.content)

Node.js / TypeScript

Install the OpenAI SDK:

bun add openai
import OpenAI from 'openai'

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

const response = await client.chat.completions.create({
  model: process.env.GETROUTER_MODEL!,
  messages: [
    { role: 'user', content: 'Write one sentence about reliable APIs.' },
  ],
})

console.log(response.choices[0]?.message.content)

Common first-request errors

ErrorCheck
401API key is complete, active, and sent as Bearer YOUR_API_KEY
404Base URL is not duplicated and the client is calling /chat/completions
Model not foundUse the exact model ID from Models
Insufficient balance or quotaReview your account balance and API key usage limit
Unsupported operationChoose a model compatible with Chat Completions

Next steps