# Cloud Call

Version requirements: wx-server-sdk 0.4.0 and later; Developer Tools 1.02.1904090 and later (RC version download)

Cloud Call is a capability provided by Cloud Base that supports the use of Mini Program APIs based on cloud functions. Application scenarios include:

# I. Server-side Calls

Cloud call must be used via wx-server-sdk in cloud functions. The access_token is not required when calling a server API via cloud call in cloud functions. Cloud call initiated in a cloud function triggered via the Mini Program is automatically authenticated by WeChat to directly call open APIs, such as sending template message, after the permission is configured. Here is how it is used:

# 1. Check if cloud call is supported for server APIs

The server API list contains all server APIs. APIs that support cloud call are labeled Cloud Call by their names. If the cloud call is supported, specific instructions and usage guide are provided in each server API documentation.

# 2. View cloud call guide in API documentation

The documentation of an API that supports cloud call contains independent sections for HTTPS call and cloud call, and both sections include request parameters, return values and examples.

# 3. Declare APIs required for cloud functions

Next, you need to configure cloud call permission. APIs required by each cloud function should be declared by adding the name of an API to be called in the permissions.openapi field of the config.json configuration file (no need to create one) under the cloud function directory, otherwise, these APIs cannot be called. The value of the permissions.openapi field is a string array, which must be the name of the server API to be called. Permission is updated based on the configuration every time when a cloud function is uploaded using the WeChat Developer Tools. It takes 10 minutes for the configuration to take effect. If there is no permission after update, try again in 10 minutes. The following example shows a configuration file of declaring the API for sending template message.

{
  "permissions": {
    "openapi": [
      "templateMessage.send"
    ]
  }
}

# 4. Use cloud call in cloud functions

First, wx-server-sdk used in cloud function must be 0.4.0 or above. It is recommended to always keep wx-server-sdk up-to-date and ensure the field of wx-server-sdk in package.json under the cloud function directory is latest. For example, if the dependency is installed locally, execute npm install --save wx-server-sdk@latest.

Now, you can use cloud call APIs in cloud functions. Cloud call APIs are all under the openapi object in the wx-server-sdk module. Each open API class is given a level 2 namespace object (e.g. all APIs for template message are under openapi.templateMessage) under the openapi object, where all open methods of this class reside (e.g. the API for sending template message is openapi.templateMessage.send). All primary and secondary class names and methods of each API can be viewed by the API name. All API names are in the <Class>.<Method> format. For example, the name of API for sending the template message is templateMessage.send. Here is an example of sending a template message to yourself:

If you need an example for direct operation, create a project of Cloud Base quick start template in IDE, where an example is given for the cloud call used to send the template message.

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.templateMessage.send({
      touser: cloud.getWXContext().OPENID, // Get OPENID via getWXContext
      page: 'index',
      data: {
        keyword1: {
          value: '339208499'
        },
        keyword2: {
          value: '2015-01-05, 12:30'
        },
        keyword3: {
          value: 'Tencent WeChat Headquarters'
        },
        keyword4: {
          value: 'No. 397, Xingang Zhonglu, Haizhu District, Guangzhou'
        }
      },
      templateId: 'TEMPLATE_ID',
      formId: 'FORMID',
      emphasisKeyword: 'keyword1.DATA'
    })
    // result Structure
    // { errCode: 0, errMsg: 'openapi.templateMessage.send:ok' }
    return result
  } catch (err) {
    // Error processing
    // err.errCode !== 0
    throw err
  }
}

# II. Open Data Calls

From base library 2.7.0 and later, if the Mini Program has already connected to Cloud Base, a unique cloudID matching with the sensitive open data can be obtained from the return value sent via the open data API of the Mini Program. Cloud call can be used to directly get the open data. For details, refer to Directly Get Open Data via Cloud Call.

点击咨询小助手