Create a Message
POST/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
Parameters
Returns
Create a Message
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
$client = new Client(apiKey: 'my-anthropic-api-key');
$message = $client->messages->create(
maxTokens: 1024,
messages: [['content' => 'Hello, world', 'role' => 'user']],
model: Model::CLAUDE_OPUS_5,
cacheControl: ['type' => 'ephemeral', 'ttl' => '5m'],
container: [
'id' => 'id',
'skills' => [
['skillID' => 'pdf', 'type' => 'anthropic', 'version' => 'latest']
],
],
inferenceGeo: 'inference_geo',
metadata: ['userID' => '13803d75-b4b5-4c3e-b2a2-6f21399b021b'],
outputConfig: [
'effort' => 'low',
'format' => ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
],
serviceTier: 'auto',
stopSequences: ['string'],
system: [
[
'text' => 'Today\'s date is 2024-06-01.',
'type' => 'text',
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'citations' => [
[
'citedText' => 'The grass is green. The sky is blue.',
'documentIndex' => 0,
'documentTitle' => 'x',
'endCharIndex' => 0,
'startCharIndex' => 0,
'type' => 'char_location',
],
],
],
],
temperature: 1,
thinking: ['type' => 'adaptive', 'display' => 'summarized'],
toolChoice: ['type' => 'auto', 'disableParallelToolUse' => true],
tools: [
[
'inputSchema' => [
'type' => 'object',
'properties' => ['location' => 'bar', 'unit' => 'bar'],
'required' => ['location'],
],
'name' => 'name',
'allowedCallers' => ['direct'],
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'deferLoading' => true,
'description' => 'Get the current weather in a given location',
'eagerInputStreaming' => true,
'inputExamples' => [['foo' => 'bar']],
'strict' => true,
'type' => 'custom',
],
],
topK: 5,
topP: 0.7,
userProfileID: 'anthropic-user-profile-id',
workspaceID: 'wrkspc_011CZkZaBF1tNoB5wlCeusgy',
);
var_dump($message);Response 200
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"container": {
"id": "container_011CpZohnwH4vuy7gazohgSP",
"expires_at": "2019-12-27T18:11:19.117Z",
"skills": [
{
"skill_id": "pdf",
"type": "anthropic",
"version": "latest"
}
]
},
"content": [
{
"citations": [
{
"cited_text": "The grass is green. The sky is blue.",
"document_index": 0,
"document_title": "My Document",
"end_char_index": 0,
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w",
"start_char_index": 0,
"type": "char_location"
}
],
"text": "Hi! My name is Claude.",
"type": "text"
}
],
"model": "claude-opus-5",
"role": "assistant",
"stop_details": {
"category": "cyber",
"explanation": "This request was declined because it conflicts with Anthropic's Usage Policy.",
"type": "refusal"
},
"stop_reason": "end_turn",
"stop_sequence": null,
"type": "message",
"usage": {
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 2051,
"cache_read_input_tokens": 2051,
"inference_geo": "global",
"input_tokens": 2095,
"output_tokens": 503,
"output_tokens_details": {
"thinking_tokens": 0
},
"server_tool_use": {
"web_fetch_requests": 2,
"web_search_requests": 0
},
"service_tier": "standard"
}
}Returns Examples
Response 200
{
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"container": {
"id": "container_011CpZohnwH4vuy7gazohgSP",
"expires_at": "2019-12-27T18:11:19.117Z",
"skills": [
{
"skill_id": "pdf",
"type": "anthropic",
"version": "latest"
}
]
},
"content": [
{
"citations": [
{
"cited_text": "The grass is green. The sky is blue.",
"document_index": 0,
"document_title": "My Document",
"end_char_index": 0,
"file_id": "file_011CNha8iCJcU1wXNR6q4V8w",
"start_char_index": 0,
"type": "char_location"
}
],
"text": "Hi! My name is Claude.",
"type": "text"
}
],
"model": "claude-opus-5",
"role": "assistant",
"stop_details": {
"category": "cyber",
"explanation": "This request was declined because it conflicts with Anthropic's Usage Policy.",
"type": "refusal"
},
"stop_reason": "end_turn",
"stop_sequence": null,
"type": "message",
"usage": {
"cache_creation": {
"ephemeral_1h_input_tokens": 0,
"ephemeral_5m_input_tokens": 0
},
"cache_creation_input_tokens": 2051,
"cache_read_input_tokens": 2051,
"inference_geo": "global",
"input_tokens": 2095,
"output_tokens": 503,
"output_tokens_details": {
"thinking_tokens": 0
},
"server_tool_use": {
"web_fetch_requests": 2,
"web_search_requests": 0
},
"service_tier": "standard"
}
}