HTTP API Quick Start
The Cloud Development HTTP API is a set of interfaces designed for developers, aimed at providing access to Cloud Development platform functionalities from both client and server sides via the HTTP protocol. These APIs allow developers to programmatically implement features such as user authentication, cloud functions, data models, AI, etc., thereby accelerating the application development process.
HTTP API is a universal access method that can be used across any platform/language. This article will take the Javascript language as an example to demonstrate how to use the HTTP API.
In the HTTP API Documentation, we provide code generation capabilities for accessing interfaces in multiple languages, including C++, Java, Go, and more. Select your language, copy and paste to integrate the HTTP API into your application.
Prerequisites
Before using the HTTP API for specific modules, ensure that HTTP API permissions are enabled for the corresponding roles.
- Go to Cloud Admin Console -> Access Control -> Policy Management
- Configure access policies for Open APIs (HTTP API) of each module for the role.
1. Authentication
Anonymous Login
const env = "your-env-id";
const deviceId = Math.random().toString(36).substring(2, 15); // Randomly generated and should be cached on the client side
const { token_type, access_token } = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/auth/v1/signin/anonymously`,
{
method: "POST",
headers: {
"x-device-id": deviceId,
},
},
)
).json();
console.log(token_type, access_token);
Username/Password Login
const env = "your-env-id";
const deviceId = Math.random().toString(36).substring(2, 15); // Randomly generated and should be cached on the client side
const { token_type, access_token } = await (
await fetch(`https://${env}.api.tcloudbasegateway.com/auth/v1/signin`, {
method: "POST",
headers: {
"x-device-id": deviceId,
},
body: JSON.stringify({
password: "your-password",
username: "your-username",
}),
})
).json();
console.log(token_type, access_token);
For details, refer to the User Authentication HTTP API
2. Invoking Cloud Functions
const functonName = "add-two";
const functionParams = {
a: 1,
b: 101,
};
const functionResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/functions/${functonName}`,
{
method: "POST",
body: JSON.stringify(functionParams),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `${token_type} ${access_token}`,
},
},
)
).text();
console.log(functionResult);
For details, refer to the Cloud Function HTTP API
3. Invoking Cloud Run
const cloudRunName = "helloworld";
const cloudRunResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/cloudrun/${cloudRunName}`,
{
headers: { Authorization: `${token_type} ${access_token}` },
},
)
).text();
console.log(cloudRunResult);
For details, refer to the Cloud Hosting HTTP API
4. Using Cloud Storage
const objectId = "test.txt";
// Call the HTTP API to get upload information
const [uploadInfo] = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/storages/get-objects-upload-info
`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
},
body: JSON.stringify([{ objectId }]),
method: "POST",
},
)
).json();
// Use the upload information to perform the upload
const uploadResult = await fetch(uploadInfo.uploadUrl, {
method: "PUT",
headers: {
Authorization: uploadInfo.authorization,
"X-Cos-Meta-Fileid": uploadInfo.cloudObjectMeta,
"X-Cos-Security-Token": uploadInfo.token,
},
body: "Hello, World!",
});
console.log(uploadResult.status, uploadResult.statusText);
For details, refer to the Cloud Storage HTTP API
5. Query Data Model
const dataModelName = "post";
const listParams = {
select: { $master: true },
getCount: true,
pageSize: 3,
};
const result = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/model/prod/${dataModelName}/list`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify(listParams),
},
)
).json();
console.log(JSON.stringify(result, null, 2));
For details, refer to the Data Model HTTP API
6. Using AI Large Models
const provider = "deepseek"; // large model provider
const model = "deepseek-v3"; // model name
const llmMessages = [{ role: "user", content: "hi" }];
// Non-streaming text generation
const llmGenerateResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/ai/${provider}/chat/completion`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
model,
messages: llmMessages,
stream: false, // Non-streaming, stream is false
}),
},
)
).json();
console.log(JSON.stringify(llmGenerateResult, null, 2));
// Streaming text generation
const llmStreamResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/ai/${provider}/chat/completion`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
"Content-Type": "application/json",
Accept: "text/event-stream",
},
method: "POST",
body: JSON.stringify({
model,
messages: llmMessages,
stream: true, // Streaming, stream is true
}),
},
)
).text();
console.log(llmStreamResult);
For details, refer to the ai Large Model Access HTTP API
7. Using Agents (Agents)
const agentId = "your-agent-id"; // Fill in the Agent ID created on the Cloud Base platform
const agentMessages = [{ role: "user", content: "hi" }];
// Non-streaming text generation
const agentGenerateResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/aibot/openai/chat/completions`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
model: agentId,
messages: agentMessages,
stream: false, // Non-streaming, stream is false
}),
},
)
).json();
console.log(JSON.stringify(agentGenerateResult, null, 2));
// Streaming text generation
const agentStreamResult = await (
await fetch(
`https://${env}.api.tcloudbasegateway.com/v1/aibot/openai/chat/completions`,
{
headers: {
Authorization: `${token_type} ${access_token}`,
"Content-Type": "application/json",
Accept: "text/event-stream",
},
method: "POST",
body: JSON.stringify({
model: agentId,
messages: agentMessages,
stream: true, // Streaming, stream is true
}),
},
)
).text();
console.log(agentStreamResult);
For details, refer to the AI Agent Access HTTP API