How to use the Anthropic Messages API on TokenRex
Send Claude requests in Anthropic's native Messages format with the Anthropic SDK.
Point the Anthropic SDK at TokenRex
Use the Anthropic client with your TokenRex key and the TokenRex base URL. This format is for Claude models only.
Example
import anthropic
client = anthropic.Anthropic(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")
msg = client.messages.create(
model="claude-sonnet-5",
max_tokens=512,
messages=[{"role": "user", "content": "Hello"}],
)
print(msg.content[0].text)Questions
- Do I need a separate key for each provider?
- No. One TokenRex API key works for every enabled model; change only the model ID.
Related
- How to use the OpenAI SDK with a custom base URL
- How to call Claude with the OpenAI SDK
- How to call Gemini with the OpenAI SDK
- How to stream LLM responses
- How to use function calling (tools) with an LLM API
- How to send images to a vision model
- How to generate images with an API
- How to convert text to speech with an API