Claude Messages API Anthropic Claude Messages 原生格式接口,调用 Claude 系列模型。
接口地址:POST /v1/messages
功能描述:使用 Anthropic Claude Messages 格式创建对话响应。
认证方式:Bearer Token
Authorization: Bearer YOUR_API_TOKEN
参数名 类型 必填 说明 示例 anthropic-version string 是 Anthropic API 版本 2023-06-01 Authorization string 是 Bearer Token Bearer sk-xxx
参数名 类型 必填 说明 示例 model string 是 模型名称 claude-opus-4-7 messages array 是 对话消息列表 - system string/array 否 系统提示词 - max_tokens integer 是 最大生成 token 数 1024 temperature number 否 温度 (0-1) 0.7 top_p number 否 Nucleus sampling 0.9 top_k integer 否 Top-k sampling 40 stream boolean 否 是否流式输出 true stop_sequences array 否 停止序列 - tools array 否 工具定义 - tool_choice object 否 工具选择策略 - thinking object 否 思考模式配置 - metadata object 否 元数据 -
参数名 类型 必填 说明 role string 是 角色,可选值:user, assistant content string/array 是 消息内容
参数名 类型 必填 说明 name string 是 工具名称 description string 否 工具描述 input_schema object 是 工具输入参数 schema
参数名 类型 必填 说明 type string 是 类型:enabled, disabled budget_tokens integer 否 思考预算 token 数
参数名 类型 说明 id string 消息 ID type string 类型,固定为 message role string 角色,固定为 assistant content array 内容列表 model string 模型名称 stop_reason string 停止原因 usage object Token 使用情况
参数名 类型 说明 type string 内容类型:text, tool_use text string 文本内容(type 为 text 时) id string 工具调用 ID(type 为 tool_use 时) name string 工具名称(type 为 tool_use 时) input object 工具输入(type 为 tool_use 时)
参数名 类型 说明 input_tokens integer 输入 tokens output_tokens integer 输出 tokens cache_creation_input_tokens integer 缓存创建 tokens cache_read_input_tokens integer 缓存读取 tokens
curl -X POST https://api.onedim.ai/v1/messages \
-H " Content-Type: application/json " \
-H " anthropic-version: 2023-06-01 " \
-H " Authorization: Bearer YOUR_API_TOKEN " \
-d ' {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
} '
响应示例:
{
" id " : " msg_01X... " ,
" type " : " message " ,
" role " : " assistant " ,
" content " : [
{
" type " : " text " ,
" text " : " Hello! How can I help you today? "
}
],
" model " : " claude-opus-4-7 " ,
" stop_reason " : " end_turn " ,
" usage " : {
" input_tokens " : 10 ,
" output_tokens " : 12 ,
" cache_creation_input_tokens " : 0 ,
" cache_read_input_tokens " : 0
}
}
curl -X POST https://api.onedim.ai/v1/messages \
-H " Content-Type: application/json " \
-H " anthropic-version: 2023-06-01 " \
-H " Authorization: Bearer YOUR_API_TOKEN " \
-d ' {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"system": "You are a professional translator. Translate all user input to Chinese.",
"messages": [
{"role": "user", "content": "Hello, how are you today?"}
]
} '
import anthropic
client = anthropic . Anthropic (
api_key = " YOUR_API_TOKEN " ,
base_url = " https://ezmodel.cloud "
)
with client . messages . stream (
model = " claude-opus-4-7 " ,
max_tokens = 1024 ,
messages =[{ " role " : " user " , " content " : " 讲一个故事 " }]
) as stream :
for text in stream . text_stream :
print ( text , end = "" , flush = True )
curl -X POST https://api.onedim.ai/v1/messages \
-H " Content-Type: application/json " \
-H " anthropic-version: 2023-06-01 " \
-H " Authorization: Bearer YOUR_API_TOKEN " \
-d ' {
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
],
"messages": [
{"role": "user", "content": "北京今天天气怎么样?"}
]
} '
import anthropic
import base64
client = anthropic . Anthropic (
api_key = " YOUR_API_TOKEN " ,
base_url = " https://ezmodel.cloud "
)
# 使用 URL
message = client . messages . create (
model = " claude-opus-4-7 " ,
max_tokens = 1024 ,
messages =[
{
" role " : " user " ,
" content " : [
{
" type " : " image " ,
" source " : {
" type " : " url " ,
" url " : " https://example.com/image.jpg "
}
},
{
" type " : " text " ,
" text " : " 描述这张图片 "
}
]
}
]
)
# 或使用 Base64
with open ( " image.jpg " , " rb " ) as f :
image_data = base64 . standard_b64encode ( f . read ()). decode ( " utf-8 " )
message = client . messages . create (
model = " claude-opus-4-7 " ,
max_tokens = 1024 ,
messages =[
{
" role " : " user " ,
" content " : [
{
" type " : " image " ,
" source " : {
" type " : " base64 " ,
" media_type " : " image/jpeg " ,
" data " : image_data
}
},
{
" type " : " text " ,
" text " : " 描述这张图片 "
}
]
}
]
)
模型 上下文窗口 说明 claude-opus-4-7 200K / 1M 最强能力,适合复杂任务 claude-sonnet-4-6 200K / 1M 速度与能力平衡,日常首选
特性 Claude Messages OpenAI Chat 系统消息 独立 system 字段 messages 数组中 响应格式 content 数组 message.content 字符串 必填参数 max_tokens 必填 max_tokens 可选 停止原因 stop_reason finish_reason
如果你习惯使用 OpenAI 格式,也可以使用 /v1/chat/completions 接口调用 Claude 模型,系统会自动转换格式。