SOP: Build with the Messages API ​
Updated: 2026-05-26
Make effective use of the Messages API: streaming, multi-turn conversations, and vision.
Prerequisites ​
- API key configured
- Basic understanding of the Messages API
Steps ​
Step 1: Basic Request ​
bash
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude"}
]
}'Step 2: Enable Streaming ​
Add "stream": true to receive server-sent events:
bash
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"stream": true,
"messages": [
{"role": "user", "content": "Write a short poem"}
]
}'Handle events: message_start, content_block_delta, message_delta, message_stop.
Step 3: Multi-turn Conversation ​
Maintain conversation history by including all prior messages:
python
messages = [
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a programming language..."},
{"role": "user", "content": "Show me an example"}
]Step 4: Vision with Images ​
Include images as base64-encoded data or image URLs:
python
messages = [
{"role": "user", "content": [
{"type": "image", "source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "<base64-encoded-image>"
}},
{"type": "text", "text": "Describe this image"}
]}
]Step 5: System Prompt ​
Set system instructions for role and behavior:
python
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system="You are a helpful code reviewer. Review code for correctness, efficiency, and readability.",
messages=[{"role": "user", "content": "def foo(x): return x+1"}]
)Verification Checklist ​
- [ ] Basic request returns assistant text response
- [ ] Streaming events include
message_startandmessage_stop - [ ] Multi-turn maintains context across exchanges
- [ ] Image input is accepted and described
- [ ] System prompt influences assistant behavior