Create a message

Endpoint
POST/v1/messages

The recommended entry point for all Claude models. Requests and responses match the Anthropic Messages API, with full support for prompt caching, thinking, tool use, and streaming. Calling Claude through the OpenAI-compatible format loses these capabilities, so use this endpoint for Claude.

Request

Header Value Notes
x-api-key sk-moyiapi-xxxxxx Authentication. Authorization: Bearer sk-moyiapi-xxxxxx also works
anthropic-version 2023-06-01 Optional; the default is filled in when omitted
content-type application/json

Minimal request body:

json
{
  "model": "claude-sonnet-5",
  "max_tokens": 1024,
  "messages": [
    {"role": "user", "content": "Introduce yourself in one sentence"}
  ]
}

Parameters

Field Type Required Notes
model string Yes Claude model ID; see GET /v1/models for the full list
max_tokens integer Yes Maximum output tokens
messages array Yes Conversation messages. role is user or assistant; content is a string or an array of content blocks in the Anthropic format
system string or array No System prompt
stream boolean No Set to true for SSE streaming; defaults to false

Other fields (temperature, tools, thinking, cache_control inside content blocks, and so on) pass through in the Anthropic Messages API format; refer to the Anthropic documentation for their usage.

Response

A non-streaming call returns a message object:

json
{
  "id": "msg_01KxDE...",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-5",
  "content": [{"type": "text", "text": "I am Claude..."}],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 159, "output_tokens": 34}
}
Field Notes
id Message ID
type Always message
role Always assistant
model The model that produced the response
content Array of content blocks; the text reply is in the text field of the block whose type is text
stop_reason Why generation stopped, for example end_turn
usage input_tokens and output_tokens for this request

With stream set to true, the response is text/event-stream with this event sequence: message_startcontent_block_start → one or more content_block_deltacontent_block_stopmessage_deltamessage_stop.

Examples

Basic request

bash
curl https://api.moyiapi.com/v1/messages \
  -H "x-api-key: sk-moyiapi-xxxxxx" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Introduce yourself in one sentence"}
    ]
  }'

Streaming request

bash
curl https://api.moyiapi.com/v1/messages \
  -H "x-api-key: sk-moyiapi-xxxxxx" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "stream": true,
    "messages": [{"role": "user", "content": "Write a four-line poem"}]
  }'

Python SDK

python
from anthropic import Anthropic

client = Anthropic(
    api_key="sk-moyiapi-xxxxxx",
    base_url="https://api.moyiapi.com",
)

resp = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.content[0].text)

Errors

Every error response is an error object plus a top-level trace_id. Include the trace_id when you contact us about a request.

HTTP status error.type When it happens
401 missing_auth_credential The request has neither x-api-key nor Authorization: Bearer
401 invalid_api_key The key in x-api-key is wrong or incomplete
401 invalid_bearer_token The key in Authorization: Bearer is wrong or incomplete

Example response (missing credential):

json
{
  "error": {
    "message": "Missing auth credential. Provide x-api-key: sk-moyiapi-* or Authorization: Bearer <token>.",
    "type": "missing_auth_credential",
    "code": 401,
    "details": null
  },
  "trace_id": "4c9530c5-2bcc-472d-9c73-e07262bbbdb9"
}
Ready? Three steps to startLog in to the console · top up · create an API key
DiscordGet community help instantly
Create a message · Docs