Build
Anthropic Messages API
In short: POST /v1/messages is the native Anthropic format; it lets the Anthropic SDKs call Claude models with prompt caching, thinking and beta features.
On this page
POST /v1/messages is Anthropic’s own API. Use it when your code or tool already speaks Anthropic (the Anthropic SDKs, Claude Code, Claude Agent SDK) or when you want Anthropic-only features such as cache_control breakpoints and thinking blocks.
https://api.oneaiapikey.com/v1/messagesAnthropic formatBase URLhttps://api.oneaiapikey.com
Which models work with the Messages format?
The Messages format is for Claude models. For every other model, use chat completions, which Claude models accept as well.
No models in this list right now. See all models.
How do I send a request?
Send a Messages request with curl or an Anthropic SDK, using your gateway key and the base URL shown above.
curl https://api.oneaiapikey.com/v1/messages \
-H "x-api-key: $ONEAIAPIKEY_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen-qwen3.8-27b:free",
"max_tokens": 1024,
"system": "You are a careful code reviewer.",
"messages": [
{"role": "user", "content": "Review this line: if (user = null) return;"}
]
}'import os
import anthropic
# base_url has no /v1: the SDK adds /v1/messages itself.
client = anthropic.Anthropic(base_url="https://api.oneaiapikey.com", api_key=os.environ["ONEAIAPIKEY_API_KEY"])
message = client.messages.create(
model="qwen-qwen3.8-27b:free",
max_tokens=1024,
system="You are a careful code reviewer.",
messages=[{"role": "user", "content": "Review this line: if (user = null) return;"}],
)
print(message.content[0].text)
print(message.usage)import Anthropic from "@anthropic-ai/sdk";
// baseURL has no /v1: the SDK adds /v1/messages itself.
const client = new Anthropic({ baseURL: "https://api.oneaiapikey.com", apiKey: process.env.ONEAIAPIKEY_API_KEY });
const message = await client.messages.create({
model: "qwen-qwen3.8-27b:free",
max_tokens: 1024,
system: "You are a careful code reviewer.",
messages: [{ role: "user", content: "Review this line: if (user = null) return;" }],
});
console.log(message.content[0].text);
console.log(message.usage);The Anthropic SDKs also read the base URL and key from the environment, so existing code needs no changes:
export ANTHROPIC_BASE_URL="https://api.oneaiapikey.com"
export ANTHROPIC_API_KEY="$ONEAIAPIKEY_API_KEY" # the Anthropic SDKs read both variablesWhich headers does the Messages endpoint use?
Your key and Content-Type are required; anthropic-version is recommended.
| Header | Required | Notes |
|---|---|---|
x-api-key |
Yes (or the next one) | Your key. The Anthropic SDKs send this. |
Authorization: Bearer <key> |
Alternative | Accepted too, so one HTTP client can serve both formats. |
anthropic-version |
Recommended | 2023-06-01. If you leave it out, the gateway sends that value. |
anthropic-beta |
Optional | Forwarded to Anthropic as it is, for beta features. |
Content-Type |
Yes | application/json. |
What do the request and response look like?
The body is Anthropic’s: model, max_tokens (required by Anthropic; the gateway sends 8,192 if you leave it out), messages, and optionally system, temperature, top_p, top_k, stop_sequences, stream, tools, tool_choice, thinking, metadata. Message content can be a string or a list of blocks: text, image, document, tool_use, tool_result.
{
"id": "msg_…",
"type": "message",
"role": "assistant",
"model": "qwen-qwen3.8-27b:free",
"content": [{ "type": "text", "text": "This assigns null instead of comparing …" }],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 28,
"output_tokens": 64,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}stop_reason is end_turn, max_tokens, stop_sequence or tool_use. usage.cache_read_input_tokens and usage.cache_creation_input_tokens show prompt caching at work.
Features that work here
- Prompt caching. Mark a stable prefix with
"cache_control": {"type": "ephemeral"}. See Prompt caching for what is billed. - Extended thinking. Pass
thinking; the reply containsthinkingblocks before the text. Thinking tokens are billed as output tokens. See Reasoning models. - Structured output. Anthropic’s
output_config.formatwith a JSON Schema is forwarded. See Structured output. - Tools. Anthropic tool definitions and
tool_use/tool_resultblocks. See Tool calling. - Images and PDFs.
imageanddocumentblocks, base64 or URL. See Vision and files. - Streaming. Anthropic’s server-sent events. See Streaming.
- Token counting.
POST /v1/messages/count_tokenswith a body shaped like a Messages request answers{"input_tokens": n}; the Anthropic SDK’sclient.messages.count_tokens()calls it. The count is a local estimate (about four characters per token, plus four per message), not the provider’s tokenizer: the reply says so withX-Count-Method: estimate. Use theusageof a real reply when you need the billed number. A valid key is required, and the call is free.
message = client.messages.create(
model="qwen-qwen3.8-27b:free",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 8000},
messages=[{"role": "user", "content": "Is 2^61 - 1 prime? Show your reasoning briefly."}],
)
for block in message.content:
print(block.type, getattr(block, "thinking", None) or getattr(block, "text", ""))What is not available?
- The Message Batches and Files APIs are not offered.
GET /v1/modelslists the models your key can use. Sent withx-api-keyandanthropic-version(as the Anthropic SDK’sclient.models.list()does), it answers in Anthropic’s list format; otherwise in OpenAI’s.
Related
- Claude Code guide: point Claude Code at the gateway.
- Authentication: keys, headers and per-key limits.
- Errors: the error body on this endpoint.