Skip to main content

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:

Go to Cloud Development Platform/Cloud Functions and click the New Cloud Function button.

  1. Select the appropriate template. If none is suitable, you can choose to create from scratch.
  2. Choose the runtime environment as Nodejs 18.15 (other versions are also available).
  3. Fill in the function name
  4. Click the Start Creation button

After creation is completed, go to the Cloud Function and you will see a default index.js file.

  1. Click Save and Install Dependencies below to complete the deployment of the Cloud Function

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

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

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

  1. Go to Cloud Development Platform/HTTP Access Service

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

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

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

See HTTP Access Service

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 '{}'