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(oranthropic). 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 / or / |
https:/ |
| Anthropic SDKs | https:/ (no /) |
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.
Recommended models
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_idis 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_ is set and passed as api_. If you pass nothing, the OpenAI SDK reads OPENAI_ instead. |
404 on every request |
OpenAI SDK without /, or Anthropic SDK with / |
OpenAI: .../. Anthropic: no /. |
404 naming the model |
Model ID typo | Copy it from the models page or GET /. |
403 naming the model |
Not on the key’s allow-list | Add it to the key. |
402 insufficient_ |
Balance too low for the request’s maximum price | Top up, or lower max_. |
402 budget_ |
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_ |
Responses API used with stored state | Send the whole conversation instead. |