Skip to main content

Initialization

NPM Version Node Version

@cloudbase/js-sdk is a cross-platform JavaScript SDK that lets you use JavaScript to access CloudBase services and resources across Web browsers, Node.js servers (Cloud Functions, CloudRun, self-hosted servers, etc.), WeChat Mini Programs, React Native, and more.

Note

The @cloudbase/js-sdk latest version is v3. This version is fully compatible with v2 and aligns with the HTTP API. Since v3.1, the Node.js adapter is built-in — no need to install @cloudbase/node-sdk separately.

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

Quick Start

Get started with CloudBase in just 3 steps:

npm install @cloudbase/js-sdk -S
import cloudbase from "@cloudbase/js-sdk";

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

// Start using CloudBase features
const db = app.database();
const result = await db.collection("todos").get();
Get Publishable Key

Go to CloudBase Platform / API Key Configuration to generate one. Publishable Key is the recommended auth method for Web — no extra login steps needed.


Installing the SDK

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

# yarn
yarn add @cloudbase/js-sdk

Full import:

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

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

On-demand import (reduce bundle size):

// Core (required)
import cloudbase from "@cloudbase/js-sdk/app";
// Auth module
import { registerAuth } from "@cloudbase/js-sdk/auth";
// Database module
import { registerDatabase } from "@cloudbase/js-sdk/database";
// Cloud Functions module
import { registerFunctions } from "@cloudbase/js-sdk/functions";
// Cloud Storage module
import { registerStorage } from "@cloudbase/js-sdk/storage";
// Data Model module
import { registerModel } from "@cloudbase/js-sdk/model";
// Realtime module (requires database module to be registered first)
import { registerRealtime } from "@cloudbase/js-sdk/realtime";
// AI module
import { registerAi } from "@cloudbase/js-sdk/ai";
// CloudRun module
import { registerCloudrun } from "@cloudbase/js-sdk/cloudrun";
// MySQL module
import { registerMysql } from "@cloudbase/js-sdk/mysql";

// Register required modules
registerAuth(cloudbase);
registerDatabase(cloudbase);
// ... other modules as needed

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

// Use corresponding features
const auth = app.auth;
const db = app.database();
Module NamePackage NameRegistration Function
Core@cloudbase/js-sdk/app-
Auth@cloudbase/js-sdk/authregisterAuth
Database@cloudbase/js-sdk/databaseregisterDatabase
Cloud Functions@cloudbase/js-sdk/functionsregisterFunctions
Cloud Storage@cloudbase/js-sdk/storageregisterStorage
Data Model@cloudbase/js-sdk/modelregisterModel
Realtime@cloudbase/js-sdk/realtimeregisterRealtime
AI@cloudbase/js-sdk/airegisterAi
CloudRun@cloudbase/js-sdk/cloudrunregisterCloudrun
MySQL@cloudbase/js-sdk/mysqlregisterMysql
Note
  • The core module @cloudbase/js-sdk/app is required
  • The realtime module depends on the database module; register database first
  • On-demand import can effectively reduce the final bundle size; recommended for production

For the latest version number, visit NPM.


Initialization Parameters

Common Parameters

FieldTypeRequiredDefaultDescription
envstringYes-TCB environment ID. Defaults to current env ID in Cloud Functions
regionstringNoap-shanghaiRegion: ap-shanghai (default), ap-guangzhou, ap-singapore
langstringNozh-CNLanguage: zh-CN (default), en-US
accessKeystringNo-CloudBase API Key / Publishable Key for authentication
timeoutnumberNo15000Request timeout (ms)
Note

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

Node.js-specific Parameters

Node.js

The following parameters are only effective in Node.js environments.

FieldTypeRequiredDescription
secretIdstringNoTencent Cloud API key pair. Generate at Tencent Cloud Console / API Key Management. Can also be configured via TENCENTCLOUD_SECRETID env var
secretKeystringNoTencent Cloud API key pair. Can also be configured via TENCENTCLOUD_SECRETKEY env var
credentialsobjectNoCustom login private key containing private_key, private_key_id, env_id, used for createTicket to issue login credentials. Download from CloudBase Platform / Auth / Login Methods under "Custom Login"
contextobjectNoThe context parameter from the CloudRun entry function, used for signature-free calls

SYMBOL_CURRENT_ENV

cloudbase.SYMBOL_CURRENT_ENV;

Use this field during initialization to specify using the current Cloud Function or CloudRun environment. Only effective in Cloud Function or CloudRun environments.

Note

In Cloud Functions or CloudRun, if init({}) is called without an env parameter, it defaults to the current environment ID instead of the CloudBase default environment. To use the default environment, specify init({env: tcb.SYMBOL_DEFAULT_ENV}).

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

// Initialize with the current Cloud Function's environment
const app = cloudbase.init({ env: cloudbase.SYMBOL_CURRENT_ENV });

exports.main = async (event, context) => {
// Business logic
};

Login Authentication

After initialization, you need to complete authentication before using CloudBase features. Web and Node.js use different auth methods:

Web Login

The Web SDK uses client-side user permissions, supporting the following login methods:

MethodUse CaseLogin Interaction Required
Publishable KeyPublic resource access❌ No login needed
Anonymous LoginUse without registration❌ No login needed
Account LoginUser identity required✅ Required

Publishable Key can be safely exposed in the browser for requesting publicly accessible resources — no extra login steps needed. For more details, refer to the documentation.

How to get: Go to CloudBase Platform / API Key Configuration to generate one.

const app = cloudbase.init({
env: "your-env-id",
accessKey: "your-publishable-key", // Fill in the generated Publishable Key
});

// No extra login needed — start using directly
const db = app.database();
Note

A Publishable Key can only be generated once. It is permanently valid and cannot be deleted. Keep it safe.

Node.js Authentication

Node.js

The server supports multiple auth methods for accessing CloudBase resources (choose one).

Auth MethodDescriptionUse Case
Default (Cloud Function/CloudRun env)No parameters needed; auto-reads credentials from env varsCloud Functions, CloudRun
API KeyConfigure server API Key in CLOUDBASE_APIKEY env var; SDK reads it autoSelf-hosted servers
Publishable KeyPass accessKey in init; uses anonymous rolePublic resource access
secretId + secretKeyExplicitly pass Tencent Cloud API key pair in initSelf-hosted servers
contextCloudRun scenario; signature-free call via entry function context paramCloudRun

In Cloud Function environments, the SDK automatically reads auth info from environment variables. These are temporary access credentialsno manual configuration needed.

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

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

exports.main = async (event, context) => {
// Use CloudBase features directly
};
Note

When the server does not use API Key or Publishable Key, it defaults to admin permissions. Otherwise, the API signatures, parameters, and return values are identical to the client side.