OpenAI SDK Integration
CloudBase AI is compatible with both OpenAI Chat Completions and Anthropic Messages API protocols. You can use the OpenAI SDK directly, making it easy to migrate existing projects or switch between models.
Online Example
Prerequisites
- A CloudBase environment (older plans can be upgraded), with model switch enabled (see Overview)
- An API Key is created (Get one here)
Installation
# Node.js
npm install openai
# Python
pip install openai
Configuration
Replace the OpenAI SDK's baseURL and apiKey with your CloudBase settings:
| Field | Value |
|---|---|
| baseURL | https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/<PROVIDER> |
| apiKey | Your CloudBase API Key |
Supported Providers
| Provider | Description |
|---|---|
| cloudbase | Resource-Point Plan (unified entry, supports multiple models) |
Node.js Examples
Non-streaming
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: "<YOUR_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});
async function main() {
const completion = await client.chat.completions.create({
model: "deepseek-v4-flash",
messages: [{ role: "user", content: "Hello" }],
temperature: 0.7
});
console.log(completion.choices[0].message.content);
}
main();
Streaming
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: "<YOUR_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});
async function main() {
const stream = await client.chat.completions.create({
model: "deepseek-v4-flash",
messages: [{ role: "user", content: "Introduce yourself" }],
stream: true
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || "";
process.stdout.write(content);
}
}
main();
Python Examples
Non-streaming
from openai import OpenAI
client = OpenAI(
api_key="<YOUR_API_KEY>",
base_url="https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
)
completion = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Hello"}],
temperature=0.7
)
print(completion.choices[0].message.content)
Streaming
from openai import OpenAI
client = OpenAI(
api_key="<YOUR_API_KEY>",
base_url="https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
)
stream = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Introduce yourself"}],
stream=True
)
for chunk in stream:
content = chunk.choices[0].delta.content or ""
print(content, 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.
Switching Models
Simply modify the provider in baseURL and the model parameter to switch between different models:
// Using cloudbase (Resource-Point Plan, recommended)
const cloudbaseClient = new OpenAI({
apiKey: "<YOUR_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});
await cloudbaseClient.chat.completions.create({
model: "deepseek-v4-flash",
messages: [{ role: "user", content: "Hello" }]
});
Supported Parameters
| Parameter | Type | Description |
|---|---|---|
| model | string | Model name |
| messages | array | List of messages |
| stream | boolean | Enable streaming |
| temperature | number | Sampling temperature (0-2) |
| top_p | number | Nucleus sampling (0-1) |
| max_tokens | number | Maximum tokens to generate |
| presence_penalty | number | Presence penalty |
| frequency_penalty | number | Frequency penalty |
Migrating from OpenAI
If your project already uses OpenAI, migrating to CloudBase only takes two steps:
- Update the configuration
// Original OpenAI configuration
const client = new OpenAI({
apiKey: "sk-xxx" // OpenAI API Key
});
// CloudBase configuration
const client = new OpenAI({
apiKey: "<YOUR_CLOUDBASE_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});
- Update the model name
// Original OpenAI model
model: "gpt-4"
// CloudBase-supported model
model: "deepseek-v4-flash" // or any other enabled model
Related Documentation
If you use the Anthropic SDK, you can connect to CloudBase in the same way. See Anthropic SDK Integration.
Error Handling
const OpenAI = require("openai");
const client = new OpenAI({
apiKey: "<YOUR_API_KEY>",
baseURL: "https://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/cloudbase"
});
try {
const completion = await client.chat.completions.create({
model: "deepseek-v4-flash",
messages: [{ role: "user", content: "Hello" }]
});
console.log(completion.choices[0].message.content);
} catch (error) {
if (error instanceof OpenAI.APIError) {
console.error("API Error:", error.status, error.message);
} else {
throw error;
}
}