Skip to main content

Initialize

NPM Version node (scoped)

@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 env parameter is not specified during initialization init({}), the current Cloud Function Environment ID will be used by default instead of the TCB default environment. To use the TCB default environment, specify init({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

FieldTypeRequiredDescription
envstringNoTCB Environment ID
secretIdstringNoTencent Cloud API fixed key pair secretId, can be omitted when executing within a cloud function. Go to the console to obtain
secretKeystringNoTencent Cloud API fixed key pair secretKey, can be omitted when executing within a cloud function. Go to the console to obtain
sessionTokenstringNoTencent Cloud API temporary key pair Token. Temporary credentials need to be obtained through the sts.AssumeRole interface. View documentation
credentialsobjectNoCloudbase private key, which contains two strings: private_key and private_key_id. You can log in to the TCB platform to obtain it.
contextContextNoThe context parameter for the entry function of Function-as-a-Service (FaaS) hosting, enabling signature-free invocation in hosting scenarios.
timeoutnumberNoTimeout duration for interface calls (ms), defaults to 15000ms (i.e., 15s)
proxystringNohttp proxy url used when calling interfaces
versionstringNoVersion 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 context parameter is passed without the env parameter, the envID parameter from context will 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:

import tcb from '@cloudbase/node-sdk'

const app = tcb.init({
env: "your-env-id",
secretId: "your-secretId",
secretKey: "your-secretKey",
})