Initialize
@cloudbase/manager-node supports developers in performing operations such as creation, management, and configuration on resources provided by TCB, including SCF, databases, and file storage, through APIs.
Install SDK
- npm
- yarn
- pnpm
npm install @cloudbase/manager-node -S
yarn add @cloudbase/manager-node
pnpm add @cloudbase/manager-node
Initialize SDK
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
secretId: "your-secret-id",
secretKey: "your-secret-key",
envId: "your-env-id" // Replace with your TCB environment ID
})
// Obtain all feature modules
const { database, functions, storage, env, commonService } = app
If you need to manage TCB services under multiple Tencent Cloud accounts, you can also choose to use the constructor method new CloudBase() to initialize the SDK. Each initialization will result in a new CloudBase instance.
import CloudBase from '@cloudbase/manager-node'
// Manage account A's environment
const appA = new CloudBase({
secretId: "account-a-secret-id",
secretKey: "account-a-secret-key",
envId: "account-a-env-id"
})
// Manage account B's environment
const appB = new CloudBase({
secretId: "account-b-secret-id",
secretKey: "account-b-secret-key",
envId: "account-b-env-id"
})
Initialization Parameters
| Field | Type | Required | Description |
|---|---|---|---|
envId | string | No | TCB environment ID, obtained from the overview page of the TCB console. If not filled, the default environment is used because many subsequent APIs depend on the environment. If it is not passed, you need to add the environment via addEnvironment() to perform subsequent API calls. |
secretId | string | No | Tencent Cloud API fixed key pair secretId. Go to Tencent Cloud Console / API Key Management to generate. secretId and secretKey must be passed together. When executing in SCF, it can be left blank. |
secretKey | string | No | Tencent Cloud API fixed key pair secretKey. Go to Tencent Cloud Console / API Key Management to generate. secretId and secretKey must be passed together. When executing in SCF, it can be left blank. |
token | string | No | Tencent Cloud API temporary key pair Token, obtained via the Apply for AssumeRole Temporary Access Credentials API. Passing this field means using temporary credentials. If you explicitly pass temporary credentials, this parameter is required. |
timeout | number | No | Timeout period for API calls in milliseconds, default is 15000ms, i.e., 15s. |
proxy | string | No | http proxy url used in API calls |
region | string | No | Specify the region. List of currently supported regions reference. In SCF environment, defaults to the current SCF environment region. |
- In a server-side environment (non-SCF environment), you need to pass
secretIdandsecretKey| - If the current account is a sub-account, please first have the root account authorize and enable
QcloudTCBFullAccess(Full read/write access to TCB) andQcloudAccessForTCBRole(TCB's access permissions to cloud resources). Sub-account Permission Settings Guide
Login Authentication
manager-node uses administrator permissions. The authentication methods in different environments are as follows:
SCF Environment
In the SCF environment, there is no need to manually configure authentication parameters.
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
envId: "your-env-id"
})
Node.js Environment
In a Node.js environment, you need to configure any one of the following authentication methods:
- Use secretId and secretKey
- Use temporary token
Go to Tencent Cloud Console / API Key Management to generate secretKey and secretId
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
envId: "your-env-id",
secretId: "your-secret-id",
secretKey: "your-secret-key"
})
Obtain the token via the Apply for AssumeRole Temporary Access Credentials API.
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
envId: "your-env-id",
token: "your-token"
})
Environment Management
Core Concepts
"@cloudbase/manager-node supports managing multiple TCB environments. Internally, the SDK manages all environment instances through EnvironmentManager and maintains a current environment concept:"
- Environment instance (Environment): Represents a TCB environment, where each environment has independent resources (SCF, database, storage, etc.)
- Current Environment: The environment that the SDK operates on by default. All resource management APIs (such as
app.functionsandapp.database) act on the current environment. - Environment Manager (EnvironmentManager): Manages multiple environment instances and supports adding and switching environments.
Single Environment Usage
In most scenarios, you only need to manage one environment:
import CloudBase from '@cloudbase/manager-node'
// When envId is specified during initialization, this environment automatically becomes the current environment.
const app = CloudBase.init({
secretId: "your-secret-id",
secretKey: "your-secret-key",
envId: "your-env-id"
})
// Directly operate on the resources of the current environment.
await app.functions.getFunctionDetail("test")
await app.database.listCollections()
Multi-Environment Management
When you need to manage multiple environments (e.g., simultaneously operating development and production environments), you can use the environment manager:
import CloudBase from '@cloudbase/manager-node'
const app = CloudBase.init({
secretId: "your-secret-id",
secretKey: "your-secret-key",
envId: "dev-env-id" // Development environment as the initial current environment
})
// Obtain the environment manager
const envManager = app.getEnvironmentManager()
// Add production environment
envManager.addEnvironment("prod-env-id")
// Obtain the current environment (currently dev-env-id)
const currentEnv = envManager.currentEnvironment()
console.log(currentEnv.EnvId) // Output: dev-env-id
// Operate the SCF in the development environment
await app.functions.getFunctionDetail("test")
// Switch to the production environment
envManager.switchEnv("prod-env-id")
// Now operating the SCF in the production environment
await app.functions.getFunctionDetail("test")
addEnvironment(envId)does not create a new environment in the TCB service; the environment corresponding to envId must exist in advance.- If
envIdis not specified during initialization, the first environment added will automatically become the current environment. - After calling
switchEnv(envId), all resource management objects (such asfunctionsanddatabase) will act on the new current environment.
Environment Management API
| Method | Description | Return Value |
|---|---|---|
getEnvironmentManager() | Obtain the environment manager instance | EnvironmentManager |
addEnvironment(envId) | Add an environment instance to the manager | void |
currentEnvironment() | Obtain the current environment instance | Environment |
switchEnv(envId) | Switch the current environment | void |
Feature Modules
All resource management capabilities are associated with the current environment and accessed via the following properties:
const app = CloudBase.init({
secretId: "your-secret-id",
secretKey: "your-secret-key",
envId: "your-env-id"
})
// Obtain all feature modules in the current environment
app.functions // SCF management
app.database // Database Management
app.storage // File Storage Management
app.env // Environment Configuration Management (including custom domains and access routing, v5.0.0+)
app.commonService // Common Service Management
app.hosting // Static Website Hosting
app.cloudBaseRun // Cloud Run (TCBR) Management
app.cloudAppService // Application Deployment Management (v5.0.0+)
app.log // CLS Log Service (v5.0.0+)
app.permission // Permission and Role Management (v5.0.0+)
app.mysql // MySQL Database Management (v5.0.0+)
app.docs // Official Documentation Search and Retrieval (v5.0.0+)
Each module provides complete resource management capabilities. For details, see the API documentation for each feature module.
Sample Code
Basic Usage
import CloudBase from '@cloudbase/manager-node'
// 1. Initialize CloudBase
const app = CloudBase.init({
secretId: "your-secret-id",
secretKey: "your-secret-key",
envId: "your-env-id"
})
async function test() {
// 2. Call getFunction under SCF management
let result = await app.functions.getFunctionDetail("test")
// 3. Print result
console.log(result)
}
test()