Skip to main content

Initialize the SDK

When creating a data model, operation SDKs for multiple ends (such as Mini Program/Cloud Function/Web) will be automatically generated. You can refer to the following documentation for initialization and usage.

Invocation in Mini-Programs

Install dependencies

Method 1: Download the SDK

You can directly download the SDK link file below to the mini-program code directory (usually miniprogram) and save it as wxCloudClientSDK.umd.js.

https://weda.cloud.tencent.com/wx-cloud-client-sdk/1.6.1/wxCloudClientSDK.umd.js

Method 2: Import via npm

Guide to Using npm in WeChat Developer Tools

In the directory where the mini-program's package.json is located (usually the miniprogram directory), run the command to install the npm package:

npm install @cloudbase/wx-cloud-client-sdk --save

Then click "Tools" -> "Build npm" on the toolbar of WeChat Developer Tools.

Initialization and Usage of the SDK

When creating a data model, operation SDKs for multiple ends (such as Mini Program/Cloud Function/Web) will be automatically generated. You can refer to the following documentation for initialization and usage.

// If you downloaded the SDK, change to const { init } = require('./wxCloudClientSDK.umd.js')
const { init } = require("@cloudbase/wx-cloud-client-sdk");

// Specify the Cloud Base environment ID.
wx.cloud.init({
env: "some-env-id", // Current Cloud Base environment ID
});

const client = init(wx.cloud);
const models = client.models; // Alternatively, you can also obtain it directly from wx.cloud.models, though this approach provides weaker type hints.

// 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 in Cloud Functions

For use in Cloud Functions, you must install version 3.10.0 or later of @cloudbase/node-sdk in the corresponding cloud function directory.

Install dependencies

When creating a cloud function, a package.json file is created by default in the cloud function directory, and users are prompted whether to install dependencies locally immediately. Note that the cloud function runtime environment is Node.js, so when installing dependencies locally, ensure Node.js is installed and both node and npm are in the environment variables. If not installing dependencies locally, you can run the following command in this directory:

npm install --save @cloudbase/node-sdk@3.10

Initialization and Usage of the SDK

Before calling the data model SDK in cloud functions, execute the initialization method once:

const cloudbase = require("@cloudbase/node-sdk");

// Specify the Cloud Base 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 in Web Pages

Install dependencies

In the root directory of the web project, use npm or yarn to install the required packages:

npm install @cloudbase/js-sdk --save

Initialization and Usage of the SDK

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

// Import the SDK.
const app = cloudbase.init({
env: "your-cloud-env-id", // Replace this value with your Cloud Base environment ID.
clientId: "your-cloud-env-id", // Replace this value with your Cloud Base 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)}))

Cross-Platform Support

@cloudbase/js-sdk supports multi-end invocation. For details, refer to One Code for Multiple Platforms. If you have requirements for Web and miniapp multi-end development, please use the @cloudbase/js-sdk method for invocation. When using @cloudbase/js-sdk in miniapps, if you need to reduce the miniapp package size, you can refer to On-Demand Import of Feature Modules.