跳转到主要内容
POST
/
api
/
v1
/
messages
curl --request POST \
  --url https://api.crun.ai/api/v1/messages \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 512,
  "messages": [
    {
      "role": "user",
      "content": "Write a concise project update for a payments API migration."
    }
  ]
}
'
{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4-6",
  "content": [
    {
      "type": "text",
      "text": "The payments API migration is on track. Authentication updates are complete, integration testing is in progress, and rollback criteria will be finalized before release."
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 18,
    "output_tokens": 34
  }
}

API 地址

POST https://api.crun.ai/api/v1/messages
该接口遵循 Anthropic Messages API 的请求和响应结构。 它面向 Claude 兼容客户端设计,同时使用您的 CRUN API Key 和 CRUN 公开模型 ID。

身份验证

您可以使用以下任一方式进行身份验证:
  • Authorization: Bearer YOUR_API_KEY
  • X-API-KEY: YOUR_API_KEY
可选的 Anthropic 请求头,例如 anthropic-versionanthropic-beta,会在提供时透传到上游。

基本消息

curl -X POST "https://api.crun.ai/api/v1/messages" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 512,
    "messages": [
      {
        "role": "user",
        "content": "Write a concise project update for a payments API migration."
      }
    ]
  }'

System Prompt

Anthropic Messages 使用顶层 system 字段,而不是在 messages 数组中使用 system role。
request body
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 800,
  "system": "You are a senior API documentation editor. Be concise and precise.",
  "messages": [
    {
      "role": "user",
      "content": "Rewrite this changelog entry for developers: fixed auth bug."
    }
  ]
}

对话历史

CRUN 不会为 /messages 存储对话状态。如需继续对话,请在下一次请求中包含之前的 user 和 assistant 轮次。
request body
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 800,
  "messages": [
    {
      "role": "user",
      "content": "Give me three names for an internal developer newsletter."
    },
    {
      "role": "assistant",
      "content": "Here are three options: Build Notes, Ship Log, and Dev Dispatch."
    },
    {
      "role": "user",
      "content": "Make them sound more enterprise-ready."
    }
  ]
}

视觉输入

当所选模型支持视觉能力时,可以传入包含 textimage blocks 的 content 数组。图片 source 可以使用 URL。
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 800,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Summarize the product shown in this image."
        },
        {
          "type": "image",
          "source": {
            "type": "url",
            "url": "https://example.com/product-photo.png"
          }
        }
      ]
    }
  ]
}

工具调用

传入 tools 后,模型可以请求结构化函数调用。您的应用负责执行工具,并在后续消息中把工具结果传回模型。
request body
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "What is the weather in Paris?"
    }
  ],
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather for a city.",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        },
        "required": ["city"]
      }
    }
  ],
  "tool_choice": {
    "type": "auto"
  }
}

Thinking

当所选模型和上游服务支持时,可以使用 thinking 请求扩展思考行为。
request body
{
  "model": "claude-sonnet-4-6",
  "max_tokens": 2048,
  "thinking": {
    "type": "enabled",
    "budget_tokens": 1024
  },
  "messages": [
    {
      "role": "user",
      "content": "Compare two API migration plans and recommend the lower-risk option."
    }
  ]
}

流式输出

设置 stream=true 后,可以接收 Anthropic 风格的 Server-Sent Events。
curl -N "https://api.crun.ai/api/v1/messages" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 512,
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": "Write a three-bullet checklist for API release readiness."
      }
    ]
  }'

响应示例

{
  "id": "msg_abc123",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-4-6",
  "content": [
    {
      "type": "text",
      "text": "The payments API migration is on track. Authentication updates are complete, integration testing is in progress, and rollback criteria will be finalized before release."
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 18,
    "output_tokens": 34
  }
}

注意事项

  • max_tokens 会在 CRUN 已配置模型上限时受所选模型输出上限约束。
  • messages 仅支持 userassistant roles。系统指令请使用顶层 system 字段。
  • 其他 Anthropic 兼容字段会被接受,并在上游模型支持时透传。
  • 工具调用、thinking、图片 blocks、URL sources 和 beta 功能取决于所选模型和上游服务商。
  • 未知模型 ID 会返回 OpenAI 风格的错误体,其中 code"model_not_found"

相关资源

LLM 快速开始

了解 base URL、身份验证方式以及 SDK 接入模式。

Responses API

使用 OpenAI 兼容的灵活输入、结构化输出和流式能力构建工作流。

价格

前往价格页面,比较不同模型的计费规则。

授权

Authorization
string
header
必填

将您的 CRUN API Key 作为 Bearer token,用于 Anthropic 兼容 SDK。

请求头

anthropic-version
string

可选 Anthropic API 版本头,会透传到上游。

示例:

"2023-06-01"

anthropic-beta
string

可选 Anthropic beta 功能头,会透传到上游。

请求体

application/json

Anthropic 兼容 Messages 请求。其他兼容字段会被接受,并在上游模型支持时透传。

model
string
必填

GET /api/v1/models 返回的公开模型 ID。

Minimum string length: 1
示例:

"claude-sonnet-4-6"

messages
object[]
必填

对话消息。要继续多轮对话,请自行传入历史 user 和 assistant 轮次。

Minimum array length: 1
max_tokens
integer

最大输出 token 数,会受所选模型限制。

必填范围: x >= 1
示例:

1024

system

系统提示词。Anthropic Messages 使用顶层 system 字段。

示例:

"You are a concise assistant."

stream
boolean
默认值:false

是否返回 Server-Sent Events 流。

示例:

false

stop_sequences
string[]

停止词列表。

示例:
["END"]
temperature
number

采样温度。

必填范围: 0 <= x <= 1
示例:

0.7

top_p
number

核采样参数。

必填范围: 0 <= x <= 1
示例:

1

top_k
integer

Top-k 采样参数。

必填范围: x >= 0
示例:

40

metadata
object

开发者自定义元数据。

tools
object[]

Anthropic 工具定义。应用需要执行工具调用,并在后续请求中返回工具结果。

tool_choice

工具选择策略。

示例:

"auto"

thinking
object

扩展思考配置,上游模型支持时生效。

示例:
{ "type": "enabled", "budget_tokens": 1024 }

响应

成功响应。stream=false 时返回 JSON,stream=true 时返回 SSE。

Anthropic Messages 响应。

id
string
示例:

"msg_abc123"

type
string
示例:

"message"

role
string
示例:

"assistant"

model
string
示例:

"claude-sonnet-4-6"

content
object[]
stop_reason
string | null
示例:

"end_turn"

stop_sequence
string | null
usage
object

Anthropic usage 信息。