Anthropic SDK integration
Configure the gateway origin in Python and Node.js and call Messages.
Prepare
Choose a Messages-compatible model and line. Set IROUTER_API_KEY and IROUTER_MODEL using environment setup. The SDK base URL is the gateway origin, https://gateway.irouter.io.
Python
Run python -m pip install anthropic, save this as example.py, and run python example.py in the same terminal.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["IROUTER_API_KEY"],
base_url="https://gateway.irouter.io")
message = client.messages.create(
model=os.environ["IROUTER_MODEL"], max_tokens=256,
messages=[{"role": "user", "content": "Reply with a short greeting."}],
)
for block in message.content:
if block.type == "text":
print(block.text)Node.js
Run npm install @anthropic-ai/sdk, save this as example.mjs, and run node example.mjs.
import Anthropic from '@anthropic-ai/sdk';
const apiKey = process.env.IROUTER_API_KEY;
const model = process.env.IROUTER_MODEL;
if (!apiKey || !model) throw new Error('Set IROUTER_API_KEY and IROUTER_MODEL');
const client = new Anthropic({ apiKey, baseURL: 'https://gateway.irouter.io' });
const message = await client.messages.create({
model, max_tokens: 256,
messages: [{ role: 'user', content: 'Reply with a short greeting.' }],
});
for (const block of message.content) {
if (block.type === 'text') console.log(block.text);
}Verify and troubleshoot
Inspect console usage after receiving a reply. content can contain different block types; these examples print only text. Production applications also need to handle tools, stop reasons, timeouts, and errors.
For 401, check the supplied iRouter key. For 404, check for a duplicated /v1/v1/messages path. For model errors, verify Messages support. SDK version headers and optional features must be compatible with the line; run regression checks after upgrading.
See Anthropic SDK documentation and the Messages guide.