# templateMessage.send

Call this API at the server side. For more information, see Server API.

This API supports Cloud Calls. The WeChat DevTools version must be 1.02.1904090 or later (download the latest stable version here), and the wx-server-sdk version must be 0.4.0 or later.

Sends a template message.

Calling methods:

# HTTPS Call

# Request Address

POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN

# Request Parameters

Attribute Type Default Required Description
access_token string Yes Credentials to call API
touser string Yes The openid of the receiver (user).
template_id string Yes The ID of the template message to be sent.
page string No The page to be redirected to after the template card is tapped. Only the pages of the current Mini Program are supported. This field can contain parameters (for example, index?foo=bar). If this field is not specified, the template is not redirected to any page.
form_id string Yes The formId added to a submit event when a form is submitted, or the prepay_id of the current payment being made.
data Object No The content of the template. If this field is not specified, an empty template is sent. For its specific format, see the examples.
emphasis_keyword string No The keywords to be enlarged in the template. If this field is not specified, no keywords are enlarged by default.

# Return Value

# Object

JSON data package that is returned

Attribute Type Description
errcode number Error code
errmsg string Error message

Valid values of errcode

Value Description Minimum Version
40037 Incorrect template_id.
41028 Incorrect or timed-out form_id.
41029 form_id is already in use.
41030 Incorrect page.
45009 API calls exceeded the upper limit (each account can call APIs for a maximum of 1,000,000 times per day by default).

# Request Example

{
  "touser": "OPENID",
  "template_id": "TEMPLATE_ID",
  "page": "index",
  "form_id": "FORMID",
  "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"
      }
  },
  "emphasis_keyword": "keyword1.DATA"
}

# Response Example

{
 "errcode": 0,
 "errmsg": "ok"
}

# Cloud Call

Cloud call is a capability provided by Mini Program·Cloud Base that allows you to call WeChat APIs in a cloud function. It must be used via wx-server-sdk in the cloud function.

# API Calling Method

openapi.templateMessage.send

You need to configure the permissions for the templateMessage.send API via config.json. Details

# Request Parameters

Attribute Type Default Required Description
touser string Yes The openid of the receiver (user).
templateId string Yes The ID of the template message to be sent.
page string No The page to be redirected to after the template card is tapped. Only the pages of the current Mini Program are supported. This field can contain parameters (for example, index?foo=bar). If this field is not specified, the template is not redirected to any page.
formId string Yes The formId added to a submit event when a form is submitted, or the prepay_id of the current payment being made.
data Object No The content of the template. If this field is not specified, an empty template is sent. For its specific format, see the examples.
emphasisKeyword string No The keywords to be enlarged in the template. If this field is not specified, no keywords are enlarged by default.

# Return Value

# Object

JSON data package that is returned

Attribute Type Description
errCode number Error code
errMsg string Error message

Valid values of errCode

Value Description Minimum Version
0 Succeeded

# Exceptions

# Object

Thrown Exceptions

Property Type Description
errCode number Error code
errMsg string Error message

Valid values of errCode

Value Description Minimum Version
40037 Incorrect template_id.
41028 Incorrect or timed-out form_id.
41029 form_id is already in use.
41030 Incorrect page.
45009 API calls exceeded the upper limit (each account can call APIs for a maximum of 1,000,000 times per day by default).

# Request Example

const cloud = require('wx-server-sdk')
  cloud.init()
  exports.main = async (event, context) => {
    try {
      const result = await cloud.openapi.templateMessage.send({
        touser: 'OPENID',
        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'
      })
      console.log(result)
      return result
    } catch (err) {
      console.log(err)
      return err
    }
  }

# Response Example

{
  "errCode": 0,
  "errMsg": "openapi.templateMessage.send:ok"
}