Claude API
  1. 消息
Claude API
  • API 参考
    • 入门指南
    • IP 地址
    • 版本
    • 错误
    • 速率限制
    • 客户端 SDK
    • 支持地区
    • 获取帮助
    • 亚马逊 BEDROCK API
      • Amazon Bedrock API
    • Vertex AI
      • Vertex AI API
    • 消息
      • 流式消息
      • 从文本补全迁移
      • 消息示例
      • 消息
        POST
      • 计算消息令牌数
        POST
    • 模型
      • 列出模型
      • 获取模型
    • 消息批次
      • 消息批次示例
      • 创建消息批次
      • 检索消息批次
      • 获取消息批次结果
      • 列出消息批次
      • 取消消息批次
      • 删除消息批次
    • 文本补全(传统)
      • 流式文本补全
      • 提示验证
      • 创建文本补全
    • 管理员 API
      • 组织成员管理
        • 获取用户
        • 列出用户
        • 更新用户
        • 移除用户
      • 组织邀请
        • 获取邀请
        • 列出邀请
        • 创建邀请
        • 删除邀请
      • 工作区管理
        • 获取工作区
        • 列出工作区
        • 更新工作区
        • 创建工作区
        • 归档工作区
      • 工作区成员管理
        • 获取工作区成员
        • 列出工作空间成员
        • 添加工作区成员
        • 更新工作空间成员
        • 删除工作区成员
      • API 密钥
        • 获取API密钥
        • 列出API密钥
        • 更新API密钥
  1. 消息

消息

POST
https://api.anthropic.com/v1/messages
Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.
The Messages API can be used for either single queries or stateless multi-turn conversations.
Learn more about the Messages API in our user guide
请求示例请求示例
Shell
JavaScript
Java
Swift
curl --location --request POST 'https://api.anthropic.com/v1/messages' \
--header 'x-api-key: ' \
--header 'anthropic-version: 2023-06-01' \
--header 'content-type: application/json' \
--data-raw '{
    "model": "claude-3-5-sonnet-20241022",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "Hello, world"}
    ]
}'
响应示例响应示例
200 - 成功示例
{
  "content": [
    {
      "text": "Hi! My name is Claude.",
      "type": "text"
    }
  ],
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "model": "claude-3-5-sonnet-20241022",
  "role": "assistant",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "input_tokens": 2095,
    "output_tokens": 503
  }
}

请求参数

Header 参数
x-api-key
string 
必需
Your unique API key for authentication.
This key is required in the header of all API requests, to authenticate your account and access Anthropic's services. Get your API key through the Console. Each key is scoped to a Workspace.
示例值:
{{ANTHROPIC_API_KEY}}
anthropic-version
string 
必需
The version of the Anthropic API you want to use.
Read more about versioning and our version history here.
示例值:
2023-06-01
content-type
string 
必需
示例值:
application/json
Body 参数application/json
model
string 
必需
The model that will complete your prompt.
See models for additional details and options.
Required string length: 1 - 256
max_tokens
integer 
必需
The maximum number of tokens to generate before stopping.
Note that our models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate.
Different models have different maximum values for this parameter. See models for details.
Required range: x > 1
messages
array [object {2}] 
必需
Input messages.
Our models are trained to operate on alternating user and assistant conversational turns. When creating a new Message, you specify the prior conversational turns with the messages parameter, and the model then generates the next Message in the conversation. Consecutive user or assistant turns in your request will be combined into a single turn.
Each input message must be an object with a role and content. You can specify a single user-role message, or you can include multiple user and assistant messages.
If the final message uses the assistant role, the response content will continue immediately from the content in that message. This can be used to constrain part of the model's response.
Example with a single user message:
[{"role": "user", "content": "Hello, Claude"}]
Example with multiple conversational turns:
[
  {"role": "user", "content": "Hello there."},
  {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"},
  {"role": "user", "content": "Can you explain LLMs in plain English?"},
]
Example with a partially-filled response from Claude:
[
  {"role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},
  {"role": "assistant", "content": "The best answer is ("},
]
Each input message content may be either a single string or an array of content blocks, where each block has a specific type. Using a string for content is shorthand for an array of one content block of type "text". The following input messages are equivalent:
{"role": "user", "content": "Hello, Claude"}
{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}
Starting with Claude 3 models, you can also send image content blocks:
{"role": "user", "content": [
  {
    "type": "image",
    "source": {
      "type": "base64",
      "media_type": "image/jpeg",
      "data": "/9j/4AAQSkZJRg...",
    }
  },
  {"type": "text", "text": "What is in this image?"}
]}
We currently support the base64 source type for images, and the image/jpeg, image/png, image/gif, and image/webp media types.
See examples for more input examples.
Note that if you want to include a system prompt, you can use the top-level system parameter — there is no "system" role for input messages in the Messages API.
role
string 
可选
Conversational role of the generated message.
This will always be "assistant".
Available options:
assistant
content
string 
可选
Content generated by the model.
This is an array of content blocks, each of which has a type that determines its shape.
Example:
[{"type": "text", "text": "Hi, I'm Claude."}]
If the request input messages ended with an assistant turn, then the response content will continue directly from that last turn. You can use this to constrain the model's output.
For example, if the input messages were:
[
  {"role": "user", "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"},
  {"role": "assistant", "content": "The best answer is ("}
]
Then the response content might be:
[{"type": "text", "text": "B)"}]
示例

返回响应

🟢200成功
application/json
Body
content
array [object {2}] 
必需
text
string 
可选
type
string 
可选
id
string 
必需
model
string 
必需
role
string 
必需
stop_reason
string 
必需
stop_sequence
null 
必需
type
string 
必需
usage
object 
必需
input_tokens
integer 
必需
output_tokens
integer 
必需
🟠400请求有误
修改于 2025-02-25 08:57:53
上一页
消息示例
下一页
计算消息令牌数
Built with