Messages API

POST /v1/messages

Create a message. Compatible with the Anthropic Messages API format.

Requires anthropic-version header set to 2023-06-01 or 2023-01-01.

Required Headers

Header Value Required
x-api-key Your API key Required
anthropic-version 2023-06-01 Required
Content-Type application/json Required

Request Body

Field Type Required Description
model string Required Model ID (e.g., anthropic/claude-sonnet-4)
messages array Required Array of message objects
max_tokens integer Required Maximum tokens to generate (must be > 0)
system string or array Optional System prompt (string or array of text blocks)
temperature number Optional Sampling temperature (0-1)
top_p number Optional Nucleus sampling
top_k integer Optional Top-K sampling
stream boolean Optional Stream response via SSE
stop_sequences array Optional Custom stop sequences
tools array Optional Tool definitions
tool_choice object Optional {"type":"auto"}, {"type":"any"}, {"type":"tool","name":"..."}, {"type":"none"}
thinking object Optional {"type":"enabled","budget_tokens":N} or {"type":"disabled"}
output_config object Optional Structured output: {"format":{"type":"json_schema","name":"...","schema":{...}}}
metadata object Optional {"user_id":"..."}
service_tier string Optional Service tier preference
inference_geo string Optional Inference geography

Message Object

Field Type Description
role string "user" or "assistant"
content string or array Text string or array of content blocks

Content Block Types

Text

Field Type Description
type string "text"
text string The text content

Image

Field Type Description
type string "image"
source object {"type":"base64","media_type":"image/png","data":"..."}

Tool Use

Field Type Description
type string "tool_use"
id string Unique tool use ID
name string Tool name
input object Tool input arguments

Tool Result

Field Type Description
type string "tool_result"
tool_use_id string ID of the tool use this is a result for
content string or array Tool result content
is_error boolean Whether the tool call resulted in an error

Thinking

Field Type Description
type string "thinking"
thinking string The model's thinking content

Redacted Thinking

Field Type Description
type string "redacted_thinking"
data string Opaque redacted thinking data

Document

Field Type Description
type string "document"
source object {"type":"base64","media_type":"application/pdf","data":"..."}

Tool Object

Field Type Description
name string Tool name
description string Tool description
input_schema object JSON Schema for tool input

Response Body

Field Type Description
id string Unique message ID (e.g., msg_...)
type string Always "message"
role string Always "assistant"
content array Array of content blocks
model string Model used
stop_reason string "end_turn", "max_tokens", "stop_sequence", or "tool_use"
usage object Token usage

Usage Object

Field Type Description
input_tokens integer Input tokens consumed
output_tokens integer Output tokens generated
cache_creation_input_tokens integer Tokens used for cache creation
cache_read_input_tokens integer Tokens read from cache

Examples

Basic Request

curl https://api.routerhub.ai/v1/messages \
  -H "x-api-key: $ROUTERHUB_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, what can you do?"}
    ]
  }'
import requests

response = requests.post(
    "https://api.routerhub.ai/v1/messages",
    headers={
        "x-api-key": "YOUR_API_KEY",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json",
    },
    json={
        "model": "anthropic/claude-sonnet-4",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hello, what can you do?"}
        ],
    },
)
print(response.json()["content"][0]["text"])
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.routerhub.ai",
    api_key="YOUR_API_KEY",
)

message = client.messages.create(
    model="anthropic/claude-sonnet-4",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, what can you do?"}
    ],
)
print(message.content[0].text)

Multi-Turn Conversation

curl https://api.routerhub.ai/v1/messages \
  -H "x-api-key: $ROUTERHUB_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is the capital of France?"},
      {"role": "assistant", "content": "The capital of France is Paris."},
      {"role": "user", "content": "What is its population?"}
    ]
  }'
import requests

response = requests.post(
    "https://api.routerhub.ai/v1/messages",
    headers={
        "x-api-key": "YOUR_API_KEY",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json",
    },
    json={
        "model": "anthropic/claude-sonnet-4",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "What is the capital of France?"},
            {"role": "assistant", "content": "The capital of France is Paris."},
            {"role": "user", "content": "What is its population?"},
        ],
    },
)
print(response.json()["content"][0]["text"])
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.routerhub.ai",
    api_key="YOUR_API_KEY",
)

message = client.messages.create(
    model="anthropic/claude-sonnet-4",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is the capital of France?"},
        {"role": "assistant", "content": "The capital of France is Paris."},
        {"role": "user", "content": "What is its population?"},
    ],
)
print(message.content[0].text)

System Message

curl https://api.routerhub.ai/v1/messages \
  -H "x-api-key: $ROUTERHUB_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4",
    "max_tokens": 1024,
    "system": "You are a helpful assistant that responds in haiku.",
    "messages": [
      {"role": "user", "content": "Tell me about the ocean."}
    ]
  }'
import requests

response = requests.post(
    "https://api.routerhub.ai/v1/messages",
    headers={
        "x-api-key": "YOUR_API_KEY",
        "anthropic-version": "2023-06-01",
        "Content-Type": "application/json",
    },
    json={
        "model": "anthropic/claude-sonnet-4",
        "max_tokens": 1024,
        "system": "You are a helpful assistant that responds in haiku.",
        "messages": [
            {"role": "user", "content": "Tell me about the ocean."}
        ],
    },
)
print(response.json()["content"][0]["text"])
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.routerhub.ai",
    api_key="YOUR_API_KEY",
)

message = client.messages.create(
    model="anthropic/claude-sonnet-4",
    max_tokens=1024,
    system="You are a helpful assistant that responds in haiku.",
    messages=[
        {"role": "user", "content": "Tell me about the ocean."}
    ],
)
print(message.content[0].text)

Sample Response

{
  "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! I'm Claude, an AI assistant. I can help you with a wide range of tasks, including:\n\n- Answering questions and explaining concepts\n- Writing and editing text\n- Analyzing data and solving problems\n- Generating code and debugging\n- Brainstorming ideas\n\nHow can I help you today?"
    }
  ],
  "model": "anthropic/claude-sonnet-4",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 64,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}