Skip to main content

Initialization

NPM Version

@cloudbase/js-sdk enables you to access Cloudbase services and resources using JavaScript in Web environments, such as PC Web pages, WeChat Official Account H5, etc.

Note

The current @cloudbase/js-sdk@latest version has been upgraded to v2. If you need to use v1, see v1 documentation.

Install and Initialize

Install the SDK

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

# yarn
yarn add @cloudbase/js-sdk

Initialize the SDK

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

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

The latest version number version can be found at NPM.

Initialization parameters

FieldTypeRequiredDefault ValueDescription
envstringYes-TCB environment ID
regionstringNoap-shanghaiRegion: ap-shanghai (default), ap-guangzhou, ap-singapore
langstringNozh-CNLanguage: zh-CN (default), en-US
accessKeystringNo-Authentication parameter for anonymous users, which can be exposed in browsers and is used to request publicly accessible resources

⚠️ Note: The region of the environment currently in use must match the currently specified region information!

Login Authentication

The js-sdk uses client-side user permissions and requires login to invoke TCB capabilities.

For details, see Anonymous Login

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

const auth = app.auth();
await auth.signInAnonymously();

Initialization Sample

Singapore Region

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

const app = cloudbase.init({
env: 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: env: "your-env-id", // Replace with your environment id
lang: "en-US",
});