Overview
This interface is used to interact with Anthropic models, following the Anthropic Messages API specification. It uses the dedicated prefix /api/anthropic/v1/messages and supports features such as multi-turn conversations and streaming responses.
API details
Constraints
- The caller must have an API Key. For more information, see Manage AI API Keys.
Request path
POST {BASE_URL}/api/anthropic/v1/messages
Request header
Name |
Required |
Example value |
Description |
|---|---|---|---|
| Authorization | Yes | Bearer {token} | Authentication information |
| Content-Type | No | application/json | Request body format |
Request parameters
Path
None
Query
None
Body
Name |
Type |
Required |
Example value |
Description |
|---|---|---|---|---|
| model | string | Yes | claude-3-opus-20240229 | Model name. For supported models, see Query Anthropic Model List |
| max_tokens | integer | Yes | 1024 | Maximum number of tokens to generate. Different models have different maximum limits |
| messages | array[object] | Yes | [{"role": "user", "content": "Hello"}] | Message list. Supports multi-turn conversations, with each message containing role and content |
| system | string or array[object] | No | "You are a helpful assistant" | System prompt, used to provide context and instructions |
messages
Each message contains the following fields:
Name |
Type |
Required |
Description |
|---|---|---|---|
| role | string | Yes | Role, optional values: user (user), assistant (assistant) |
| content | string or array[object] | Yes | Message content. Can be a string or an array of content blocks |
Content block types:
- Text content:
{"type": "text", "text": "Message text"} - Image content:
{"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": "..."}} - Tool use:
{"type": "tool_use", "id": "...", "name": "...", "input": {...}} - Tool result:
{"type": "tool_result", "tool_use_id": "...", "content": "..."}
Response
Response parameters
Name |
Type |
Description |
|---|---|---|
| success | boolean | Whether the request was successful |
| code | string | Return code |
| message | string | Return information |
| data | object | Business return data |
Request example
Basic text conversation
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello, Claude"
}
]
}'
Multi-turn conversation
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"system": "You are a helpful assistant",
"messages": [
{
"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?"
}
]
}'
Streaming response
curl --request POST '{BASE_URL}/api/anthropic/v1/messages' \
--header 'Authorization: Bearer {token}' \
--header 'Content-Type: application/json' \
--data '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Write a short poem"
}
],
"stream": true
}'
Response example
Success response
{
"id": "msg_01XYZ",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"model": "claude-3-opus-20240229",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 10,
"output_tokens": 8,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}
Streaming response events
event: message_start
data: {"type": "message_start", "message": {"id": "msg_01XYZ", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "usage": {"input_tokens": 10, "output_tokens": 0}}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}
event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}
event: content_block_stop
data: {"type": "content_block_stop", "index": 0}
event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 8}}
event: message_stop
data: {"type": "message_stop"}
Error response
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "messages: field required"
}
}
