Initialize
@Cloudbase/node-sdk enables you to access Cloudbase services and resources using Node.js services on the server side.
⚠️ Note: Starting from v3, if the
envparameter is not specified during initializationinit({}), the current Cloud Function Environment ID will be used by default instead of the TCB default environment. To use the TCB default environment, specifyinit({env: tcb.SYMBOL_DEFAULT_ENV}).
Install the SDK
# npm
npm install @cloudbase/node-sdk -S
# yarn
yarn add @cloudbase/node-sdk
Initialize the SDK
Basic Usage
import tcb from '@cloudbase/node-sdk'
// The Cloud Function environment uses the commonjs module system
// const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: env: "your-env-id", // Replace with your Environment ID
})
Initialization Parameters
| Field | Type | Required | Description |
|---|---|---|---|
env | string | No | TCB Environment ID |
secretId | string | No | Tencent Cloud API fixed key pair secretId, can be omitted when executing within a cloud function. Go to the console to obtain |
secretKey | string | No | Tencent Cloud API fixed key pair secretKey, can be omitted when executing within a cloud function. Go to the console to obtain |
sessionToken | string | No | Tencent Cloud API temporary key pair Token. Temporary credentials need to be obtained through the sts.AssumeRole interface. View documentation |
credentials | object | No | Cloudbase private key, which contains two strings: private_key and private_key_id. You can log in to the TCB platform to obtain it. |
context | Context | No | The context parameter for the entry function of Function-as-a-Service (FaaS) hosting, enabling signature-free invocation in hosting scenarios. |
timeout | number | No | Timeout duration for interface calls (ms), defaults to 15000ms (i.e., 15s) |
proxy | string | No | http proxy url used when calling interfaces |
version | string | No | Version number, the version number of the dependent project |
Sign-in Authentication
node-sdk uses administrator permissions. Authentication methods in different environments are as follows:
Cloud Function Environment
In the Cloud Function environment, manual configuration of authentication parameters is not required.
import tcb from '@cloudbase/node-sdk'
const app = tcb.init({
env: env: tcb.SYMBOL_DEFAULT_ENV // Use the current Cloud Function default environment ID
})
Cloud Hosting Environment
In the Function-as-a-Service (FaaS) hosting environment, signature-free invocation is achieved by passing the context parameter.
💡 Note: If the
contextparameter is passed without theenvparameter, theenvIDparameter fromcontextwill be used as the environment ID.
import tcb from '@cloudbase/node-sdk'
exports.main = async (event, context) => {
const app = tcb.init({
context
})
}
Node.js Environment
In the Node.js environment, you need to configure one of the following authentication methods:
- Using secretId and secretKey
- Using Temporary Credentials
import tcb from '@cloudbase/node-sdk'
const app = tcb.init({
env: "your-env-id",
secretId: "your-secretId",
secretKey: "your-secretKey",
})
import tcb from '@cloudbase/node-sdk'
const app = tcb.init({
env: "your-env-id",
sessionToken: "your-sessionToken",
})