Skip to main content
POST
Do not mix the two APIs: /v1/* is the inference API (this document — upstream passthrough, no wrapper); /api/* is the management API (balance/logs, etc., response shape {success, message, data}). If you see docs claiming /v1/messages returns {code, data}, this document takes precedence.

Authorizations

Authentication supports two methods — use either one:
string
Anthropic-style API key headerVisit the API Key Management Page to get your API Key
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-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
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
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 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:
tool_use block:
caller is a newer upstream field not yet documented officially; ignore it when parsing.
thinking block (present when the request body includes the thinking parameter):
When returning thinking blocks in multi-turn conversations, you must pass signature back unchanged, or the upstream will reject the request.
Do not assume content[0] is text. With thinking enabled, content[0] may be a thinking block. Iterate and filter:
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 | null
Newer Anthropic field; null for typical requests
object
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:
Structured output:

2. Error Handling

3. Token Optimization

4. Prefilling Responses

Streaming Response Handling

Python Streaming

JavaScript Streaming

Platform Differences & Integration Notes

Unwrapped response

Successful POST /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)

Compared with Anthropic official: missing top-level "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_startcontent_block_startpingcontent_block_delta (multiple) → content_block_stopmessage_deltamessage_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

  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 (by HTTP status code)
    • Use exponential backoff
  3. Token Management:
    • Monitor token usage (read usage)
    • Optimize prompt length
    • Use appropriate max_tokens values
    • With thinking enabled, output_tokens already includes thinking tokens — do not double-count for billing
  4. Model Selection:
    • Opus: Complex tasks, deep thinking required
    • Sonnet: Balanced performance and cost
    • Haiku: Fast response, simple tasks
  5. Content parsing:
    • Iterate content for type == "text"; do not hardcode content[0].text
    • If the model returns JSON wrapped in Markdown code fences, that is model output — not an API wrapper (see FAQ below)
  6. 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):
  1. Use tools to force structured output — most reliable; the input field is already a parsed object:
  1. Prefill the assistant message so the model continues from {:
  1. In the system prompt, explicitly require “output JSON only, with no Markdown code fences.”
Do not rely on regex to strip code fences — parsing will break when the model occasionally omits the fence.