This tutorial shows you how to use Alibaba Cloud EMAS Serverless to develop a simple to-do list WeChat Mini Program.
About this tutorial
This tutorial uses a sample to-do list Mini Program to demonstrate how to use the EMAS Serverless software development kit (SDK) in the WeChat IDE to develop a WeChat Mini Program by calling Cloud Database APIs. The sample code already includes the required business logic. You only need to activate EMAS Serverless and configure the Mini Program to run the application.
EMAS Serverless SDK V3.1 is compatible with WeChat. After you import the SDK dependency, you must call user.authorize to grant permissions. For more information, see Install the client SDK.
Preparations
Before you begin, complete the following preparations:
Download and install WeChat Developer Tools.
For more information, see Download Instructions.
ImportantMake sure you download the latest version of the WeChat IDE.
Activate the EMAS service.
Before you use EMAS Serverless for the first time, you must activate the EMAS service. For more information, see the "Activate the EMAS service" section of Create your first service space.
Download the sample Mini Program project.
ImportantThe sample Mini Program project uses EMAS Serverless SDK V3.1. For more information, see Install the client SDK.
Step 1: Create a service space
The cloud resources that you create during mini program development are associated with a service space. Each service space has a globally unique space ID. Cloud resources are associated with your project through this space ID.
For instructions on how to create a service space, see Create your first service space.
Step 2: Set database permissions
Log on to the EMAS console, select Serverless, and click Enter to open the Serverless console.
In the navigation pane on the left, select ApsaraDB. Then, in the upper-left corner of the page, select the service space that you created.
On the ApsaraDB page, click Add Now.
Click your data table, and then select Permissions > Edit Permissions.
NoteTo ensure data security, database access is restricted by default. Set database permissions as needed. For more information, see Data permission management.
On the Permission Management page, click Custom Security Rules and set the write permission to true.
Click OK.
Add a WeChat mini program
Before you start development, add your WeChat Mini Program information to the Alibaba Cloud EMAS Serverless console.
Follow these steps to add the WeChat Mini Program information:
Log on to the EMAS console. Choose Serverless > Click to Enter to go to the Serverless console.
In the navigation pane on the left, you can choose Settings.
On the WeChat tab, click Add Secret.
On the Add WeChat Secret page, enter the required information and click OK.
AppID: Enter the App ID of your WeChat Mini Program.
Log on to the WeChat Official Accounts Platform and obtain the App ID on the Development Settings page.
App Secret: Enter the App Secret for your WeChat Mini Program.
Step 3: Develop a WeChat Mini Program
Follow these steps to develop the WeChat Mini Program:
Before you can use EMAS Serverless to develop a WeChat Mini Program, you must import the EMAS Serverless SDK dependency. This lets you call cloud services such as functions, databases, and storage. The JavaScript (JS) file for the EMAS Serverless SDK is already included in the sample project. When you use the EMAS Serverless SDK in the WeChat IDE, you must import the SDK dependency and then grant authorization. For more information, see Use the SDK in a WeChat Mini Program.
Configure server domain names for the Mini Program
Log on to the WeChat Mini Program platform.
In the navigation pane on the left, click Development and then click Development Settings.
In the Server Domain Name section, click Modify.
In the Configure Server Information panel, enter the required information.
Click the plus sign (+) next to the request valid domain names text box and enter the Mini Program API and file upload Endpoints.
Then, enter the file upload and download Endpoints in the uploadFile valid domain names and downloadFile valid domain names text boxes.
NoteFor more information about server domain name configuration, see the WeChat Mini Program documentation.
You can find the SpaceID, Secret, API Endpoint, and File Upload Endpoint in the EMAS console by choosing Platform Service, and then from the navigation pane on the left. The information is located on the Space Access Information tab of the overview page.
Import the sample Mini Program files
Click here to download the to-do Mini Program sample project files, and then unzip the downloaded file.
In the WeChat Mini Program IDE menu bar, choose to import the decompressed sample files from the previous step.
ImportantYou must replace the AppID in the image with your own WeChat Mini Program App ID.

Click OK.
Configure the app.js file
Open the app.js file, configure the following information, and then save the file.

appId is the ID of the WeChat Mini Program. You can obtain the App ID on the Development Settings page of the WeChat Official Accounts Platform.
The spaceId, clientSecret, and endpoint parameters are available after you create a service space in the EMAS Serverless console. For more information, see Create your first service space.
Local settings
In the upper-right corner of the WeChat IDE, click Details. Select the Compile JS to ES5 and Use npm module options.
Install the SDK
Install the EMAS Serverless SDK dependency in the Mini Program and then build the project.
In the Mini Program terminal, run the npm install command to install the SDK.
In WeChat Developer Tools, click Build npm.
Step 4: Debug the Mini Program
Open the Mini Program emulator. Click Add to add a to-do item.