---¶
title: Using the Messages API¶
url: https://platform.claude.com/docs/en/build-with-claude/working-with-messages¶
description: Practical patterns and examples for using the Messages API effectively¶
featureMetadata:¶
status: stable¶
zdr:¶
eligibility: eligible¶
note: Excludes [Covered Models](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention#model-specific-data-retention-requirements). Standard API calls for generating Claude responses.¶
supportedModels:¶
- claude-fable-5-2¶
- claude-mythos-5-2¶
- claude-fable-5-1¶
- claude-mythos-5-1¶
- claude-fable-5¶
- claude-mythos-5¶
- claude-mythos-preview¶
- claude-opus-5-5¶
- claude-opus-5¶
- claude-sonnet-5-5¶
- claude-sonnet-5¶
- claude-haiku-5-5¶
- claude-haiku-4-5-20251001¶
- claude-opus-4-8¶
- claude-opus-4-7¶
- claude-opus-4-6¶
- claude-sonnet-4-6¶
- claude-opus-4-5-20251101¶
- claude-sonnet-4-5-20250929¶
supportedPlatforms:¶
Claude API: stable¶
Amazon Bedrock: stable¶
Amazon Bedrock (bedrock-runtime): stable¶
Claude Platform on AWS: stable¶
Claude Models on Google Cloud (Anthropic managed): stable¶
Claude Models on Google Cloud (Google managed): stable¶
Microsoft Foundry: stable¶
---¶
¶
Anthropic offers two ways to build with Claude, each suited to different use cases:¶
¶
| | Messages API | Claude Managed Agents |¶
| ----- | ----- | ----- |¶
| **What it is** | Direct model prompting access | Pre-built, configurable agent harness that runs in managed infrastructure |¶
| **Best for** | Custom agent loops and fine-grained control | Long-running tasks and asynchronous work |¶
¶
This guide covers common patterns for working with the Messages API, including basic requests, multi-turn conversations, prefill techniques, and vision capabilities. For complete API specifications, see the [Messages API reference](https://platform.claude.com/docs/en/api/messages/create). For the managed agent harness instead, see the [Claude Managed Agents overview](https://platform.claude.com/docs/en/managed-agents/overview).¶
¶
## Basic request and response¶
¶
<Note>¶
The `temperature`, `top_p`, and `top_k` sampling parameters are not supported on Claude 4.7 and later models and Claude Mythos Preview, or on any model starting with [API version `2026-10-21`](https://platform.claude.com/docs/en/api/migration-2026-10-21#sampling-parameters). Setting them to a non-default value returns a 400 error, and starting with `2026-10-21` any value does. Omit them from request payloads and use prompting to guide the model’s behavior instead. See the [breaking changes in the migration guide](https://platform.claude.com/docs/en/models/opus-5-5/migration-guide#opus-46-breaking-changes).¶
</Note>¶
¶
<CodeGroup>¶
```bash cURL¶
#!/bin/sh¶
curl https://api.anthropic.com/v1/messages \¶
-H "x-api-key: $ANTHROPIC_API_KEY" \¶
-H "anthropic-version: 2026-10-21" \¶
-H "content-type: application/json" \¶
-d '{¶
"model": "claude-opus-5-5",¶
"max_tokens": 1024,¶
"messages": [¶
{"role": "user", "content": "Hello, Claude"}¶
]¶
}'¶
```¶
¶
```bash CLI¶
ant messages create \¶
--model claude-opus-5-5 \¶
--max-tokens 1024 \¶
--message '{role: user, content: "Hello, Claude"}'¶
```¶
¶
```python Python¶
message = anthropic.Anthropic().messages.create(¶
model="claude-opus-5-5",¶
max_tokens=1024,¶
messages=[{"role": "user", "content": "Hello, Claude"}],¶
)¶
print(message)¶
```¶
¶
```typescript TypeScript¶
const anthropic = new Anthropic();¶
¶
const message = await anthropic.messages.create({¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [{ role: "user", content: "Hello, Claude" }]¶
});¶
console.log(message);¶
```¶
¶
```csharp C#¶
AnthropicClient client = new();¶
¶
var parameters = new MessageCreateParams¶
{¶
Model = Model.ClaudeOpus5_5,¶
MaxTokens = 1024,¶
Messages = [new UserMessageParam { Content = "Hello, Claude" }]¶
};¶
var message = await client.Messages.Create(parameters);¶
Console.WriteLine(message);¶
```¶
¶
```go Go¶
client := anthropic.NewClient()¶
¶
response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{¶
Model: anthropic.ModelClaudeOpus5_5,¶
MaxTokens: 1024,¶
Messages: []anthropic.MessageParam{¶
anthropic.NewUserMessage(anthropic.NewTextBlock("Hello, Claude")),¶
},¶
})¶
if err != nil {¶
log.Fatal(err)¶
}¶
fmt.Println(response)¶
```¶
¶
```java Java¶
AnthropicClient client = AnthropicOkHttpClient.fromEnv();¶
¶
MessageCreateParams params = MessageCreateParams.builder()¶
.model(Model.CLAUDE_OPUS_5_5)¶
.maxTokens(1024L)¶
.addUserMessage("Hello, Claude")¶
.build();¶
¶
Message response = client.messages().create(params);¶
System.out.println(response);¶
```¶
¶
```php PHP¶
$client = new Client();¶
¶
$message = $client->messages->create(¶
maxTokens: 1024,¶
messages: [['role' => 'user', 'content' => 'Hello, Claude']],¶
model: Model::CLAUDE_OPUS_5_5,¶
);¶
echo json_encode($message, JSON_PRETTY_PRINT), PHP_EOL;¶
```¶
¶
```ruby Ruby¶
client = Anthropic::Client.new¶
¶
message = client.messages.create(¶
model: Anthropic::Model::CLAUDE_OPUS_5_5,¶
max_tokens: 1024,¶
messages: [¶
{ role: "user", content: "Hello, Claude" }¶
]¶
)¶
puts message¶
```¶
</CodeGroup>¶
¶
```json Output¶
{¶
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",¶
"type": "message",¶
"role": "assistant",¶
"content": [¶
{¶
"type": "text",¶
"text": "Hello!"¶
}¶
],¶
"model": "claude-opus-5-5",¶
"stop_reason": "end_turn",¶
"stop_sequence": null,¶
"usage": {¶
"input_tokens": 12,¶
"output_tokens": 6¶
}¶
}¶
```¶
¶
Refusal responses (`stop_reason: "refusal"`) also include a `stop_details` object identifying the policy category that triggered the refusal, on every model. See [What a refusal looks like](https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#refusal-response) for the field reference and example handling code.¶
¶
## Multiple conversational turns¶
¶
The Messages API is stateless, which means that you always send the full conversational history to the API. You can use this pattern to build up a conversation over time. Earlier conversational turns don’t necessarily need to actually originate from Claude. You can use synthetic `assistant` messages.¶
¶
With [message threads](https://platform.claude.com/docs/en/build-with-claude/message-threads), in beta, the API can keep the conversation, so that each later request sends only the new messages. A thread expires 5 minutes after its last request and is deleted no more than 1 hour after its last use.¶
¶
<CodeGroup>¶
```bash cURL¶
#!/bin/sh¶
curl https://api.anthropic.com/v1/messages \¶
-H "x-api-key: $ANTHROPIC_API_KEY" \¶
-H "anthropic-version: 2026-10-21" \¶
-H "content-type: application/json" \¶
-d '{¶
"model": "claude-opus-5-5",¶
"max_tokens": 1024,¶
"messages": [¶
{"role": "user", "content": "Hello, Claude"},¶
{"role": "assistant", "content": "Hello!"},¶
{"role": "user", "content": "Can you describe LLMs to me?"}¶
¶
]¶
}'¶
```¶
¶
```bash CLI¶
ant messages create \¶
--model claude-opus-5-5 \¶
--max-tokens 1024 \¶
--message '{role: user, content: "Hello, Claude"}' \¶
--message '{role: assistant, content: "Hello!"}' \¶
--message '{role: user, content: "Can you describe LLMs to me?"}'¶
```¶
¶
```python Python¶
message = anthropic.Anthropic().messages.create(¶
model="claude-opus-5-5",¶
max_tokens=1024,¶
messages=[¶
{"role": "user", "content": "Hello, Claude"},¶
{"role": "assistant", "content": "Hello!"},¶
{"role": "user", "content": "Can you describe LLMs to me?"},¶
],¶
)¶
print(message)¶
```¶
¶
```typescript TypeScript¶
const anthropic = new Anthropic();¶
¶
const message = await anthropic.messages.create({¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [¶
{ role: "user", content: "Hello, Claude" },¶
{ role: "assistant", content: "Hello!" },¶
{ role: "user", content: "Can you describe LLMs to me?" }¶
]¶
});¶
console.log(message);¶
```¶
¶
```csharp C#¶
AnthropicClient client = new();¶
¶
var parameters = new MessageCreateParams¶
{¶
Model = Model.ClaudeOpus5_5,¶
MaxTokens = 1024,¶
Messages =¶
[¶
new UserMessageParam { Content = "Hello, Claude" },¶
new AssistantMessageParam { Content = "Hello!" },¶
new UserMessageParam { Content = "Can you describe LLMs to me?" }¶
]¶
};¶
¶
var message = await client.Messages.Create(parameters);¶
Console.WriteLine(message);¶
```¶
¶
```go Go¶
client := anthropic.NewClient()¶
¶
response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{¶
Model: anthropic.ModelClaudeOpus5_5,¶
MaxTokens: 1024,¶
Messages: []anthropic.MessageParam{¶
anthropic.NewUserMessage(anthropic.NewTextBlock("Hello, Claude")),¶
anthropic.NewAssistantMessage(anthropic.NewTextBlock("Hello!")),¶
anthropic.NewUserMessage(anthropic.NewTextBlock("Can you describe LLMs to me?")),¶
},¶
})¶
if err != nil {¶
log.Fatal(err)¶
}¶
fmt.Println(response)¶
```¶
¶
```java Java¶
AnthropicClient client = AnthropicOkHttpClient.fromEnv();¶
¶
MessageCreateParams params = MessageCreateParams.builder()¶
.model(Model.CLAUDE_OPUS_5_5)¶
.maxTokens(1024L)¶
.addUserMessage("Hello, Claude")¶
.addAssistantMessage("Hello!")¶
.addUserMessage("Can you describe LLMs to me?")¶
.build();¶
¶
Message response = client.messages().create(params);¶
System.out.println(response);¶
```¶
¶
```php PHP¶
$client = new Client();¶
¶
$message = $client->messages->create(¶
maxTokens: 1024,¶
messages: [¶
['role' => 'user', 'content' => 'Hello, Claude'],¶
['role' => 'assistant', 'content' => 'Hello!'],¶
['role' => 'user', 'content' => 'Can you describe LLMs to me?'],¶
],¶
model: Model::CLAUDE_OPUS_5_5,¶
);¶
¶
echo json_encode($message, JSON_PRETTY_PRINT), PHP_EOL;¶
```¶
¶
```ruby Ruby¶
client = Anthropic::Client.new¶
¶
message = client.messages.create(¶
model: Anthropic::Model::CLAUDE_OPUS_5_5,¶
max_tokens: 1024,¶
messages: [¶
{ role: "user", content: "Hello, Claude" },¶
{ role: "assistant", content: "Hello!" },¶
{ role: "user", content: "Can you describe LLMs to me?" }¶
]¶
)¶
puts message¶
```¶
</CodeGroup>¶
¶
```json Output¶
{¶
"id": "msg_018gCsTGsXkYJVqYPxTgDHBU",¶
"type": "message",¶
"role": "assistant",¶
"content": [¶
{¶
"type": "text",¶
"text": "Sure, I'd be happy to provide..."¶
}¶
],¶
"model": "claude-opus-5-5",¶
"stop_reason": "end_turn",¶
"stop_sequence": null,¶
"usage": {¶
"input_tokens": 30,¶
"output_tokens": 309¶
}¶
}¶
```¶
¶
### System role in messages¶
¶
To add a new system instruction partway through a conversation, include a message with `"role": "system"` right after a user turn. That message can’t be the first entry in `messages`. This works on Claude Fable 5.2, [Claude Mythos 5.2](https://platform.claude.com/docs/en/models/mythos-5-2/overview), Claude Fable 5.1, [Claude Mythos 5.1](https://platform.claude.com/docs/en/models/mythos-5-1/overview), Claude Fable 5, [Claude Mythos 5](https://platform.claude.com/docs/en/models/mythos-5/overview), Claude Opus 5.5, Claude Opus 5, Claude Opus 4.8, Claude Sonnet 5.5, and Claude Haiku 5.5. See the full [placement rules](https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages#limitations).¶
¶
A mid-conversation system message has the same authority as the top-level `system` field, but because it is appended to the end of the message history, it does not invalidate any cached prefix that came before it. Use the top-level `system` field for instructions that should apply from the very first turn, and a mid-conversation system message for instructions that only become relevant later.¶
¶
See [Mid-conversation system messages and tool changes](https://platform.claude.com/docs/en/build-with-claude/mid-conversation-system-messages) for the complete guide, including how to combine it with [prompt caching](https://platform.claude.com/docs/en/build-with-claude/prompt-caching).¶
¶
## Prefilling Claude’s response¶
¶
You canA prefill part of Claude’s response in the last position of the input messages list. Use this techniqis text that you write in an `assistant` message and put last in `messages`. An `assistant` message anywhere else in `messages` isn’t a prefill. Claude continues to shape Claude’s response. The following example uses `"max_tokens": 1`he prefilled text, and the response holds only the continuation.¶
¶
Use a prefill to gset a single multiple-choice answer from Claude.¶
¶
<Warning>¶
Prefilling is not supported on how a response starts, for example, to start it in a fixed format or to skip a preamble.¶
¶
Whether the API accepts a prefill depends on the model, not on the API version:¶
¶
* **Claude 4.65 and latearlier models and [Claude Mytho:** The API accepts a prefill unless the model is Preview](https://anthropic.com/glasswing). Requests using prefill with thesetired on the platform that you use. All requests to a retired model fail. To check a model’s return a 400 error. Use [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs) on models that support it, or system prompt instructions, instead. See [Migration guides](https://platform.claude.com/docs/en/about-claude/models/migration-guide) for migration patterns.¶
</Warning>status, see [Model deprecations](https://platform.claude.com/docs/en/about-claude/model-deprecations).¶
* **All other models:** The API returns a 400 error (see the [Prefill not supported](https://platform.claude.com/docs/en/api/errors#prefill-not-supported) section of the Claude API errors page). Use one of the [alternatives to a prefill](https://platform.claude.com/docs/en/build-with-claude/working-with-messages#alternatives-to-a-prefill) instead.¶
¶
The following example runs on Claude Opus 4.5 and sets the format of a multiple-choice answer. The prefill, `The answer is (`, ends right before the answer’s letter so that Claude writes the letter first. Because the answer is one letter, the example also sets `max_tokens` to `1`. For a longer answer, set a higher `max_tokens`.¶
¶
<CodeGroup>¶
```bash cURL¶
#!/bin/sh¶
curl https://api.anthropic.com/v1/messages \¶
-H "x-api-key: $ANTHROPIC_API_KEY" \¶
-H "anthropic-version: 2026-10-21" \¶
-H "content-type: application/json" \¶
-d '{¶
"model": "claude-sonnet-4-5opus-4-5-20251101",¶
"max_tokens": 1,¶
"messages": [¶
{"role": "user", "content": "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"},¶
{"role": "assistant", "content": "The answer is ("}¶
]¶
}'¶
```¶
¶
```bash CLI¶
ant messages create <<'YAML'¶
\¶
--model: claude-sonnet-4-5¶
opus-4-5-20251101 \¶
--max_-tokens: 1 \¶
--messages:¶
- '{role: user¶
, content: "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"}' \¶
- -message '{role: assistant¶
, content: "The answer is ("¶
YAML}'¶
```¶
¶
```python Python¶
message = anthropic.Anthropic().messages.create(¶
model="claude-sonnet-4-5opus-4-5-20251101",¶
max_tokens=1,¶
messages=[¶
{¶
"role": "user",¶
"content": "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae",¶
},¶
{"role": "assistant", "content": "The answer is ("},¶
],¶
)¶
print(message)¶
```¶
¶
```typescript TypeScript¶
const anthropic = new Anthropic();¶
¶
const message = await anthropic.messages.create({¶
model: "claude-sonnet-4-5opus-4-5-20251101",¶
max_tokens: 1,¶
messages: [¶
{¶
role: "user",¶
content: "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"¶
},¶
{ role: "assistant", content: "The answer is (" }¶
]¶
});¶
console.log(message);¶
```¶
¶
```csharp C#¶
AnthropicClient client = new();¶
¶
var parameters = new MessageCreateParams¶
{¶
Model = Model.ClaudeSonnet4_5Opus4_5_20251101,¶
MaxTokens = 1,¶
Messages = [¶
new UserMessageParam { Content = "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae" },¶
new AssistantMessageParam { Content = "The answer is (" }¶
]¶
};¶
¶
var message = await client.Messages.Create(parameters);¶
Console.WriteLine(message);¶
```¶
¶
```go Go¶
client := anthropic.NewClient()¶
¶
response, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{¶
Model: anthropic.ModelClaudeSonnet4_5Opus4_5_20251101,¶
MaxTokens: 1,¶
Messages: []anthropic.MessageParam{¶
anthropic.NewUserMessage(anthropic.NewTextBlock("What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")),¶
anthropic.NewAssistantMessage(anthropic.NewTextBlock("The answer is (")),¶
},¶
})¶
if err != nil {¶
log.Fatal(err)¶
}¶
fmt.Println(response)¶
```¶
¶
```java Java¶
AnthropicClient client = AnthropicOkHttpClient.fromEnv();¶
¶
MessageCreateParams params = MessageCreateParams.builder()¶
.model(Model.CLAUDE_SONNET_4_5OPUS_4_5_20251101)¶
.maxTokens(1L)¶
.addUserMessage("What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae")¶
.addAssistantMessage("The answer is (")¶
.build();¶
¶
Message response = client.messages().create(params);¶
System.out.println(response);¶
```¶
¶
```php PHP¶
$client = new Client();¶
¶
$message = $client->messages->create(¶
maxTokens: 1,¶
messages: [¶
['role' => 'user', 'content' => 'What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae'],¶
['role' => 'assistant', 'content' => 'The answer is ('],¶
],¶
model: 'claude-sonnet-4-5',¶
);¶
echo $message->content[0]->textModel::CLAUDE_OPUS_4_5_20251101,¶
);¶
echo json_encode($message, JSON_PRETTY_PRINT), PHP_EOL;¶
```¶
¶
```ruby Ruby¶
client = Anthropic::Client.new¶
¶
message = client.messages.create(¶
model: Anthropic::Model::CLAUDE_SONNET_4_5OPUS_4_5_20251101,¶
max_tokens: 1,¶
messages: [¶
{¶
role: "user",¶
content: "What is lLatin for Aant? (A) Apoidea, (B) Rhopalocera, (C) Formicidae"¶
},¶
{ role: "assistant", content: "The answer is (" }¶
]¶
)¶
puts message¶
```¶
</CodeGroup>¶
¶
```json Output¶
{¶
"id": "msg_01Q8Faay6S7QPTvEUUQARt7h",¶
"type": "message",¶
"role": "assistant",¶
"content": [¶
{¶
"type": "text",¶
"text": "C"¶
}¶
],¶
"model": "claude-sonnet-4-5opus-4-5-20251101",¶
"stop_reason": "max_tokens",¶
"stop_sequence": null,¶
"usage": {¶
"input_tokens": 42,¶
"output_tokens": 1¶
}¶
}¶
```¶
¶
Note two fields in this response:¶
¶
* **`text`:** The value is `C`. It holds only the continuation, not the prefill.¶
* **`stop_reason`:** The value is `max_tokens` because the example ends the response after one token.¶
¶
To get the full text, `The answer is (C`, join the prefill and the `text` value in your code.¶
¶
On Claude 4.5 and earlier models, these limits apply to a prefill:¶
¶
* **Whitespace:** If the prefill ends with whitespace, the API returns a 400 error.¶
* **Structured outputs:** If the request also sets `output_config.format`, the API returns a 400 error (see the [Feature compatibility](https://platform.claude.com/docs/en/build-with-claude/structured-outputs#feature-compatibility) section of the Structured outputs page).¶
* **[Extended thinking](https://platform.claude.com/docs/en/build-with-claude/extended-thinking):** If the request also sets `thinking.type` to `enabled`, the prefill causes no error. The API treats thinking as off, so the response has no `thinking` block.¶
¶
The `message` in each 400 error says what caused it.¶
¶
### Alternatives to a prefill¶
¶
In place of a prefill, end `messages` with a `user` message. Then, choose an approach by what you want from the response:¶
¶
* **JSON that follows a schema:** Use [structured outputs](https://platform.claude.com/docs/en/build-with-claude/structured-outputs), which constrain the response to follow the schema.¶
* **Another output format:** Describe the format in the system prompt, which is the request’s [`system` field](https://platform.claude.com/docs/en/api/messages/create#create.system).¶
* **No preamble:** Ask in the system prompt for a direct answer.¶
¶
Unlike a prefill, a system prompt instruction doesn’t guarantee how the response starts, so check the response’s text in your code.¶
¶
For what to use in place of other common prefills, see the [Migrating away from prefilled responses](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#migrating-away-from-prefilled-responses) section of the Prompting best practices page.¶
¶
To move the multiple-choice example to a model that doesn’t accept a prefill, follow these steps:¶
¶
1. Change the model and remove the `assistant` message.¶
2. Ask in the system prompt for only the answer’s letter.¶
3. Set `max_tokens` high enough for the whole response. On [models with thinking on by default](https://platform.claude.com/docs/en/build-with-claude/thinking#configuring-thinking), thinking tokens also count toward `max_tokens`.¶
4. Read the letter from the response’s `text` block. This block can follow a `thinking` block. Don’t join the letter to the prefill.¶
¶
## Vision¶
¶
Claude can read both text and images in requests. You can supply images using the `base64`, `url`, or `file` source types. The `file` source type references an image uploaded through the [Files API](https://platform.claude.com/docs/en/build-with-claude/files). Supported media types are `image/jpeg`, `image/png`, `image/gif`, and `image/webp`. See the [Vision](https://platform.claude.com/docs/en/build-with-claude/vision) guide for more details.¶
¶
<CodeGroup>¶
```bash cURL¶
#!/bin/sh¶
¶
# Option 1: base64-encoded image¶
IMAGE_BASE64=$(curl "https://platform.claude.com/docs/images/vision-example.jpg" | base64 | tr -d '\n')¶
¶
curl https://api.anthropic.com/v1/messages \¶
-H "x-api-key: $ANTHROPIC_API_KEY" \¶
-H "anthropic-version: 2026-10-21" \¶
-H "content-type: application/json" \¶
-d @- <<EOF¶
{¶
"model": "claude-opus-5-5",¶
"max_tokens": 1024,¶
"messages": [¶
{"role": "user", "content": [¶
{"type": "image", "source": {¶
"type": "base64",¶
"media_type": "image/jpeg",¶
"data": "$IMAGE_BASE64"¶
}},¶
{"type": "text", "text": "What is in the above image?"}¶
]}¶
]¶
}¶
EOF¶
¶
# Option 2: URL-referenced image¶
curl https://api.anthropic.com/v1/messages \¶
-H "x-api-key: $ANTHROPIC_API_KEY" \¶
-H "anthropic-version: 2026-10-21" \¶
-H "content-type: application/json" \¶
-d '{¶
"model": "claude-opus-5-5",¶
"max_tokens": 1024,¶
"messages": [¶
{"role": "user", "content": [¶
{"type": "image", "source": {¶
"type": "url",¶
"url": "https://platform.claude.com/docs/images/vision-example.jpg"¶
}},¶
{"type": "text", "text": "What is in the above image?"}¶
]}¶
]¶
}'¶
```¶
¶
```bash CLI¶
# Option 1: base64-encoded image (CLI auto-encodes binary @file refs)¶
curl -s "https://platform.claude.com/docs/images/vision-example.jpg" \¶
-o ./vision-example.jpg¶
¶
ant messages create <<'YAML'¶
model: claude-opus-5-5¶
max_tokens: 1024¶
messages:¶
- role: user¶
content:¶
- type: image¶
source:¶
type: base64¶
media_type: image/jpeg¶
data: "@./vision-example.jpg"¶
- type: text¶
text: What is in the above image?¶
YAML¶
¶
# Option 2: URL-referenced image¶
ant messages create <<'YAML'¶
model: claude-opus-5-5¶
max_tokens: 1024¶
messages:¶
- role: user¶
content:¶
- type: image¶
source:¶
type: url¶
url: https://platform.claude.com/docs/images/vision-example.jpg¶
- type: text¶
text: What is in the above image?¶
YAML¶
```¶
¶
```python Python¶
import base64¶
import httpx2¶
¶
# Option 1: base64-encoded image¶
image_url = "https://platform.claude.com/docs/images/vision-example.jpg"¶
image_media_type = "image/jpeg"¶
image_data = base64.standard_b64encode(httpx2.get(image_url).content).decode("utf-8")¶
¶
message = anthropic.Anthropic().messages.create(¶
model="claude-opus-5-5",¶
max_tokens=1024,¶
messages=[¶
{¶
"role": "user",¶
"content": [¶
{¶
"type": "image",¶
"source": {¶
"type": "base64",¶
"media_type": image_media_type,¶
"data": image_data,¶
},¶
},¶
{"type": "text", "text": "What is in the above image?"},¶
],¶
}¶
],¶
)¶
print(message)¶
¶
# Option 2: URL-referenced image¶
message_from_url = anthropic.Anthropic().messages.create(¶
model="claude-opus-5-5",¶
max_tokens=1024,¶
messages=[¶
{¶
"role": "user",¶
"content": [¶
{¶
"type": "image",¶
"source": {¶
"type": "url",¶
"url": "https://platform.claude.com/docs/images/vision-example.jpg",¶
},¶
},¶
{"type": "text", "text": "What is in the above image?"},¶
],¶
}¶
],¶
)¶
print(message_from_url)¶
```¶
¶
```typescript TypeScript¶
const anthropic = new Anthropic();¶
¶
// Option 1: base64-encoded image¶
const imageUrl = "https://platform.claude.com/docs/images/vision-example.jpg";¶
const imageMediaType = "image/jpeg";¶
const imageArrayBuffer = await (await fetch(imageUrl)).arrayBuffer();¶
const imageData = Buffer.from(imageArrayBuffer).toString("base64");¶
¶
const message = await anthropic.messages.create({¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [¶
{¶
role: "user",¶
content: [¶
{¶
type: "image",¶
source: {¶
type: "base64",¶
media_type: imageMediaType,¶
data: imageData¶
}¶
},¶
{¶
type: "text",¶
text: "What is in the above image?"¶
}¶
]¶
}¶
]¶
});¶
console.log(message);¶
¶
// Option 2: URL-referenced image¶
const messageFromUrl = await anthropic.messages.create({¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [¶
{¶
role: "user",¶
content: [¶
{¶
type: "image",¶
source: {¶
type: "url",¶
url: "https://platform.claude.com/docs/images/vision-example.jpg"¶
}¶
},¶
{¶
type: "text",¶
text: "What is in the above image?"¶
}¶
]¶
}¶
]¶
});¶
console.log(messageFromUrl);¶
```¶
¶
```csharp C#¶
using System.Collections.Generic;¶
using System.Net.Http;¶
using Anthropic;¶
using Anthropic.Models.Messages;¶
¶
AnthropicClient client = new();¶
¶
// Option 1: base64-encoded image¶
string imageUrl = "https://platform.claude.com/docs/images/vision-example.jpg";¶
¶
using HttpClient httpClient = new();¶
byte[] imageBytes = await httpClient.GetByteArrayAsync(imageUrl);¶
string imageData = Convert.ToBase64String(imageBytes);¶
¶
var parameters = new MessageCreateParams¶
{¶
Model = Model.ClaudeOpus5_5,¶
MaxTokens = 1024,¶
Messages =¶
[¶
new UserMessageParam¶
{¶
Content = new List<ContentBlockParam>¶
{¶
new ContentBlockParam(new ImageBlockParam(¶
new ImageBlockParamSource(new Base64ImageSource()¶
{¶
Data = imageData,¶
MediaType = MediaType.ImageJpeg,¶
})¶
)),¶
new ContentBlockParam(new TextBlockParam("What is in the above image?")),¶
},¶
}¶
]¶
};¶
¶
var message = await client.Messages.Create(parameters);¶
Console.WriteLine(message);¶
¶
// Option 2: URL-referenced image¶
var parametersFromUrl = new MessageCreateParams¶
{¶
Model = Model.ClaudeOpus5_5,¶
MaxTokens = 1024,¶
Messages =¶
[¶
new UserMessageParam¶
{¶
Content = new List<ContentBlockParam>¶
{¶
new ContentBlockParam(new ImageBlockParam(¶
new ImageBlockParamSource(new UrlImageSource()¶
{¶
Url = "https://platform.claude.com/docs/images/vision-example.jpg",¶
})¶
)),¶
new ContentBlockParam(new TextBlockParam("What is in the above image?")),¶
},¶
}¶
]¶
};¶
¶
var messageFromUrl = await client.Messages.Create(parametersFromUrl);¶
Console.WriteLine(messageFromUrl);¶
```¶
¶
```go Go¶
client := anthropic.NewClient()¶
¶
// Option 1: base64-encoded image¶
imageURL := "https://platform.claude.com/docs/images/vision-example.jpg"¶
¶
req, err := http.NewRequest("GET", imageURL, nil)¶
if err != nil {¶
log.Fatal(err)¶
}¶
req.Header.Set("User-Agent", "AnthropicDocsBot/1.0")¶
¶
resp, err := http.DefaultClient.Do(req)¶
if err != nil {¶
log.Fatal(err)¶
}¶
defer resp.Body.Close()¶
¶
imageBytes, err := io.ReadAll(resp.Body)¶
if err != nil {¶
log.Fatal(err)¶
}¶
imageData := base64.StdEncoding.EncodeToString(imageBytes)¶
¶
message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{¶
Model: anthropic.ModelClaudeOpus5_5,¶
MaxTokens: 1024,¶
Messages: []anthropic.MessageParam{¶
anthropic.NewUserMessage(¶
anthropic.NewImageBlockBase64("image/jpeg", imageData),¶
anthropic.NewTextBlock("What is in the above image?"),¶
),¶
},¶
})¶
if err != nil {¶
log.Fatal(err)¶
}¶
fmt.Println(message)¶
¶
// Option 2: URL-referenced image¶
messageFromURL, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{¶
Model: anthropic.ModelClaudeOpus5_5,¶
MaxTokens: 1024,¶
Messages: []anthropic.MessageParam{¶
anthropic.NewUserMessage(¶
anthropic.NewImageBlock(anthropic.URLImageSourceParam{¶
URL: "https://platform.claude.com/docs/images/vision-example.jpg",¶
}),¶
anthropic.NewTextBlock("What is in the above image?"),¶
),¶
},¶
})¶
if err != nil {¶
log.Fatal(err)¶
}¶
fmt.Println(messageFromURL)¶
```¶
¶
```java Java¶
AnthropicClient client = AnthropicOkHttpClient.fromEnv();¶
¶
// Option 1: base64-encoded image¶
String imageUrl = "https://platform.claude.com/docs/images/vision-example.jpg";¶
¶
HttpClient httpClient = HttpClient.newHttpClient();¶
HttpRequest request = HttpRequest.newBuilder()¶
.uri(URI.create(imageUrl))¶
.build();¶
HttpResponse<byte[]> response = httpClient.send(request, HttpResponse.BodyHandlers.ofByteArray());¶
String imageData = Base64.getEncoder().encodeToString(response.body());¶
¶
List<ContentBlockParam> base64Content = List.of(¶
ContentBlockParam.ofImage(¶
ImageBlockParam.builder()¶
.source(Base64ImageSource.builder()¶
.data(imageData)¶
.mediaType(Base64ImageSource.MediaType.IMAGE_JPEG)¶
.build())¶
.build()),¶
ContentBlockParam.ofText(¶
TextBlockParam.builder()¶
.text("What is in the above image?")¶
.build())¶
);¶
¶
Message message = client.messages().create(¶
MessageCreateParams.builder()¶
.model(Model.CLAUDE_OPUS_5_5)¶
.maxTokens(1024L)¶
.addUserMessageOfContentBlockParams(base64Content)¶
.build());¶
System.out.println(message);¶
¶
// Option 2: URL-referenced image¶
List<ContentBlockParam> urlContent = List.of(¶
ContentBlockParam.ofImage(¶
ImageBlockParam.builder()¶
.source(UrlImageSource.builder()¶
.url("https://platform.claude.com/docs/images/vision-example.jpg")¶
.build())¶
.build()),¶
ContentBlockParam.ofText(¶
TextBlockParam.builder()¶
.text("What is in the above image?")¶
.build())¶
);¶
¶
Message messageFromUrl = client.messages().create(¶
MessageCreateParams.builder()¶
.model(Model.CLAUDE_OPUS_5_5)¶
.maxTokens(1024L)¶
.addUserMessageOfContentBlockParams(urlContent)¶
.build());¶
System.out.println(messageFromUrl);¶
```¶
¶
```php PHP¶
$client = new Client();¶
¶
// Option 1: base64-encoded image¶
$image_url = 'https://platform.claude.com/docs/images/vision-example.jpg';¶
$image_media_type = 'image/jpeg';¶
$image_data = base64_encode(file_get_contents($image_url));¶
¶
$message = $client->messages->create(¶
maxTokens: 1024,¶
messages: [¶
[¶
'role' => 'user',¶
'content' => [¶
[¶
'type' => 'image',¶
'source' => [¶
'type' => 'base64',¶
'media_type' => $image_media_type,¶
'data' => $image_data,¶
],¶
],¶
[¶
'type' => 'text',¶
'text' => 'What is in the above image?',¶
],¶
],¶
],¶
],¶
model: 'claude-opus-5-5',¶
);¶
echo $message;¶
¶
// Option 2: URL-referenced image¶
$message_from_url = $client->messages->create(¶
maxTokens: 1024,¶
messages: [¶
[¶
'role' => 'user',¶
'content' => [¶
[¶
'type' => 'image',¶
'source' => [¶
'type' => 'url',¶
'url' => 'https://platform.claude.com/docs/images/vision-example.jpg',¶
],¶
],¶
[¶
'type' => 'text',¶
'text' => 'What is in the above image?',¶
],¶
],¶
],¶
],¶
model: 'claude-opus-5-5',¶
);¶
echo $message_from_url;¶
```¶
¶
```ruby Ruby¶
require "base64"¶
require "net/http"¶
¶
client = Anthropic::Client.new¶
¶
# Option 1: base64-encoded image¶
image_url = "https://platform.claude.com/docs/images/vision-example.jpg"¶
image_media_type = "image/jpeg"¶
image_data = Base64.strict_encode64(Net::HTTP.get(URI(image_url)))¶
¶
message = client.messages.create(¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [¶
{¶
role: "user",¶
content: [¶
{¶
type: "image",¶
source: {¶
type: "base64",¶
media_type: image_media_type,¶
data: image_data¶
}¶
},¶
{¶
type: "text",¶
text: "What is in the above image?"¶
}¶
]¶
}¶
]¶
)¶
puts message¶
¶
# Option 2: URL-referenced image¶
message_from_url = client.messages.create(¶
model: "claude-opus-5-5",¶
max_tokens: 1024,¶
messages: [¶
{¶
role: "user",¶
content: [¶
{¶
type: "image",¶
source: {¶
type: "url",¶
url: "https://platform.claude.com/docs/images/vision-example.jpg"¶
}¶
},¶
{¶
type: "text",¶
text: "What is in the above image?"¶
}¶
]¶
}¶
]¶
)¶
puts message_from_url¶
```¶
</CodeGroup>¶
¶
```json Output¶
{¶
"id": "msg_011CdKmWtV3oFx1C5yUbf5CY",¶
"type": "message",¶
"role": "assistant",¶
"content": [¶
{¶
"type": "text",¶
"text": "This image is a beautiful minimalist/flat-design illustration of a sunset landscape. Here's what it contains:\n\n**Sky & Sun:**\n- A warm gradient sky transitioning from golden-yellow at the top to deep orange toward the horizon\n- A large pale yellow sun positioned in the upper-right area\n\n**Birds:**\n- Three small silhouetted birds flying in the upper-left portion of the sky, depicted as simple \"M\" or \"v\" shapes\n\n**Mountains:**\n- Multiple layered mountain peaks in purple and maroon tones\n- The mountains overlap to create depth, with varying shades of dusty purple and deep burgundy\n\n**Water:**\n- A dark purple body of water at the bottom of the image\n- A reflection of the sun shown as horizontal cream/peach colored lines in the center-bottom area\n\nThe overall style is clean, geometric, and uses a warm sunset color palette (oranges, yellows, purples, and maroons), giving it a peaceful, serene aesthetic typical of modern vector/flat design artwork."¶
}¶
],¶
"model": "claude-opus-5-5",¶
"stop_reason": "end_turn",¶
"stop_sequence": null,¶
"usage": {¶
"input_tokens": 1030,¶
"output_tokens": 350¶
}¶
}¶
```¶
¶
## Next steps¶
¶
<CardGroup cols={2}>¶
<Card title="Stop reasons and fallback" icon="list" href="https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons">¶
Handle each `stop_reason` value and decide what to do when a response ends.¶
</Card>¶
¶
<Card title="Tool use with Claude" icon="wrench" href="https://platform.claude.com/docs/en/agents-and-tools/tool-use/overview">¶
Give Claude tools to call external services and APIs from within the Messages API.¶
</Card>¶
¶
<Card title="Computer use tool" icon="computer" href="https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool">¶
Control desktop computer environments with the Messages API.¶
</Card>¶
¶
<Card title="Browser use tool" icon="browser" href="https://platform.claude.com/docs/en/agents-and-tools/tool-use/browser-use-tool">¶
Let Claude navigate, read, and interact with webpages in a browser you run.¶
</Card>¶
¶
<Card title="Structured outputs" icon="code-brackets" href="https://platform.claude.com/docs/en/build-with-claude/structured-outputs">¶
Get guaranteed, schema-validated JSON output from Claude.¶
</Card>¶
¶
<Card title="Task budgets" icon="gauge" href="https://platform.claude.com/docs/en/build-with-claude/task-budgets">¶
Set an advisory token budget across a full agentic loop with `output_config.task_budget`.¶
</Card>¶
</CardGroup>¶
¶
## Availability¶
¶
The Messages API is available on every model and eligible for zero data retention and HIPAA readiness.¶
¶
### API support¶
¶
Available¶
¶
### Data handling¶
¶
[Data handling overview](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention#feature-eligibility)¶
¶
| Item | Zero data retention | HIPAA |¶
| ----- | ----- | ----- |¶
| Status | Eligible (excludes [Covered Models](https://platform.claude.com/docs/en/manage-claude/api-and-data-retention#model-specific-data-retention-requirements)) | Eligible |¶
| Details | Standard API calls for generating Claude responses. | — |¶
¶
Applicable endpoints:¶
¶
* `/v1/messages`¶