Call Cloud Functions

Last Updated on : 2024-10-24 10:03:49download

Development with Tuya WeChat Mini Program supports cloud functions. A cloud function includes authentication and interface distribution of the Tuya Developer Platform. You can call cloud functions to easily develop branded WeChat mini programs as required and implement the control and management of smart devices.

Features

Cloud functions of Tuya WeChat Mini Program can be used to realize plenty of features for WeChat mini programs, for example, device control, scene management, and automation. This allows you to effortlessly push the boundaries of WeChat mini program capabilities. The following table lists the capabilities supported by cloud functions.

Capability Description
Device Control UI BizBundle Control a device on the WeChat mini program. The device acts on the received command and then reports the latest status that will be reflected on the control panel.
Device Management Add, query, modify, and delete a device.
Device sharing Share a device with a home member.
Weather Display weather information.
Automation Run automation scenes.
Home management Add, query, modify, and delete a home member.
Room management Add, query, modify, and delete a room.
Push notification Push device data to users.
Scene management Manage scenes for an account. For example, add, modify, query, and delete a scene.
Gather statistics Gather statistics of typical device data, such as energy metering data.
Firmware update Update the firmware version over the air.
Sync data between the WeChat mini program and app Use a WeChat mini program as a lightweight supplement to the app.

Get started with cloud functions

Step 1: Enter AppSecret of WeChat mini program

  1. Enter the App Secret of the WeChat mini program and click Save.

    Call Cloud Functions
  2. You can click How to get the AppSecret to learn more details.

Step 2: Get cloud plug-in

If it is the first time you grant access to the current WeChat mini program, click Apply for the license for the WeChat Mini Program plug-in to request the plug-in.

Call Cloud Functions

Step 3: Run the demo

Go to the details page of the WeChat mini program on the Tuya Developer Platform, and click Run Demo.

You must download and import the demo into the WeChat developer tool to run the demo.

Call Cloud Functions

Step 4: Test the demo

After the above steps are finished, you can interact with the interfaces of the WeChat mini program and call cloud functions to test the WeChat Mini Program SDK.

  • Open the demo project and test the user interfaces.

  • Run the test based on the following JavaScript program:

    import { fetch } from '@ray-js/wechat';
    const params = {
        // action: the interface name. For more information, see the API reference documentation.
        action: "hello",
        // params: the parameters.
        params: {
            hello: 'world'
        }
    };
    
    // Use the fetch API provided in @ray-js/wechat to call the cloud functions.
    // Use the Promise object or async and await keywords to make the API request.
    
    fetch(params).then(res =>{
        console.log('res', res);
    }).catch(err => console.log('err', err))
    

If the response is returned without any error, the SDK configuration is successful. Then, you can proceed with more API requests. For more information, see API List.