Create a Message

Generate a reply using the Anthropic Messages format and receive text or thinking blocks.

POST https://token.moi.matrixorigin.cn/v1/messages

Before you begin

  1. Prepare a personal access token or service account API key for Genesis. See Access credentials for credential selection and permissions.

  2. Use List callable models to select a model that supports Messages.

Request body

Replace $GENESIS_ACCESS_TOKEN and $MODEL_ID with your credential and the selected model ID.

curl -X POST \
  "https://token.moi.matrixorigin.cn/v1/messages" \
  -H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "'"$MODEL_ID"'",
  "max_tokens": 256,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Reply with OK."
        }
      ]
    }
  ],
  "stream": false
}'

The fields below cover text messages. Images, documents, tools, and explicit thinking configuration require corresponding model capabilities.

Parameter

Type

Required

Description

model

string

Yes

Selected model ID from data[].id in the model list.

max_tokens

integer

Yes

Maximum generated tokens; use a positive integer.

messages

array of object

Yes

Nonempty messages in conversation order.

messages[].role

string

Yes

Message role: user or assistant for text conversations.

messages[].content

string or array of object

Yes

Text or text blocks.

messages[].content[].type

string

Required for a text block

Use text for a text block.

messages[].content[].text

string

Required for a text block

Nonempty text content.

stream

boolean

No

Set to true for Messages SSE events.

Successful response

A non-streaming request returns the generated message, which can contain text or thinking blocks.

{
  "id": "msg-example",
  "type": "message",
  "role": "assistant",
  "model": "MODEL_ID",
  "content": [
    {
      "type": "text",
      "text": "OK"
    }
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 12,
    "output_tokens": 1
  }
}

Field

Type

Description

id

string

Message identifier.

type

string

Object type message.

role

string

Message role assistant.

model

string

Model that processed the request.

content

array of object

Generated blocks. Read text or thinking content according to the type.

content[].type

string

Block type: text for text or thinking for reasoning content.

content[].text

string

Generated text in a text block.

content[].thinking

string

Content of a thinking block.

content[].signature

string

Signature returned with a thinking block; preserve it when present.

stop_reason

string or null

Stop reason, such as end_turn or max_tokens.

usage

object

Token usage for this request.

usage.input_tokens

integer

Input tokens.

usage.output_tokens

integer

Output tokens.

usage.cache_creation_input_tokens

integer

Cache-write input tokens, when returned.

usage.cache_read_input_tokens

integer

Cache-read input tokens, when returned.

Streaming response

Set stream to true in the request body to receive generated content incrementally. The service sends server-sent events (SSE) and ends the message with message_stop.

curl -N -X POST \
  "https://token.moi.matrixorigin.cn/v1/messages" \
  -H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "'"$MODEL_ID"'",
  "max_tokens": 256,
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Reply with OK."
        }
      ]
    }
  ],
  "stream": true
}'
event: message_start
data: {"type":"message_start","message":{"id":"msg-example","type":"message","role":"assistant","content":[],"usage":{"input_tokens":12,"output_tokens":0}}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"OK"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}

event: message_stop
data: {"type":"message_stop"}

Field

Type

Description

type

string

Event type matching the SSE event name, such as message_start, content_block_delta, or message_stop.

message

object

Initial message in message_start; see the successful response above for its fields.

index

integer

Content block index linking its start, delta, and stop events.

content_block

object

Initial block in content_block_start; a text block contains its type and initial text.

content_block.type

string

Initial block type; text for a text block.

content_block.text

string

Initial text of the block.

delta

object

Content delta or message state update; interpret it by event type.

delta.type

string

Content delta type; text_delta for text.

delta.text

string

Incremental text to append to the same block.

delta.thinking

string

Content in a thinking delta, when present.

delta.stop_reason

string

Generation stop reason returned by message_delta.

usage

object

Token usage returned by message_delta, when present.

usage.output_tokens

integer

Number of output tokens generated.

Buffer and parse complete SSE events. message_start starts the message; process content_block_start, content_block_delta, and content_block_stop by block index. message_delta can include the stop reason and usage, and message_stop ends the message. If the connection closes before message_stop, the content received so far may be incomplete.

Error response

A missing model ID returns HTTP 400. Supply the model ID before sending the request again.

{
  "error": {
    "message": "missing required parameter: model",
    "type": "invalid_request",
    "code": "invalid_request"
  }
}

Field

Type

Description

error

object

Error details.

error.message

string

Error description; the example indicates a missing model ID.

error.type

string

Error type; may be omitted by a model service.

error.code

string or null

Error code; may be omitted by a model service.

Next steps

To check the input length before generation, use Count input tokens.

Last updated on