Skip to content

Unify API

Giữ OpenAI SDK. Dùng mọi model.

Giữ nguyên code OpenAI SDK. Đổi base URL và key, mọi model trả lời trên cùng một dạng request.

POST /api/v1/chat/completions
diff --git a/app.py b/app.pyimport osfrom openai import OpenAIclient = OpenAI(removed:     api_key=os.environ["OPENAI_API_KEY"],added:     base_url="https://anyrouter.dev/api/v1",added:     api_key=os.environ["ANYROUTER_API_KEY"],)resp = client.chat.completions.create(removed:     model="gpt-5-mini",added:     model="anyrouter/free",    messages=[{"role": "user", "content": "Hi"}],)print(resp.choices[0].message.content)
Truy cập API bắt đầu từ gói Go ($2/tháng, hoặc miễn phí khi bạn đóng góp một key vào Pool). anyrouter/free miễn phí trên Go; anyrouter/auto dùng credit.

Kết quả trả về

Tab cURL ở trên, chạy trên prod ngày 2026-10-31 với key gói Go. Chỉ giữ các field hiển thị, giá trị dài được cắt bằng "…".

captured from prod
HTTP 200
x-anyrouter-model: poolside/laguna-s-2.1
x-anyrouter-provider-chain: hagiang
x-anyrouter-credential-source: platform
x-request-id: req_7dd5cca42d4443309b205080

{
  "object": "chat.completion",
  "model": "poolside/laguna-s-2.1",
  "choices": [
    {
      "finish_reason": "stop",
      "message": {
        "content": "Hello! How can I help you today?",
        "reasoning": null,
        "refusal": null,
        "role": "assistant"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 45,
    "completion_tokens": 10,
    "cost": 0
  }
}
x-anyrouter-model
Model đã trả lời. Preset như anyrouter/free ghi model nó chọn ở đây; chỉ định model id thì header lặp lại id của bạn.
x-anyrouter-provider-chain
Các upstream đã thử, theo thứ tự. Ở đây chỉ một chặng; khi một chặng lỗi, chặng sau trả lời và header thành a->b.
x-anyrouter-credential-source
Key upstream nào đã trả: platform (của chúng tôi), own_byok (của bạn) hoặc pool (key được đóng góp).
x-request-id
Gửi cho bộ phận hỗ trợ, hoặc tìm request trong log của bạn.
usage.cost
Chi phí của request này, tính bằng USD, nằm trong body. Bằng 0 với model miễn phí.

Tùy chọn: điều hướng route

Không mục nào bắt buộc. Mỗi mục là một field hoặc header trên cùng request, chạy thật trên prod.

  • Chọn model id bất kỳ

    Dùng owner/model trong danh mục thay cho preset.

    "model": "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning"
    → HTTP 200, provider-chain: hagiang
  • Chỉ định provider

    Một header, phân tách bằng dấu phẩy theo thứ tự ưu tiên.

    X-AnyRouter-Provider: hagiang,kilo
    "model": "poolside/laguna-s-2.1"
    → HTTP 200, provider-chain: hagiang
  • Sắp thứ tự provider trong body

    Giống object provider của OpenRouter.

    "model": "nvidia/nemotron-3-ultra-550b-a55b"
    "provider": {"order": ["hagiang", "kilo"]}
    → HTTP 200, provider-chain: hagiang

Hỗ trợ

  • Streaming (SSE)
  • Tools / gọi hàm
  • Đầu ra có cấu trúc (response_format)
  • Mức độ suy luận (reasoning)
  • Nhận ảnh với model hỗ trợ vision

Model Decisions

Không phải chat. Gửi một tình huống và câu hỏi có các lựa chọn cố định, nhận lại xác suất cho từng lựa chọn. Decisions miễn phí từ gói Go trở lên, gói Free có 5 request dùng thử.

POST /api/v1/decisions
bash
curl https://anyrouter.dev/api/v1/decisions \
  -H "Authorization: Bearer $ANYROUTER_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "hal-x/thx-01", "state": "My card was charged twice",
    "questions": {"team": {"type": "choice", "criteria": {"billing": "Payments", "tech": "API problems"}}}}'

# {"answers": {"team": {"type": "choice", "choice": "billing", "probabilities": {"billing": 0.97, "tech": 0.03}}}, "model": "hal-x/thx-01", "status": "completed"}

Tài liệu và use case liên quan

Tiếp theo