Skip to content

Integrations / Python, Node and curl quick setup

Python, Node and curl quick setup

Call Yunshu Router with the official OpenAI or Anthropic SDK in Python and Node, or with curl. Base URLs, fallbacks, cost lookup and fixes.

Get an API key

Yunshu Router serves the OpenAI Chat Completions API, the OpenAI Responses API and the Anthropic Messages API. The official SDKs work by changing two things: the base URL and the key. We run the OpenAI and Anthropic Python SDKs against the gateway in our test suite on every change.

What you need

  • A Yunshu Router account with credit on the balance.
  • An API key from the console (API keys → Create key).
  • For Python: pip install openai (or anthropic). For Node: npm install openai (or @anthropic-ai/sdk).

Keep the key in an environment variable:

export YUNSHU_API_KEY="YOUR_YUNSHU_KEY"

Base URLs

Client Base URL
OpenAI SDKs, curl to /chat/completions or /responses https://api.yunshurouter.com/v1
Anthropic SDKs https://api.yunshurouter.com (no /v1)

Python (OpenAI SDK)

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.yunshurouter.com/v1",
    api_key=os.environ["YUNSHU_API_KEY"],
)

reply = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "Hello"}],
    extra_body={"models": ["deepseek-v4-pro", "glm-5.3"]},  # optional fallbacks
)
print(reply.choices[0].message.content)
print(reply.model)  # the model that answered

LangChain’s ChatOpenAI(model=..., base_url=..., api_key=...) works the same way.

Python (Anthropic SDK)

import os
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.yunshurouter.com",
    api_key=os.environ["YUNSHU_API_KEY"],
)

message = client.messages.create(
    model="kimi-k3",
    max_tokens=512,
    messages=[{"role": "user", "content": "Hello"}],
    extra_body={"fallbacks": [{"model": "deepseek-v4-pro"}]},  # optional
)
print(message.content[0].text)

The Anthropic SDK sends the key in x-api-key; we accept that and Authorization: Bearer.

Node (OpenAI SDK)

import OpenAI from "openai";

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

const reply = await client.chat.completions.create({
  model: "deepseek-v4-pro",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(reply.choices[0].message.content);

curl

curl https://api.yunshurouter.com/v1/chat/completions \
  -H "Authorization: Bearer $YUNSHU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "deepseek-v4-pro", "models": ["deepseek-v4-pro", "glm-5.3"],
       "messages": [{"role": "user", "content": "Hello"}]}'

Anthropic format:

curl https://api.yunshurouter.com/v1/messages \
  -H "x-api-key: $YUNSHU_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model": "kimi-k3", "max_tokens": 256,
       "messages": [{"role": "user", "content": "Hello"}]}'

Check it works

List the models your key can call:

curl https://api.yunshurouter.com/v1/models -H "Authorization: Bearer $YUNSHU_API_KEY"

Then send one of the requests above. Every reply carries response headers you can log:

Header What it tells you
x-yunshu-request-id The request id, for cost lookup and support
x-yunshu-model The model that served the request
x-yunshu-cache Whether the answer came from cache (cache hits are not charged)

Look up what a request cost

The cost is not in the response body. Look it up afterwards with the request id:

curl "https://api.yunshurouter.com/v1/generation?id=REQUEST_ID" \
  -H "Authorization: Bearer $YUNSHU_API_KEY"

For streamed responses, read the id from the x-yunshu-request-id header. Every request also appears in the console under Usage, and you can export usage as CSV.

Fallbacks and saved chains

  • OpenAI format: add "models": ["first", "second"]. If the first model is down, out of capacity or the prompt is too long for it, the next is tried.
  • Anthropic format: add "fallbacks": [{"model": "second"}].

You are billed only for the model that answered, and the response’s model field names it. A fallback chain counts as one request against your rate limit.

For code tasks: deepseek-v4-pro, glm-5.3, kimi-k3.

Set a budget on the key

Give each app its own key. In the console, open API keys and set a monthly budget, a rate limit and a model allow-list per key. A request that would pass the budget is refused before it is sent, with status 402 and code budget_exceeded.

Known limitations

  • We serve Chat Completions, Responses and Messages (with count_tokens). Embeddings, image and audio endpoints are not available.
  • The Responses API is stateless: previous_response_id is refused, and only function tools are passed on. Send the full conversation each turn.
  • The usage chunk in an OpenAI stream is sent only when you ask for it with stream_options: {"include_usage": true}.

Troubleshooting

What you see Likely cause Fix
401 Key missing or wrong Check YUNSHU_API_KEY is set and passed as api_key. If you pass nothing, the OpenAI SDK reads OPENAI_API_KEY instead.
404 on every request OpenAI SDK without /v1, or Anthropic SDK with /v1 OpenAI: .../v1. Anthropic: no /v1.
404 naming the model Model ID typo Copy it from the models page or GET /v1/models.
403 naming the model Not on the key’s allow-list Add it to the key.
402 insufficient_balance Balance too low for the request’s maximum price Top up, or lower max_tokens.
402 budget_exceeded The key’s monthly budget is used up Raise the budget.
429 Rate limit on the key or organisation Wait for the time in Retry-After.
Stream ends with an error event The upstream failed after the stream started Retry, or add fallbacks.
400 naming previous_response_id Responses API used with stored state Send the whole conversation instead.

Questions

Do I need a special SDK?

No. Use the official OpenAI or Anthropic SDK and change the base URL and key.

Can I call Claude models with the OpenAI SDK?

Not yet. Claude models can’t be called today. Every model we serve works on both APIs, so you can call kimi-k3 with the OpenAI SDK too.

Does streaming work?

Yes, with server-sent events relayed as they arrive. Every OpenAI stream ends with [DONE].

Are errors in the format my SDK expects?

Yes. The Anthropic endpoints answer with Anthropic error types; the OpenAI endpoints with the OpenAI error shape.