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
- A CloudBase environment is set up
- A Token Resource Pack is purchased and the model switch is enabled (see AI Model Access)
- 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:
| Parameter | Value |
|---|---|
| authToken | Your CloudBase API Key |
| baseURL | https://<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
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"
});
const messages = [];
async function chat(userMessage) {
messages.push({ role: "user", content: userMessage });
const response = await client.messages.create({
model: "hy3-preview",
max_tokens: 1024,
system: "You are a poetry expert.",
messages
});
const assistantContent = response.content[0].text;
messages.push({ role: "assistant", content: assistantContent });
return assistantContent;
}
// Example usage
await chat("What is Li Bai's most famous poem?");
await chat("What is the background of that poem?");
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
| Parameter | Type | Description |
|---|---|---|
| model | string | Model name |
| messages | array | List of messages |
| system | string | System prompt |
| max_tokens | number | Maximum tokens to generate (required) |
| stream | boolean | Enable streaming |
| temperature | number | Sampling temperature (0–1) |
| top_p | number | Nucleus sampling (0–1) |