initialization
Initialization
@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}).
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 | TCB 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 |
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 |
Authentication example
- Use default authentication
- Use Publishable Key
- Use API Key
- Use secretId and secretKey
- Use sessionToken
Users do not need to input in init, secretId and secretKey are automatically read from environment variable
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "your-env-id",
});
For detailed introduction of Publishable Key, see Documentation Description.
- User permission: anonymous user permission
- Validity Period: long-term valid -Method for obtaining: Obtain from Cloud Development Platform/ApiKey Management.
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "your-env-id",
accessKey: "your-publishable-key",
});
You can configure through environment variable CLOUDBASE_APIKEY, enable API key settings in SCF, and add CLOUDBASE_APIKEY.

import tcb from "@cloudbase/node-sdk";
// No need to explicitly input accessKey, the SDK automatically reads from environment variable
const app = tcb.init({
env: "your-env-id",
});
Go to tencent cloud console/API key management to generate secretKey, secretId
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "your-env-id",
secretId: "your-secretId",
secretKey: "your-secretKey",
});
Obtain the Token through the submit a request for role assumption Temporary Access Credentials API
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: "your-env-id",
sessionToken: "your-sessionToken",
});
Examples
- SCF Environment
- CloudRun Environment
In the SCF environment, the SDK automatically reads authentication information from the environmental variable without manual configuration of authentication parameters.
import tcb from "@cloudbase/node-sdk";
const app = tcb.init({
env: tcb.SYMBOL_DEFAULT_ENV, // Use the default environment ID of the current SCF
});
In a functional managed environment, implement signature-free calls by importing the context parameter.
💡 Note: If the input has the
contextparameter but noenvparameter, then theenvIDparameter incontextwill be used as the environment ID.
import tcb from "@cloudbase/node-sdk";
exports.main = async (event, context) => {
const app = tcb.init({
context,
});
};