Skip to main content

Initialization

NPM Version

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

Note

The @cloudbase/js-sdk v3 next version is now available. This version is fully compatible with v2 and aligns with the HTTP API.

If you need to use the Node.js server-side SDK, refer to the Node.js SDK documentation.

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

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 with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});

For the latest version number, visit NPM.

Initialization Parameters

FieldTypeRequiredDefaultDescription
envstringYes-TCB environment ID
regionstringNoap-shanghaiRegion: ap-shanghai (default), ap-guangzhou, ap-singapore
langstringNozh-CNLanguage: zh-CN (default), en-US
accessKeystringNo-Anonymous user authentication key, safe to expose in the browser for public resource access
Note

The region of the current environment must match the region value specified during initialization.

Login Authentication

js-sdk uses client-side user permissions. You must log in before calling CloudBase features.

For details, see Anonymous Login.

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

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

Initialization Examples

Singapore Region

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

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

Use English Prompts

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

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