Skip to main content

SDK Initialization

init

1. Interface Description

Function: Initialize the SDK instance

Interface declaration: init: (config?: ICloudBaseConfig): CloudBase

2. Input Parameters

FieldTypeRequiredDescription
secretIdstringNoTencent Cloud API fixed key pair secretId. Optional when executed within Cloud Functions. Go to the console to obtain
secretKeystringNoTencent Cloud API fixed key pair secretKey. Optional when executed within Cloud Functions. Go to the console to obtain
sessionTokenstringNoTencent Cloud API temporary key pair Token. Temporary credentials require obtaining temporary access credentials via the sts.AssumeRole interface View documentation
envstringNoTCB Environment ID. When the SDK runs in the TCB Cloud Function environment, it uses the environment ID of the function by default. If running in other environments and this parameter is omitted, the default environment will be used.
timeoutnumberNoTimeout duration for interface calls (ms), defaults to 15000ms (i.e., 15s)
proxystringNoThe http proxy url used when calling interfaces
credentialsobjectNoCloudBase private key, containing two strings private_key and private_key_id, can be obtained by logging into the CloudBase Console
versionstringNoVersion number of the dependent project

In Function-based Cloud Hosting, the SDK can be initialized with the following parameters to achieve signature-free calls:

FieldTypeRequiredDescription
contextContextNoThe context parameter of the entry function in Function-based Cloud Hosting

If the context parameter is passed in but the env parameter is not provided, the envID parameter in context will be used as the environment ID.

3. Response

FieldTypeRequiredDescription
-CloudBaseYestcb object instance

4. Sample Code

import tcb from '@cloudbase/node-sdk'

// Initialize. Under Cloud Functions, secretId and secretKey can be omitted. If env is not specified, the environment ID of the current function's environment will be used.
const app = tcb.init()
const app = tcb.init({env: 'xxx'})
const app = tcb.init({
secretId: 'xxxxx',
secretKey: 'xxxx',
sessionToken: 'xxxx',
env: 'xxx'
})

// Set the request timeout
const app = tcb.init({
timeout: 5000
})

// Initialize the environments 'xx' and 'zz'
const app1 = tcb.init({env: 'xx'})
const app2 = tcb.init({env: 'zz'})

Initializing and calling the SDK in Function-based Cloud Hosting

import tcb from '@cloudbase/node-sdk'

exports.main = async (event, context) => {
const { httpContext } = context
const { url, httpMethod } = httpContext
console.log(`[${httpMethod}][${url}]`)

const tcbapp = tcb.init({ context })
const result = await tcbapp.callFunction({
name: 'helloworld',

// Function-based Cloud Hosting parameters
type: 'cloudrun',
method: 'POST',
path: '/abc',
data: {
key1: 'test value 1',
key2: 'test value 2'
},
{
timeout: 5000
}
})
console.log(result)

// Other business logic, for example:
// Call the container
// Call the database
// Call cloud storage
}