tencent cloud

CloudBase

Product Introduction
Product Overview
Features and Strengths
Use Cases
System Limits
Purchase Guide
Product Pricing
Description Of Billing Capability Items
Yearly/Monthly Subscription Package Description
Alarm and Notification
Overdue Payment Instructions
Development Guide
Cloud Storage
Database
Identity Verification
Cloud function
Static website management
SDK Documentation
Client SDK
Server SDKs
Management-side SDK
Product Agreement
Cloud Development Service Level Agreement

Initializing SDK

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2025-12-31 11:48:17
When creating a data model, multi-platform operation SDK, such as cloud functions and web, are automatically generated. For initialization and usage, see the following documents.

Calling SDK in Cloud Functions

To use SDK in cloud functions, install @cloudbase/node-sdk 3.2.0 or later versions in the corresponding cloud function directory.

Dependency Installation

When a cloud function is created, a package.json is generated by default in the cloud function directory, and the user will be prompted whether to install dependencies locally immediately. Note that the runtime environment of the cloud function is Node.js, so ensure Node.js is installed before dependencies are installed locally, with both node and npm in the environmental variables. If dependencies are not installed locally, run the following command in the directory:
npm install --save @cloudbase/node-sdk@3.2

Initializing and Using SDK

Before calling the data model SDK in cloud functions, execute the initialization method once:
const cloudbase = require("@cloudbase/node-sdk");

// Specify the CloudBase environment ID.
const app = cloudbase.init({
env: "some-env-id",
});

exports.main = async (event, context) => {
const models = app.models;
// Now you can call the CRUD methods on the models.
// models.post.create({
// data: {
// body: "Hello, World\\n\\nfrom china",
// title: "Hello, World",
// slug: "hello-world-cn",
// },
// }).then(({ data } => { console.log(data)}))
};

Calling SDK in Web Pages

Dependency Installation

In the root directory of the web project, use npm or yarn to install the required packages:
npm install @cloudbase/js-sdk --save

Initializing and Using SDK

import cloudbase from "@cloudbase/js-sdk";

// Import the SDK.
const app = cloudbase.init({
env: "your-cloud-env-id", // Replace this value with your CloudBase environment ID.
clientId: "your-cloud-env-id", // Replace this value with your CloudBase environment ID.
});
const auth = app.auth({
persistence: "local",
});
await auth.signInAnonymously(); // Or use other login methods.

const models = app.models;

// Now you can call the CRUD methods on the models.
// Example: Create a post data record.
// models.post.create({
// data: {
// body: "Hello, World\\n\\nfrom china",
// title: "Hello, World",
// slug: "hello-world-cn",
// },
// }).then(({ data } => { console.log(data)}))

Multi-Platform Support

@cloudbase/js-sdk supports multi-platform calls. If you have web and Mini Program development requirements, use @cloudbase/js-sdk via API call.

One Codebase for Multiple Platforms

Note: No configuration is needed for web clients and Mini Program.
Since @cloudbase/js-sdk has built-in adapters for web clients and Mini Programs, no configuration is needed on these two platforms.
If you need to make one codebase compatible with multiple platforms, @cloudbase/js-sdk can incorporate multiple adapters. At runtime, the platform type is determined by the isMatch function of each adapter, and then the corresponding compatibility logic is introduced. For example, the following code is compatible with QQ Mini Games, Cocos native, and Baidu Mini Games:
import cloudbase from '@cloudbase/js-sdk';
import adapter as adapterOfQQGame from 'cloudbase-adapter-qq_game';
import adapter as adapterOfCocosNative from 'cloudbase-adapter-cocos_native';
import adapter as adapterOfBDGame from 'cloudbase-adapter-bd_game';

cloudbase.useAdapters([
adapterOfQQGame,
adapterOfCocosNative,
adapterOfBDGame
]);
Note:
When using @cloudbase/js-sdk in a Mini Program, if the Mini Program package size needs to be reduced, you can import functional modules as needed.
// Kernel.
import cloudbase from "@cloudbase/js-sdk/app";
// Login module.
import "@cloudbase/js-sdk/auth";
// Cloud function module.
import "@cloudbase/js-sdk/functions";
// Cloud storage module.
import "@cloudbase/js-sdk/storage";
// Database module.
import "@cloudbase/js-sdk/database";
// Real-time push module, which should be imported after the database module is imported.
import "@cloudbase/js-sdk/realtime";
// Ad reporting module.
import "@cloudbase/js-sdk/analytics";

const app = cloudbase.init({
env: "your-env-id",
});


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백