OpenRouter 是 LLM gateway,一個金鑰可呼叫數百種 model (OpenAI / Anthropic / Google / Meta / Mistral 等),model 名格式為 vendor/model。
POST /tw/api/openrouter/v1/chat/completionsChat Completions (相容 OpenAI 格式)。
curl https://www.myai168.com/tw/api/openrouter/v1/chat/completions \
-H "Authorization: Bearer YOUR_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"anthropic/claude-3.5-sonnet","messages":[{"role":"user","content":"Hello"}]}'
import requests
r = requests.post(
"https://www.myai168.com/tw/api/openrouter/v1/chat/completions",
headers={"Authorization": "Bearer YOUR_DEV_KEY"},
json={"model": "anthropic/claude-3.5-sonnet", "messages": [{"role": "user", "content": "Hello"}]},
)
print(r.json())
POST /tw/api/openrouter/v1/chat/responses注意:此端點不是 OpenAI Responses API——請求與回應皆為 Chat Completions 格式(欄位同上方 completions),與 completions 的唯一差異是送出前會先檢查點數餘額,點數不足直接回 402、不轉發上游。要用 OpenAI Responses API 格式請改用 /tw/api/openai/v1/responses。
curl https://www.myai168.com/tw/api/openrouter/v1/chat/responses \
-H "Authorization: Bearer YOUR_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"openai/gpt-4o","messages":[{"role":"user","content":"Hello"}]}'
import requests
r = requests.post(
"https://www.myai168.com/tw/api/openrouter/v1/chat/responses",
headers={"Authorization": "Bearer YOUR_DEV_KEY"},
json={"model": "openai/gpt-4o", "messages": [{"role": "user", "content": "Hello"}]},
)
print(r.json())
POST /tw/api/openrouter/v1/audio/speech文字轉語音 (透過 OpenRouter 路由到 TTS 供應商)。
curl https://www.myai168.com/tw/api/openrouter/v1/audio/speech \
-H "Authorization: Bearer YOUR_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"openai/tts-1","input":"Hello","voice":"nova"}' \
--output speech.mp3
import requests
r = requests.post(
"https://www.myai168.com/tw/api/openrouter/v1/audio/speech",
headers={"Authorization": "Bearer YOUR_DEV_KEY"},
json={"model": "openai/tts-1", "input": "Hello", "voice": "nova"},
)
with open("speech.mp3", "wb") as f:
f.write(r.content)
POST /tw/api/openrouter/v1/audio/transcriptions語音轉文字 (multipart upload)。
curl https://www.myai168.com/tw/api/openrouter/v1/audio/transcriptions \
-H "Authorization: Bearer YOUR_DEV_KEY" \
-F file=@audio.mp3 \
-F model=gpt-4o-transcribe
import requests
with open("audio.mp3", "rb") as f:
r = requests.post(
"https://www.myai168.com/tw/api/openrouter/v1/audio/transcriptions",
headers={"Authorization": "Bearer YOUR_DEV_KEY"},
files={"file": f},
data={"model": "gpt-4o-transcribe"},
)
print(r.json())
POST /tw/api/openrouter/v1/video/generations影片生成 (依 OpenRouter 支援的 video model)。
curl https://www.myai168.com/tw/api/openrouter/v1/video/generations \
-H "Authorization: Bearer YOUR_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"vendor/some-video-model","prompt":"A cat dancing"}'
import requests
r = requests.post(
"https://www.myai168.com/tw/api/openrouter/v1/video/generations",
headers={"Authorization": "Bearer YOUR_DEV_KEY"},
json={"model": "vendor/some-video-model", "prompt": "A cat dancing"},
)
print(r.json())
兩端點的請求本體都是 Chat Completions 格式 JSON(chat/responses 只是多了點數預檢,不是 OpenAI Responses API)。認證帶 Authorization: Bearer YOUR_API_KEY 標頭(金鑰不可放在網址查詢參數)。
| 參數 | 型別 | 必填 | 說明 |
|---|---|---|---|
model | string | 必填 | OpenRouter 模型代號,格式 vendor/model(例 anthropic/claude-3.5-sonnet)。 |
messages | array | 必填 | 對話訊息陣列,每筆 {"role": "system" | "user" | "assistant", "content": "..."}。 |
stream | boolean | 選填 | true 時以 SSE 串流回應。 |
| 其餘官方參數 | — | 選填 | 原樣透傳 OpenRouter。 |
與 OpenAI Chat Completions 相容:文字在 choices[0].message.content(串流增量在 choices[0].delta.content)。計費依 OpenRouter 回報的實際費用換算點數;餘額可隨時以 GET /tw/api/user/get_credits 查詢。
| HTTP 狀態 | 意義 |
|---|---|
401 | 金鑰缺漏或無效(同 IP 連續失敗會被暫時封鎖)。 |
402 | 點數不足(chat/responses/音訊/影片端點會在送出前先擋下)。 |
400 | 請求本體格式錯誤,或音訊端點指定了無計價資訊的模型("No pricing info! Please use other models!")。 |
502 | 上游供應商錯誤——一律不扣點。 |
任何錯誤(含供應商未回用量/費用資訊)一律不扣點。