Anthropic Messages
Use Anthropic's SDK and /v1/messages format with every chat model.
On this page
POST https://api.routerplex.com/v1/messages
RouterPlex accepts Anthropic's Messages API request format and returns Anthropic-format responses for every chat model in the catalog — Claude, GPT, Gemini, DeepSeek, Kimi, Qwen, and more. The same RouterPlex key and prepaid balance work across both API formats.
Anthropic SDK base URL: https://api.routerplex.com — do not append /v1; the SDK adds /v1/messages itself.
Before you start #
These examples run on macOS, Linux, and Windows. Set your key and prepare your environment first.
For Python, install with .venv/bin/python -m pip install --upgrade anthropic on macOS/Linux or .\.venv\Scripts\python.exe -m pip install --upgrade anthropic in PowerShell. Save the Python example as messages.py and run it with that same Python executable.
For JavaScript, install @anthropic-ai/sdk with npm install @anthropic-ai/sdk on macOS/Linux or npm.cmd install @anthropic-ai/sdk in PowerShell. The TypeScript example also runs as a .mjs file with node messages.mjs.
curl — macOS and Linux #
curl https://api.routerplex.com/v1/messages \-H "x-api-key: $ROUTERPLEX_API_KEY" \-H "anthropic-version: 2023-06-01" \-H "content-type: application/json" \-d '{"model": "gpt-5.4","max_tokens": 256,"messages": [{"role": "user", "content": "Explain HTTP in one line"}]}'
Windows PowerShell #
$body = @{ model = 'claude-sonnet-4-6'; max_tokens = 256; messages = @(@{ role = 'user'; content = 'Reply with exactly: RouterPlex connected.' }) } | ConvertTo-Json -Depth 5Invoke-RestMethod -Uri 'https://api.routerplex.com/v1/messages' -Method Post -Headers @{ 'x-api-key' = $env:ROUTERPLEX_API_KEY; 'anthropic-version' = '2023-06-01' } -ContentType 'application/json' -Body $body
Authorization: Bearer $ROUTERPLEX_API_KEY also works if your HTTP client already uses bearer authentication.
Python SDK #
import osfrom anthropic import Anthropicclient = Anthropic(base_url="https://api.routerplex.com",api_key=os.environ["ROUTERPLEX_API_KEY"],)message = client.messages.create(model="claude-sonnet-4-6",max_tokens=256,messages=[{"role": "user", "content": "Explain HTTP in one line"}],)print(message.content[0].text)
TypeScript SDK #
import Anthropic from "@anthropic-ai/sdk";const client = new Anthropic({baseURL: "https://api.routerplex.com",apiKey: process.env.ROUTERPLEX_API_KEY,});const message = await client.messages.create({model: "claude-sonnet-4-6",max_tokens: 256,messages: [{ role: "user", content: "Explain HTTP in one line" }],});console.log(message.content);
Streaming #
Set stream: true or use the Anthropic SDK's streaming helper. RouterPlex returns Anthropic SSE events such as message_start, content_block_delta, and message_stop, including when the selected model is not a Claude model.
Which format should I use? #
| Client | Base URL | Format |
|---|---|---|
| OpenAI SDKs and OpenAI-compatible tools | https://api.routerplex.com/v1 | /v1/chat/completions |
| Anthropic SDK and Claude Code | https://api.routerplex.com | /v1/messages |
Both formats reach the same 63 chat models and deduct from the same balance. gpt-image-2 is image-generation only and uses /v1/images/generations, not a chat endpoint. Tool use, vision, thinking, and structured-output support still depend on the selected model.
Confirm the returned text and the matching request in Usage. Official SDK references: Python, JavaScript/TypeScript.