OROpenRoute AI Docs

Quick Start

OpenRoute provides a unified API that gives you access to hundreds of AI models through a single endpoint, while automatically handling fallbacks and selecting the most cost-effective options. Get started with just a few lines of code using your preferred SDK or framework.

Looking for information about free models and rate limits? Please see the FAQ

In the examples below, the OpenRoute-specific headers are optional. Setting them allows your app to appear on the OpenRoute leaderboards. For detailed information about app attribution, see our App Attribution guide.

Using the OpenAI SDK

Python
from openai import OpenAI

client = OpenAI(
  base_url="https://api.openroute.cn/v1",
  api_key="<OpenRoute_API_KEY>",
)

completion = client.chat.completions.create(
  extra_headers={
    "HTTP-Referer": "<YOUR_SITE_URL>", # Optional. Site URL for rankings on openroute.cn.
    "X-Title": "<YOUR_SITE_NAME>", # Optional. Site title for rankings on openroute.cn.
  },
  model="openai/gpt-4o",
  messages=[
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
)

print(completion.choices[0].message.content)
TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: 'https://api.openroute.cn/v1',
  apiKey: '<OpenRoute_API_KEY>',
  defaultHeaders: {
    'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openroute.cn.
    'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openroute.cn.
  },
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: 'openai/gpt-4o',
    messages: [
      {
        role: 'user',
        content: 'What is the meaning of life?',
      },
    ],
  });

  console.log(completion.choices[0].message);
}

main();

Using the OpenRoute API directly

You can use the interactive Request Builder to generate OpenRoute API requests in the language of your choice.

Python
import requests
import json

response = requests.post(
  url="https://api.openroute.cn/v1/chat/completions",
  headers={
    "Authorization": "Bearer <OpenRoute_API_KEY>",
    "HTTP-Referer": "<YOUR_SITE_URL>", # Optional. Site URL for rankings on openroute.cn.
    "X-Title": "<YOUR_SITE_NAME>", # Optional. Site title for rankings on openroute.cn.
  },
  data=json.dumps({
    "model": "openai/gpt-4o", # Optional
    "messages": [
      {
        "role": "user",
        "content": "What is the meaning of life?"
      }
    ]
  })
)
TypeScript
fetch('https://api.openroute.cn/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer <OpenRoute_API_KEY>',
    'HTTP-Referer': '<YOUR_SITE_URL>', // Optional. Site URL for rankings on openroute.cn.
    'X-Title': '<YOUR_SITE_NAME>', // Optional. Site title for rankings on openroute.cn.
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'openai/gpt-4o',
    messages: [
      {
        role: 'user',
        content: 'What is the meaning of life?',
      },
    ],
  }),
});
Shell
curl https://api.openroute.cn/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OpenRoute_API_KEY" \
  -d '{
  "model": "openai/gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ]
}'

The API also supports streaming.

Using third-party SDKs

For information about using third-party SDKs and frameworks with OpenRoute, please see our frameworks documentation.