Skip to main content

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.

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 openai

# Python
pip install openai

Configuration​

Replace the OpenAI SDK's baseURL and apiKey with your CloudBase settings:

FieldValue
baseURLhttps://<ENV_ID>.api.tcloudbasegateway.com/v1/ai/<PROVIDER>
apiKeyYour CloudBase API Key

Supported Providers​

ProviderDescription
cloudbaseResource-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​

ParameterTypeDescription
modelstringModel name
messagesarrayList of messages
streambooleanEnable streaming
temperaturenumberSampling temperature (0-2)
top_pnumberNucleus sampling (0-1)
max_tokensnumberMaximum tokens to generate
presence_penaltynumberPresence penalty
frequency_penaltynumberFrequency penalty

Migrating from OpenAI​

If your project already uses OpenAI, migrating to CloudBase only takes two steps:

  1. 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"
});
  1. Update the model name
// Original OpenAI model
model: "gpt-4"

// CloudBase-supported model
model: "deepseek-v4-flash" // or any other enabled model

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;
}
}