Skip to main content

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

  1. Select an appropriate template. If none are suitable, you can choose to create from scratch.

  2. Select the runtime environment as Nodejs 18.15 (you can also choose other versions)

  3. Fill in the function name

  4. Click the Start Creation button

After creation is complete, go to the cloud function and you will see a default index.js file.

  1. Write the index.js file content as follows:
exports.main = async function() {
return "Hello World!";
};
  1. 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

  1. Right-click the cloud functions directory and select New Node.js Cloud Function to create a cloud function.
  1. Write the index.js file content as follows:
exports.main = async function() {
return "Hello World!";
};
  1. 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

  1. Locally create an empty folder as the project's root directory, named cloud-demo (project root directory)
  2. Enter the root directory and create a functions folder (directory for storing cloud functions)
  3. Create a hello_world folder under functions (specific cloud function directory)
  4. 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
  1. Write the index.js file content as follows:
exports.main = async function() {
return "Hello World!";
};
  1. Then open the terminal in the current hello_world folder and execute the following command to initialize package.json
npm init -y
  1. 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.

  1. 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.

  1. 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

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

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

  1. Go to CloudBase/HTTP Access Service

  2. 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

  1. Click OK, wait for 3-5 minutes, and the HTTP service will be created.

  1. 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.