Skip to content

curl 接入 ​

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

基础调用 ​

bash
export COREROUTER_API_KEY="sk-xxxxxxxxxxxxxxxx"

curl https://api.corerouter.cloud/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 --no-buffer https://api.corerouter.cloud/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
  }'

--no-buffer 关闭 curl 自身的输出缓冲,便于直接观察事件;它不能消除上游、代理或客户端的缓冲。

多轮对话 ​

bash
curl https://api.corerouter.cloud/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.cloud/v1/models \
  -H "Authorization: Bearer $COREROUTER_API_KEY"

Responses API ​

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

bash
curl https://api.corerouter.cloud/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.cloud 作为 Base URL,实际请求路径是 /v1/messages。

bash
curl https://api.corerouter.cloud/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.cloud/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"

$body = @{
  model = "claude-sonnet-4-5"
  messages = @(@{ role = "user"; content = "Hello" })
} | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod `
  -Uri "https://api.corerouter.cloud/v1/chat/completions" `
  -Method Post `
  -Headers @{ Authorization = "Bearer $env:COREROUTER_API_KEY" } `
  -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

$response | ConvertTo-Json -Depth 20

上例使用 PowerShell 原生命令构造 JSON 并发送 UTF-8 请求,避免 Windows PowerShell 5.1 和 PowerShell 7 向外部程序传递 JSON 引号时的差异。若自行使用 curl,请显式写 curl.exe,因为 curl 在部分版本中是 Invoke-WebRequest 的别名。

常见错误 ​

  • 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.