Quick Start
Cloud Functions are essentially pieces of code. Here, using Node.js as an example, we introduce how to create a Cloud Function.
Node.js services typically require an entry file index.js. If you use npm packages, a package.json file is also needed to describe dependencies.
Therefore, the most basic Node.js service directory structure is as follows:
└── helloWorld
    ├── index.js
    └── package.json
Creating Cloud Functions
You can create Cloud Functions in the following three ways. Please choose the appropriate method based on your development environment:
- Cloud Development Console
- Mini Program IDE
- Local Creation
Go to Cloud Development Platform/Cloud Functions and click the New Cloud Function button.
- Select the appropriate template. If none is suitable, you can choose to create from scratch.
- Choose the runtime environment as Nodejs 18.15 (other versions are also available).
- Fill in the function name
- Click the Start Creation button
After creation is completed, go to the Cloud Function and you will see a default index.js file.
- Click Save and Install Dependencies below to complete the deployment of the Cloud Function
If you are using the WeChat Developer Tools to develop Cloud Functions, you can directly create Cloud Functions within the WeChat Developer Tools. For steps, refer to Getting Started with Cloud Functions.
- Right-click the cloud function directory and choose New Node.js Cloud Function to create the cloud function

- Write the content of the index.jsfile as follows:
exports.main = async function () {
    return 'Hello World!';
};
- Choose the cloud function directory, right-click, and select Create and Deploy: Cloud-based Dependency Installation to complete the deployment of the Cloud Function

- Locally create an empty folder named cloud-demoas the project root directory
- Go to the root directory and create the functions folder (the directory for storing cloud functions)
- Create the hello_world folder under functions (specific cloud function directory)
- Create index.js in the hello_world cloud function (cloud function entry file)
At this point, the directory structure is as follows:
└── cloud-demo          # Project root directory
    └── functions        # cloud functions directory
        └── hello_world  # Cloud Function instance
            └── index.js # Cloud Function Entry File
- Write the content of the index.jsfile as follows:
exports.main = async function () {
    return 'Hello World!';
};
- Then open a terminal in the current hello_worlddirectory and execute the following command to initialize package.json:
npm init -y
- Install and log in to the CLI tool, then execute the following command in the terminal to install the CLI globally:
For specific details about the CLI tool, refer to: CLI Quick Start
npm i -g @cloudbase/cli
After successful installation, enter the following command to check if the installation was successful:
tcb -v
If you see the version number output, it means the installation was successful.
- CLI Log In
You need to have the CLI log in to the environment where cloud functions are to be deployed and execute the following command:
tcb login
CloudBase CLI will automatically open the Cloud Development Console to obtain authorization. You need to click the Agree Authorization button to allow CloudBase CLI to obtain permissions. If you are not logged in, you will need to log in before performing this operation.
- Run the following command in the project root directory and use the default configuration:
⚠️ Note: You need to obtain the Environment ID of the CloudBase environment
tcb fn deploy hello_world -e <env-id>

Invoking a Cloud Function
Invoking Cloud Functions mainly includes the following methods:
- Use the Cloud Development SDK
- Use the HTTP Access Service
- Use the HTTP API
- Use trigger
Using the SDK to Invoke a Cloud Function
- Mini Program
- Web
- Node.js
wx.cloud
    .callFunction({
        // Cloud function name
        name: 'hello_world',
        // parameters passed to the cloud function
        data: {
            a: 1,
        },
    })
    .then((res) => {
        console.log(res); // Hello World!
    })
    .catch(console.error);
import cloudbase from '@cloudbase/js-sdk';
const app = cloudbase.init({
    env: 'xxxx-yyy',
});
app.callFunction({
    // Cloud function name
    name: 'hello_world',
    // parameters passed to the cloud function
    data: {
        a: 1,
    },
})
    .then((res) => {
        console.log(res); // Hello World!
    })
    .catch(console.error);
const cloudbase = require('@cloudbase/node-sdk');
const app = cloudbase.init({
    env: 'xxxx-yyy',
});
app.callFunction({
    // Cloud function name
    name: 'hello_world',
    // parameters passed to the cloud function
    data: {
        a: 1,
    },
})
    .then((res) => {
        console.log(res); // Hello World!
    })
    .catch(console.error);
Using the HTTP Access Service to Invoke a Cloud Function
You can choose to create an HTTP service to access the cloud function and then invoke the cloud function via HTTP.
// parameters passed to the cloud function
Cloud Development Console
- Create a new "Domain-Associated Resource": - Select the cloud function "hello_world" as the associated resource
- You can select the default domain name or choose your custom domain name
- Enter the trigger path as /hello
 

- Click OK and wait for 3-5 minutes to complete the creation of the HTTP service.

- The HTTP service address is default domain name + trigger path. You can also select to use a custom domain name to bind resources.
CLI Tool
Run the following command to create an HTTP service route with the path /hello pointing to the hello_world cloud function:
tcb service create -p hello -f hello_world -e <env-id>
Then you can go to Cloud Development Platform/HTTP Access Service to view the created HTTP service.
Using the HTTP API to Invoke a Cloud Function
After creating the cloud function, you can invoke it from any client via the HTTP API without needing the SDK.
For details, please refer to Invoking Cloud Functions via HTTP API. To access the HTTP API, an ACCESS_TOKEN is required.
curl -L 'https://your-envId.api.tcloudbasegateway.com/v1/functions/:name' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer <ACCESS_TOKEN>' \
  -d '{}'