Skip to content
On this page

curl 接入

curl 不需要安装 SDK,适合第一次验证 CoreRouter 配置,也适合排查客户端问题。

基础调用

bash
export COREROUTER_API_KEY="sk-xxxxxxxxxxxxxxxx"

curl https://api.corerouter.tech/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $COREROUTER_API_KEY" \
  -d '{
    "model": "claude-sonnet-4-5",
    "messages": [
      {
        "role": "user",
        "content": "你好,请介绍一下你自己。"
      }
    ]
  }'

流式输出

bash
curl https://api.corerouter.tech/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $COREROUTER_API_KEY" \
  -d '{
    "model": "claude-sonnet-4-5",
    "messages": [
      {"role": "user", "content": "用三句话介绍 CoreRouter。"}
    ],
    "stream": true
  }'

多轮对话

bash
curl https://api.corerouter.tech/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $COREROUTER_API_KEY" \
  -d '{
    "model": "claude-sonnet-4-5",
    "messages": [
      {"role": "user", "content": "你好"},
      {"role": "assistant", "content": "你好!有什么可以帮助你的吗?"},
      {"role": "user", "content": "请写一个 Python hello 函数。"}
    ]
  }'

查询模型列表

bash
curl https://api.corerouter.tech/v1/models \
  -H "Authorization: Bearer $COREROUTER_API_KEY"

Responses API

Responses API 适合 Codex、Agent 和部分需要统一事件流的应用。它要求所选 Model ID 支持 Responses,普通聊天模型不一定可用。

bash
curl https://api.corerouter.tech/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $COREROUTER_API_KEY" \
  -d '{
    "model": "responses-model-id",
    "input": "请用三句话介绍 CoreRouter。"
  }'

Anthropic Messages

Claude Code 或 Anthropic 风格客户端通常使用 https://api.corerouter.tech 作为 Base URL,实际请求路径是 /v1/messages。

bash
curl https://api.corerouter.tech/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $COREROUTER_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello"}
    ]
  }'

Gemini 风格接口

bash
curl "https://api.corerouter.tech/v1beta/models/gemini-model-id:generateContent" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $COREROUTER_API_KEY" \
  -d '{
    "contents": [
      {
        "parts": [
          {"text": "Hello"}
        ]
      }
    ]
  }'

Windows PowerShell

powershell
$env:COREROUTER_API_KEY="sk-xxxxxxxxxxxxxxxx"

curl.exe https://api.corerouter.tech/v1/chat/completions `
  -H "Content-Type: application/json" `
  -H "Authorization: Bearer $env:COREROUTER_API_KEY" `
  -d '{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"Hello"}]}'

PowerShell 里 curl 可能是 Invoke-WebRequest 的别名。为了行为和 macOS / Linux 的 curl 一致,建议写成 curl.exe。

常见错误

  • 401 Unauthorized:API Key 无效、过期、被禁用,或 Header 缺少 Bearer。
  • 400 Bad Request:JSON 格式错误、缺少 model,或模型 ID 不存在。
  • 403 Forbidden:API Key 权限、IP 白名单、模型权限或账户状态不满足。
  • 429 Too Many Requests:请求过快,降低并发或稍后重试。
  • 404 Not Found:检查路径是否正确,尤其是 /v1/responses、/v1/messages 和 Gemini 路径。

Released under the MIT License.