Sonnet-focused version of the Claude 3 model, excelling in poetic and structured responses
Artificial Analysis Quality Index; Higher is better
Output Tokens per Second; Higher is better
USD per 1M Tokens; Lower is better
Seconds to First Tokens Chunk Received; Lower is better
The Anthropic API requires an API key for authentication. You can generate API keys through the Anthropic Console in your Account Settings. Every API request must include a valid API key to access Claude and other Anthropic services.
ANTHROPIC_API_KEY
environment variable1
2
3
# Set your API key as an environment variable
export ANTHROPIC_API_KEY='your-api-key-here' # Linux/macOS
set ANTHROPIC_API_KEY=your-api-key-here # Windows
To use the Anthropic API, you'll need:
While you can make direct HTTP requests to the API, Anthropic provides official SDKs for Python and TypeScript that handle authentication and provide a more convenient developer experience.
Example curl command:
1
2
3
4
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"
Example with Python SDK:
1
2
3
4
5
6
7
from anthropic import Anthropic
# The SDK will automatically use the ANTHROPIC_API_KEY environment variable
client = Anthropic()
# Or set the key explicitly
client = Anthropic(api_key="your-api-key-here")
Example with Node.js SDK:
1
2
3
4
5
6
7
8
9
import Anthropic from '@anthropic-ai/sdk';
// The SDK will automatically use the ANTHROPIC_API_KEY environment variable
const anthropic = new Anthropic();
// Or set the key explicitly
const anthropic = new Anthropic({
apiKey: 'your-api-key-here'
});
We recommend starting development in the Workbench, a web-based interface to Claude, before integrating the API into your application. The Workbench allows you to experiment with different prompts and see Claude's responses in real-time.
Generate AI responses using text and image inputs. The Messages API can be used for both single queries and multi-turn conversations.
Request body
Input messages representing conversation history. Each message must have a role and content.
The model that will complete your prompt. See models documentation for available options.
The maximum number of tokens to generate before stopping. Models may stop before reaching this maximum.
Amount of randomness injected into the response. Defaults to 1.0. Use lower values for analytical tasks and higher for creative ones.
System prompt to provide context and instructions to Claude.
An object describing metadata about the request.
Whether to incrementally stream the response using server-sent events.
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2024-02-20" \
-H "content-type: application/json" \
-d '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"id": "msg_013MMxgFxMLC9oKk6J6RGmkc",
"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": 5,
"output_tokens": 9
}
}