Skip to main content

Anthropic SDK Integration

CloudBase AI is compatible with both OpenAI Chat Completions and Anthropic Messages API protocols. You can use the Anthropic SDK directly by passing authToken and baseURL in the client constructor.

Authentication

CloudBase authenticates via Authorization: Bearer <token>, which corresponds to the authToken parameter in the Anthropic SDK — not apiKey.

Prerequisites

  1. A CloudBase environment (older plans can be upgraded), with model switch enabled (see Overview)
  2. An API Key is created (Get one here)

Installation

# Node.js
npm install @anthropic-ai/sdk

# Python
pip install anthropic

Configuration

Pass authToken and baseURL when initializing the client:

ParameterValue
authTokenYour CloudBase API Key
baseURLhttps://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase

Replace <ENV_ID> with your actual CloudBase environment ID and <YOUR_CLOUDBASE_API_KEY> with your actual API Key.

Node.js Examples

Non-streaming

const Anthropic = require("@anthropic-ai/sdk");

const client = new Anthropic({
authToken: "<YOUR_CLOUDBASE_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});

async function main() {
const message = await client.messages.create({
model: "hy3-preview",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }]
});

console.log(message.content[0].text);
}

main();

Streaming

const Anthropic = require("@anthropic-ai/sdk");

const client = new Anthropic({
authToken: "<YOUR_CLOUDBASE_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});

async function main() {
const stream = await client.messages.stream({
model: "hy3-preview",
max_tokens: 1024,
messages: [{ role: "user", content: "Introduce yourself" }]
});

for await (const chunk of stream) {
if (
chunk.type === "content_block_delta" &&
chunk.delta.type === "text_delta"
) {
process.stdout.write(chunk.delta.text);
}
}
}

main();

Python Examples

Non-streaming

import anthropic

client = anthropic.Anthropic(
auth_token="<YOUR_CLOUDBASE_API_KEY>",
base_url="https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
)

message = client.messages.create(
model="hy3-preview",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}]
)

print(message.content[0].text)

Streaming

import anthropic

client = anthropic.Anthropic(
auth_token="<YOUR_CLOUDBASE_API_KEY>",
base_url="https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
)

with client.messages.stream(
model="hy3-preview",
max_tokens=1024,
messages=[{"role": "user", "content": "Introduce yourself"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)

Multi-turn Conversations

Multi-turn conversations require maintaining a complete messages array in each request. See the Multi-turn Conversation documentation for details.

Migrating from Anthropic

If your project already uses the Anthropic SDK, update the client initialization to use authToken instead of apiKey:

// Original Anthropic configuration
const client = new Anthropic({
apiKey: "<ANTHROPIC_API_KEY>"
});

// CloudBase configuration
const client = new Anthropic({
authToken: "<YOUR_CLOUDBASE_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});

Replace the model name with a model enabled in CloudBase (e.g., hy3-preview). No other code changes are needed.

Error Handling

const Anthropic = require("@anthropic-ai/sdk");

const client = new Anthropic({
authToken: "<YOUR_CLOUDBASE_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});

try {
const message = await client.messages.create({
model: "hy3-preview",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello" }]
});
console.log(message.content[0].text);
} catch (error) {
if (error instanceof Anthropic.APIError) {
console.error("API Error:", error.status, error.message);
} else {
throw error;
}
}

Supported Parameters

ParameterTypeDescription
modelstringModel name
messagesarrayList of messages
systemstringSystem prompt
max_tokensnumberMaximum tokens to generate (required)
streambooleanEnable streaming
temperaturenumberSampling temperature (0–1)
top_pnumberNucleus sampling (0–1)