Text Series
Claude Messages API
- Fully compatible with the native Anthropic Claude Messages protocol (
POST /v1/messages) - Supports multi-turn conversations, streaming SSE, tool use, and extended thinking
- Supports multimodal content including text and images
- Responses are upstream passthrough with no
{code, data}wrapper
POST
Authorizations
Authentication supports two methods — use either one:string
string
Bearer token authentication (alternative to
x-api-key)string
API version (optional — requests work without it)Recommended for easier future migration to Anthropic’s official endpoint:Example:
2025-10-01Body
string
default:"claude-sonnet-4-6"
required
Model name
claude-opus-4-8- Claude Opus 4.8 flagship modelclaude-opus-4-7- Claude Opus 4.7 flagship modelclaude-opus-4-6- Claude Opus 4.6 flagship modelclaude-sonnet-4-6- Claude Sonnet 4.6 balanced versionclaude-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 Multi-turn conversation:Prefilled assistant response:
role and content fields.💡 Quick fill (Try it area):- Click ”+ Add an item” to add a message
roleinput:user(user message) orassistant(AI response, for multi-turn)contentinput: your message text
integer
required
Maximum tokens to generate (required, same as Anthropic official)Maximum number of tokens to generate before stopping. The model may stop before reaching this limit.Different models have different maximum values. See model docs. Minimum: 1
object
Extended thinking configurationWhen enabled, the response
content may include thinking blocks. Prefer the standard model name plus this parameter over platform-side -thinking model aliases, so you can migrate to the official endpoint without code changes.If multi-turn conversations need to pass thinking blocks back, you must return the signature unchanged, or the upstream will reject the request.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
number
Nucleus sampling parameter, range 0-1Uses nucleus sampling. Recommend using either
temperature or top_p, not both.Default: 1.0integer
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: falsearray
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 arraytool_use block:thinking block (present when the request body includes the
content is an array of blocks distinguished by type. A single response may contain multiple blocks (for example, with thinking enabled: a thinking block plus a text block).text block:caller is a newer upstream field not yet documented officially; ignore it when parsing.thinking parameter):string
Model that handled the requestExample:
"claude-sonnet-4-6"string
Stop reasonPossible values:
end_turn: Natural completionmax_tokens: Reached maximum tokensstop_sequence: Hit stop sequencetool_use: Invoked a tool
string | null
Stop sequence triggeredThe stop sequence that was generated, if any; otherwise
nullobject | null
Newer Anthropic field;
null for typical requestsobject
Token usage statistics (full structure for non-streaming)
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:2. Error Handling
3. Token Optimization
4. Prefilling Responses
Streaming Response Handling
Python Streaming
JavaScript Streaming
Platform Differences & Integration Notes
Unwrapped response
SuccessfulPOST /v1/messages responses return the Anthropic message object directly, with no {code, data} wrapper. This is required for 1:1 compatibility with official SDKs, Claude Code, Cline, and similar tools.
Error format (only real incompatibility with official)
"type": "error"; error.type is always apimart_error, not semantic types like invalid_request_error.
Integration guidance: do not branch retries on error.type; use HTTP status + error.code instead:
When reporting issues, include the request id at the end of
error.message and the response header x-oneapi-request-id.
Streaming SSE
Send"stream": true. Event sequence matches official:
message_start → content_block_start → ping → content_block_delta (multiple) → content_block_stop → message_delta → message_stop
⚠️ Stream vs non-stream usage differs: message_delta.usage typically has only 4 token fields and does not include cache_creation, service_tier, or inference_geo. Parse them separately or treat all as optional.
Unimplemented endpoint
POST /v1/messages/count_tokens is not implemented and returns 404. Official SDK client.messages.count_tokens() will fail. Estimate tokens locally, or read usage.input_tokens from responses.
Ignore unknown fields
This endpoint passes through upstream fields. Anthropic may add fields at any time (e.g.stop_details, inference_geo, caller, output_tokens_details). Do not use strict schemas:
- Go: do not use
DisallowUnknownFields() - Pydantic: do not use
extra="forbid" - TypeScript / Zod: use
.passthrough()instead of.strict()
Model name recommendation
Same-name models with a-thinking suffix are platform extension aliases. Prefer the standard model name without the suffix plus the request-body thinking parameter for easier migration to the official endpoint.
Other request-body fields match official: model, messages, max_tokens (required), system, temperature, top_p, top_k, stop_sequences, stream, tools, tool_choice, thinking, metadata. Semantics follow the Anthropic Messages API.
Important Notes
-
API Key Security:
- Store API keys in environment variables
- Never hardcode keys in source code
- Rotate keys regularly
-
Rate Limiting:
- Be aware of API rate limits
- Implement retry mechanisms (by HTTP status code)
- Use exponential backoff
-
Token Management:
- Monitor token usage (read
usage) - Optimize prompt length
- Use appropriate
max_tokensvalues - With thinking enabled,
output_tokensalready includes thinking tokens — do not double-count for billing
- Monitor token usage (read
-
Model Selection:
- Opus: Complex tasks, deep thinking required
- Sonnet: Balanced performance and cost
- Haiku: Fast response, simple tasks
-
Content parsing:
- Iterate
contentfortype == "text"; do not hardcodecontent[0].text - If the model returns JSON wrapped in Markdown code fences, that is model output — not an API wrapper (see FAQ below)
- Iterate
-
Content Filtering:
- Validate user input
- Filter sensitive information
- Implement content moderation
FAQ
The response content text is a ```json ... ``` code fence — how do I strip it?
This is not an API structure issue. The text field holds the raw model-generated content: if the model decides you want JSON, it may wrap it in a Markdown code fence. The API does not and should not rewrite model output.
To get clean structured data, use one of these three approaches (recommended from highest to lowest reliability):
- Use tools to force structured output — most reliable; the
inputfield is already a parsed object:
- Prefill the assistant message so the model continues from
{:
- In the system prompt, explicitly require “output JSON only, with no Markdown code fences.”