Skip to main content

initialization

Initialization

NPM Version node (scoped)

@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

FieldData TypeRequiredDescription
envstringNoTCB environment ID
accessKeystringNoCloudbase API Key, the highest priority authentication method. It can also be configured through the environment variable CLOUDBASE_APIKEY.
secretIdstringNoTencent 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.
secretKeystringNoTencent 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.
sessionTokenstringNoTencent Cloud API temporary key pair Token, obtained through the role assumption temporary access credential API.
credentialsobjectNoCloudbase 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
contextContextNoFunctional cloud hosting entry function context parameter, used for signature-free calls in cloud hosting scenarios.
timeoutnumberNoAPI call timeout period (ms), defaults to 15000ms, i.e. 15s.
proxystringNohttp proxy url used when calling API.
versionstringNoVersion 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 methodDescription
Environment variable TENCENTCLOUD_SECRETID + TENCENTCLOUD_SECRETKEYNo need to input in init, automatically read secretId and secretKey from environment variables, access with role identity of administrator
Use client Publishable KeyExplicitly 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 KeyNo 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.extendedContextThe managed environment automatically obtains temporary key and environment ID from context

Authentication example

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",
});

Examples

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
});