Skip to main content

initialization

Initialization

NPM Version

@cloudbase/js-sdk lets you use JavaScript to access cloudbase Service and resources on the Web (such as PC webpages, WeChat public platform H5) and Node.js server.

Note

Currently, the @cloudbase/js-sdk version has launched the v3 next version. This version is fully compatible with v2 and aligns with the HTTP API.

If you need to use v2, refer to the v2 document.

If you need to use v1, refer to the v1 document

Installation and Initialization

Installing the SDK

# npm
npm install @cloudbase/js-sdk@next -S

# yarn
yarn add @cloudbase/js-sdk@next

Initialize SDK

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

const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});

The latest version number can go to NPM to view.

Initialize the parameter

FieldTypeRequiredDefault ValueDescription
envstringYes-TCB environment ID
regionstringNoap-shanghaiRegion: ap-shanghai (default), ap-guangzhou, ap-singapore
langstringNozh-CNSpecified language: zh-CN (default), en-US
accessKeystringNo-Anonymous user authentication parameter, can be exposed in browser, used for requesting public accessed resources

⚠️ Note: The region of the currently used environment must be consistent with the designated region information.

Login Authentication

js-sdk uses C-end user permission. You need to log in to call cloud development capability.

For details, see anonymous login.

const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
});

const { data, error } = await app.auth.signInAnonymously();

Initialization Example

Singapore region

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

const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-singapore",
});

Use English prompt

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

const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
lang: "en-US",
});