Overview
Since v3.1, @cloudbase/js-sdk has a built-in Node.js adapter. You no longer need to install @cloudbase/node-sdk or @cloudbase/adapter-node separately to use the full CloudBase capabilities in Node.js environments (cloud functions, CloudBase Run, self-hosted servers, etc.).
Node.js version >= 16.0 is required
Installation
# npm
npm install @cloudbase/js-sdk@next -S
# yarn
yarn add @cloudbase/js-sdk@next
Some server-side features depend on the following optional packages. Install them as needed:
| Package | Purpose | When to Install |
|---|---|---|
@cloudbase/signature-nodejs | Request signing (TC3-HMAC-SHA256) | When using secretId / secretKey auth |
jsonwebtoken | Custom login ticket signing | When calling auth.createTicket |
ws | WebSocket connection | When using real-time data push |
# Install as needed
npm install @cloudbase/signature-nodejs jsonwebtoken ws
Initialization
Basic Usage
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
});
Initialization Parameters
| Field | Type | Required | Description |
|---|---|---|---|
env | string | No | TCB environment ID. If not specified in a cloud function environment, the current environment ID is used |
accessKey | string | No | Cloudbase API Key, the highest priority authentication method. Can also be configured via the CLOUDBASE_APIKEY environment variable |
auth | object | No | Authentication configuration, including secretId, secretKey, credentials, etc. |
auth.secretId | string | No | Tencent Cloud API key pair. Generate at Tencent Cloud Console / API Key Management. Can also be configured via the TENCENTCLOUD_SECRETID environment variable |
auth.secretKey | string | No | Tencent Cloud API key pair. Can also be configured via the TENCENTCLOUD_SECRETKEY environment variable |
auth.credentials | object | No | Custom login private key, containing private_key, private_key_id, env_id, used for createTicket to issue login credentials. Download the private key from CloudBase Console / Authentication / Login Methods under "Custom Login" |
context | object | No | The context parameter of the function-based CloudBase Run entry function, used for signature-free calls |
timeout | number | No | Request timeout (ms), default 15000 |
Authentication
The server SDK can access CloudBase resources using different authentication methods. The following methods are supported (choose one):
| Authentication Method | Description |
|---|---|
| Default (Cloud Function / CloudBase Run) | No parameters needed; credentials are automatically read from environment variables |
| API Key | Configure the server API Key in the CLOUDBASE_APIKEY environment variable; the SDK reads it automatically |
| Publishable Key | Pass accessKey in init to access with anonymous role identity |
secretId + secretKey | Explicitly pass Tencent Cloud API key pair in init |
context | In CloudBase Run, use the entry function's context parameter for signature-free calls |
Authentication Examples
- Cloud Function (Default Auth)
- CloudBase Run
- Publishable Key
- secretId + secretKey
- API Key
In a cloud function environment, the SDK automatically reads authentication info from environment variables. These are temporary credentials and require no manual configuration.
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "your-env-id",
});
exports.main = async (event, context) => {
// Use CloudBase capabilities directly
};
In a function-based CloudBase Run environment, pass the context parameter for signature-free calls.
💡 Note: If
contextis provided butenvis not, theenvIDfromcontextwill be used as the environment ID.
const cloudbase = require("@cloudbase/js-sdk");
exports.main = async (event, context) => {
const app = cloudbase.init({
context,
});
// Use CloudBase capabilities
};
For detailed information about Publishable Key, please refer to the documentation.
- User Permissions: Anonymous user permissions
- Validity: Long-term valid
- How to Obtain: Get it from CloudBase Console / ApiKey Management
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "your-env-id",
accessKey: "your-publishable-key",
});
Generate a key pair at Tencent Cloud Console / API Key Management.
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "your-env-id",
auth: {
secretId: "your-secretId",
secretKey: "your-secretKey",
},
});
Configure via the CLOUDBASE_APIKEY environment variable. In cloud functions, enable API key settings and add CLOUDBASE_APIKEY; the SDK reads it automatically.

const cloudbase = require("@cloudbase/js-sdk");
// No need to pass explicitly; the SDK reads from environment variables automatically
const app = cloudbase.init({
env: "your-env-id",
});
Environment Info
parseContext
app.parseContext(context: object): object
Parses the context parameter of the cloud function entry function, converting runtime environment variables into a structured object.
- Only effective in cloud function environments
参数
The context parameter of the cloud function entry function
返回
示例
Identity Authentication
In addition to the following server-specific APIs, the server SDK can also call all client-side authentication APIs, such as email login, phone login, username/password login, linked login, etc. See Authentication for details.
getUserInfo
auth.getUserInfo(): IGetUserInfoResult
Gets the current request's user information (synchronous method, reads from cloud function runtime environment variables).
- Synchronous method, no
awaitneeded - Only effective in cloud function / CloudBase Run environments
参数
无参数