GuideBasics

Quick Start

Generate an AnyRouter API key and make your first call: Python, JavaScript and curl examples for chat/completions, OpenAI SDK base_url setup, and streaming.

Generate an API key and make your first call

Calling the AnyRouter API Directly

Replace <AnyRouter_API_KEY> with your AnyRouter Key; mind the key's expiration date and credit limit.

For the list of available model values, see the model marketplace — just copy a model name and swap it in.

import requests
import json

response = requests.post(
    url="https://anyrouter.win/v1/chat/completions",
    headers={
        "Authorization": "Bearer <AnyRouter_API_KEY>",
        "Content-Type": "application/json",
    },
    data=json.dumps({
        "model": "gpt-4o-mini",  # Replace with your model id
        "messages": [
            {
                "role": "user",
                "content": "What is the meaning of life?"
            }
        ]
    })
)
// Run this under the https://anyrouter.win domain, otherwise the browser will block it with CORS errors
fetch("https://anyrouter.win/v1/chat/completions", {
  method: "POST",
  headers: {
    Authorization: "Bearer <AnyRouter_API_KEY>",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "gpt-4o-mini",
    messages: [
      {
        role: "user",
        content: "What is the meaning of life?",
      },
    ],
  }),
});
curl 'https://anyrouter.win/v1/chat/completions' \
  -H 'Authorization: Bearer <AnyRouter_API_KEY>' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {
        "role": "user",
        "content": "What is the meaning of life?"
      }
    ]
  }'

Streaming is supported — just add the parameter stream: true

Using the OpenAI SDK

Replace <AnyRouter_API_KEY> with your AnyRouter Key; mind the key's expiration date and credit limit. For the list of available model values, see the model marketplace — just copy a model name and swap it in.

from openai import OpenAI
import random

client = OpenAI(
    base_url="https://anyrouter.win/v1",
    api_key="<AnyRouter_API_KEY>",
)

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {
            "role": "developer",
            "content": "总是用中文回复"
        },
        {
            "role": "user",
            "content": "What is the meaning of life?"
        }
    ],
    temperature=0.8,
    max_tokens=1024,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
    seed=random.randint(1, 1000000000),
)

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

const openai = new OpenAI({
  baseURL: "https://anyrouter.win/v1",
  apiKey: "<AnyRouter_API_KEY>",
});

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

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

main();

For models that support search, append the parameter below to enable it:

  web_search_options={}, # Search options

Available models: gpt-4o-search-preview, gpt-4o-mini-search-preview.

Note that search models do not currently support fine-grained parameters such as temperature.

On this page