initialization
Initialization
@cloudbase/node-sdk is no longer maintained (the 4.x next version is entirely based on @cloudbase/js-sdk). Please use @cloudbase/js-sdk v3 directly for development — it already supports all Node.js features.
@Cloudbase/node-sdk lets you use Node.js for server-side access to Cloudbase Service and resources
⚠️ Note: Starting from v3, if the env parameter is not specified in init({}), the current Serverless Cloud Function (SCF) environment ID is used by default, and the cloud development default environment is no longer in use. To use the cloud development default environment, you can specify init({env: tcb.SYMBOL_DEFAULT_ENV}).
Finish MCP setup first, then select a prompt to start your AI-native development journey
Installing the SDK
# npm
npm install @cloudbase/node-sdk -S
# yarn
yarn add @cloudbase/node-sdk
Initialize SDK
Basic Usage
import tcb from "@cloudbase/node-sdk";
// Use commonjs introduction method in SCF environment
// const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: "your-env-id", // Replace this value with your environment ID
});
Initialize the parameter
| Field | Data Type | Required | Description |
|---|---|---|---|
env | string | No | CloudBase environment ID |
accessKey | string | No | Cloudbase API Key, the highest priority authentication method. It can also be configured through the environment variable CLOUDBASE_APIKEY. |
secretId | string | No | Tencent Cloud API fixed key pair secretId, go to Tencent Cloud Console/API Key Management to generate. It can also be configured through the environment variable TENCENTCLOUD_SECRETID. |
secretKey | string | No | Tencent Cloud API fixed key pair secretKey, go to Tencent Cloud Console/API Key Management to generate. It can also be configured through the environment variable TENCENTCLOUD_SECRETKEY. |
sessionToken | string | No | Tencent Cloud API temporary key pair Token, obtained through the role assumption temporary access credential API. |
credentials | object | No | Cloudbase private key, containing two strings private_key and private_key_id, used to issue tickets for custom login scenario Log in to Cloud Development Platform/Identity Verification/Log-in Methods and obtain via "private key download" for custom login |
context | Context | No | Functional cloud hosting entry function context parameter, used for signature-free calls in cloud hosting scenarios. |
timeout | number | No | API call timeout period (ms), defaults to 15000ms, i.e. 15s. |
proxy | string | No | http proxy url used when calling API. |
version | string | No | Version number, dependency project version number. |
Login Authentication
node-sdk uses the following authentication methods in different environments:
During SDK initialization, you can select the authentication method as follows (just choose one of them):
| Authentication method | Description |
|---|---|
Environment variable TENCENTCLOUD_SECRETID + TENCENTCLOUD_SECRETKEY | No need to input in init, automatically read secretId and secretKey from environment variables, access with role identity of administrator. Only applies to event-triggered Cloud Functions; does NOT apply to Web Cloud Functions (HTTP Cloud Functions) |
Use client Publishable Key | Explicitly input Publishable Key in init, access with anonymous role identity |
secretId + secretKey (explicit configuration) | Explicitly input secretId and secretKey in init, access with admin role identity |
Use server API Key | No need to input in init, automatically read CLOUDBASE_APIKEY from environment variables, configure server API Key to environment variable CLOUDBASE_APIKEY |
sessionToken (explicit configuration) | Explicitly input sessionToken in init, access with temporary access credential via role assumption |
context.extendedContext | The managed environment automatically obtains temporary key and environment ID from context |