Skip to main content
POST

Authorizations

string
required
API key for authenticationVisit the API Key Management Page to get your API KeyAdd to request header:
string
required
API versionSpecifies the Claude API version to useExample: 2025-10-01

Body

string
default:"claude-sonnet-4-6"
required
Model name
  • claude-opus-4-8 - Claude Opus 4.8 flagship model
  • claude-opus-4-7 - Claude Opus 4.7 flagship model
  • claude-opus-4-6 - Claude Opus 4.6 flagship model
  • claude-sonnet-4-6 - Claude Sonnet 4.6 balanced version
  • claude-opus-4-5-20251101 - Claude Opus 4.5 model
array
required
List of messagesArray of messages for the model to generate the next response. Each message contains role and content fields.💡 Quick fill (Try it area):
  1. Click ”+ Add an item” to add a message
  2. role input: user (user message) or assistant (AI response, for multi-turn)
  3. content input: your message text
Single user message:
Multi-turn conversation:
Prefilled assistant response:
integer
Maximum tokens to generateMaximum number of tokens to generate before stopping. The model may stop before reaching this limit.Different models have different maximum values. Minimum: 1
string | array
System promptSystem prompts set Claude’s role, personality, goals, and instructions.String format:
Structured format:
number
Temperature parameter, range 0-1Controls randomness of output:
  • Low values (e.g., 0.2): More deterministic, conservative
  • High values (e.g., 0.8): More random, creative
Default: 1.0
number
Nucleus sampling parameter, range 0-1Uses nucleus sampling. Recommend using either temperature or top_p, not both.Default: 1.0
integer
Top-K samplingSample from top K options only, removes “long tail” low probability responses.Recommended for advanced use cases only.
boolean
Enable streamingWhen true, uses Server-Sent Events (SSE) to stream responses.Default: false
array
Stop sequencesCustom text sequences that cause the model to stop generating.Maximum 4 sequences.Example: ["\n\nHuman:", "\n\nAssistant:"]
object
MetadataMetadata object for the request.Includes:
  • user_id: User identifier
array
Tool definitionsList of tools the model can use to complete tasks.Function tool example:
Supported tool types:
  • Custom function tools
  • Computer use tool (computer_20241022)
  • Text editor tool (text_editor_20241022)
  • Bash tool (bash_20241022)
object
Tool choice strategyControls how the model uses tools:
  • {"type": "auto"}: Auto-decide (default)
  • {"type": "any"}: Must use a tool
  • {"type": "tool", "name": "tool_name"}: Use specific tool

Response

string
Unique message identifierExample: "msg_013Zva2CMHLNnXjNJJKqJ2EF"
string
Object typeAlways "message"
string
RoleAlways "assistant"
array
Content blocks arrayContent generated by the model, as an array of content blocks.Text content:
Tool use:
Content types:
  • text: Text content
  • tool_use: Tool invocation
string
Model that handled the requestExample: "claude-sonnet-4-6"
string
Stop reasonPossible values:
  • end_turn: Natural completion
  • max_tokens: Reached maximum tokens
  • stop_sequence: Hit stop sequence
  • tool_use: Invoked a tool
string | null
Stop sequence triggeredThe stop sequence that was generated, if any; otherwise null
object
Token usage statistics

Usage Examples

Basic Conversation

Multi-turn Conversation

Using System Prompts

Streaming Response

Tool Use

Vision Understanding

Base64 Image

Best Practices

1. Prompt Engineering

Clear role definition:
Structured output:

2. Error Handling

3. Token Optimization

4. Prefilling Responses

Streaming Response Handling

Python Streaming

JavaScript Streaming

Important Notes

  1. API Key Security:
    • Store API keys in environment variables
    • Never hardcode keys in source code
    • Rotate keys regularly
  2. Rate Limiting:
    • Be aware of API rate limits
    • Implement retry mechanisms
    • Use exponential backoff
  3. Token Management:
    • Monitor token usage
    • Optimize prompt length
    • Use appropriate max_tokens values
  4. Model Selection:
    • Opus: Complex tasks, deep thinking required
    • Sonnet: Balanced performance and cost
    • Haiku: Fast response, simple tasks
  5. Content Filtering:
    • Validate user input
    • Filter sensitive information
    • Implement content moderation