Anthropic SDK
Call models on Inference.net using the Anthropic SDK and the Messages API.
The Inference.net API supports the Anthropic Messages format at POST https://api.inference.net/v1/messages. This means you can use the Anthropic SDK to call any serverless model hosted on Inference.net, such as glm-5.2. Only your Inference.net API key is needed.
Requests are converted to and from the model's native format for you. Both non-streaming and streaming responses are supported.
Authentication
The Anthropic SDK sends its apiKey as the x-api-key header, but the Inference.net API expects your key as a Bearer token in the Authorization header. Pass your Inference.net API key as authToken (TypeScript) or auth_token (Python), which sends Authorization: Bearer <key>. In TypeScript, also set apiKey: null so the SDK does not look for an Anthropic key.
export INFERENCE_API_KEY=<your-api-key>Non-streaming
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.inference.net",
apiKey: null,
authToken: process.env.INFERENCE_API_KEY,
});
const message = await client.messages.create({
model: "glm-5.2",
max_tokens: 1024,
messages: [{ role: "user", content: "What is the meaning of life?" }],
});
console.log(message.content);Streaming
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: "https://api.inference.net",
apiKey: null,
authToken: process.env.INFERENCE_API_KEY,
});
const stream = client.messages.stream({
model: "glm-5.2",
max_tokens: 1024,
messages: [{ role: "user", content: "Count from 1 to 5." }],
});
stream.on("text", (text) => {
process.stdout.write(text);
});
const message = await stream.finalMessage();
console.log("\n", message.usage);The same code works for every serverless model. Set model to the model ID you want. Browse available models in the model catalog.