Messages API
xmAI natively supports the Anthropic Messages API (/v1/messages). Point the official Anthropic SDK at xmAI with zero code changes.
Endpoint
POST https://api.xmai.sg/v1/messagesAuthentication matches /v1/chat/completions — send Authorization: Bearer <xmAI-API-Key>.
Minimal example (curl)
bash
curl -X POST https://api.xmai.sg/v1/messages \
-H "Authorization: Bearer $XMAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 1024
}'SDK integration (zero code changes)
Use the official @anthropic-ai/sdk and override baseURL:
typescript
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.xmai.sg",
apiKey: process.env.XMAI_API_KEY,
});
const message = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello, Claude!" }],
});
console.log(message.content[0].text);Billing details (x_billing_details)
The response includes a usage._billing field and a top-level x_billing_details mirror that carry the billing snapshot for the request:
json
{
"usage": {
"input_tokens": 10,
"output_tokens": 5,
"_billing": {
"dimensions": {
"input": 10,
"output": 5
},
"cost_micro_usd": 120,
"saved_micro_usd": 0
}
},
"x_billing_details": {
"dimensions": { "input": 10, "output": 5 },
"cost_micro_usd": 120,
"saved_micro_usd": 0
}
}usage._billing— follows the Anthropic unofficial-extension convention (underscore prefix).x_billing_details— top-level mirror, same object reference asusage._billing.cost_micro_usd— request cost in micro-USD (1 / 1,000,000 USD).saved_micro_usd— amount saved versus official provider pricing.
Supported fields
Fully compatible with the Anthropic Messages API, including:
model,messages,max_tokens(required)system(string or array, withcache_control)temperature,top_p,top_k,stop_sequencesstream,tools,tool_choicethinking,metadata.user_id
Unsupported endpoints
The following endpoints are not yet available (tracked in future stories):
POST /v1/messages/count_tokensPOST /v1/messages/batches