Quick Start
A cloud function is essentially a piece of code. Here, we use Node.js as an example to demonstrate how to create a cloud function.
Node.js services generally require an entry file index.js. If you use npm packages, you also need a package.json file to describe dependencies.
Therefore, the most basic node service directory structure is as follows:
└── helloWorld
├── index.js
└── package.json
Create Cloud Function
You can create cloud functions in the Cloud Development Console or choose to create them in your local project.
Creating in the CloudBase Console
Go to CloudBase/Cloud Functions and click the New Cloud Function button
Select an appropriate template. If none are suitable, you can choose to create from scratch.
Select the runtime environment as Nodejs 18.15 (you can also choose other versions)
Fill in the function name
Click the Start Creation button
After creation is complete, go to the cloud function and you will see a default index.js file.
- Write the
index.js
file content as follows:
exports.main = async function() {
return "Hello World!";
};
- Click the Save and Install Dependencies button below to complete the deployment of the cloud function.
Creating in the Mini Program IDE
If you are developing cloud functions based on WeChat DevTools, you can directly create cloud functions within WeChat DevTools. For steps, refer to Cloud Functions Quick Start
- Right-click the cloud functions directory and select New Node.js Cloud Function to create a cloud function.

- Write the
index.js
file content as follows:
exports.main = async function() {
return "Hello World!";
};
- Select the cloud function directory, right-click, and choose Create and Deploy: Cloud-based Dependency Installation to complete the deployment of the cloud function.

Create Locally
- Locally create an empty folder as the project's root directory, named
cloud-demo
(project root directory) - Enter the root directory and create a functions folder (directory for storing cloud functions)
- Create a hello_world folder under functions (specific cloud function directory)
- Create index.js in the cloud function hello_world (entry file for the cloud function)
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
index.js
file content as follows:
exports.main = async function() {
return "Hello World!";
};
- Then open the terminal in the current
hello_world
folder and execute the following command to initialize package.json
npm init -y
- Install and log in to the CLI tool, and execute the following command in the terminal to install cli globally:
For specific details of the CLI tool, refer to: CLI Quick Start
npm i -g @cloudbase/cli
After successful installation, enter the following command to verify the installation:
tcb -v
If the version number is displayed, the installation was successful.
- Log in to the cli
You need to log in to the environment where the cloud function will be deployed using the cli and execute the following command:
tcb login
CloudBase CLI will automatically open the CloudBase Console to obtain authorization. You need to click the Agree to Authorize button to allow CloudBase CLI to obtain authorization. If you are not logged in, you need to log in before you can perform 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 Cloud Functions
There are two methods to invoke cloud functions:
- Use the Cloud Development SDK
- Use the HTTP Access Service
Invoking Cloud Functions Using the SDK
- 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);
//Initialize the SDK instance
const cloudbase = require("@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);
Invoking Cloud Functions Using HTTP
You can choose to create an HTTP service to access cloud functions and then invoke cloud functions via HTTP.
There are two operation methods:
CloudBase Console
Create a Domain-Associated Resource
- Associate Resource: select cloud function, hello_world
- Select the default domain or your custom domain
- Fill in the trigger path: /hello
- Click OK, wait for 3-5 minutes, and the HTTP service will be created.
- The HTTP service address is
default domain + trigger path
. You can also choose to bind resources with a custom domain.
cli tools
For details, refer to http Access Service
Execute the following command to create an HTTP service route with the path /hello
pointing to the cloud function hello_world
:
tcb service create -p hello -f hello_world -e <env-id>
You can then go to CloudBase/HTTP Access Service to view the created HTTP service.