# Use a template

Debugging Tools

Interface should be called on the server side, not in the front end (Weixin Mini Program, web pages, APP, etc.) directly called, specific reference interface call guide

Interface name: addwxanewtemplate

Select a template from a public template library to a private template library

# 1. How to call

# HTTPS calls

POST https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token=ACCESS_TOKEN

# Cloud Calls

  • Call method: subscribeMessage.addTemplate

  • The input and exit parameters are the same as the HTTPS call, which can be called in the cloud call documentation

# Third party invocation

  • This interface supports Third Party Platform generation business call.

  • This interface belongs to the permission set id: 18,89

  • When a service provider is authorized by one of the permissions set, it can call on behalf of the merchant by using authorizer_access_token , which can be viewed in the third-party call documentation.

# 2. Request parameters

# Query parametersQuery String parameters

Parameter NametypeRequired to fill inExampleIntroductions
access_tokenstringyesACCESS_TOKENInterface invocation credentials, using access_token , authorizer_access_token

# Request BodyRequest Payload

Parameter NametypeRequired to fill inIntroductions
tidstringyesTemplate title id, can be obtained through the interface, you can also log in Weixin Mini Program background view to get
kidListnumarrayyesA template keyword list composed by the developer. The keyword order can be freely paired (e.g. [3,5,4] or [4,5,3]), and up to five and a minimum of two keyword combinations can be supported
sceneDescstringyesDescription of the service scenario, within 15 words

# 3. Return Parameters

# Response Payload

Parameter NametypeIntroductions
errcodenumberError code
errmsgstringError message
priTmplIdstringAdded to the template id under account, required to send Weixin Mini Program subscription message

# 4. Note

  1. Template title id can be accessed via interface or viewed in background
  2. Keyword combination needs 2-5 words
  3. Service scenario description limited to 15 words

# 5. Code examples

# 5.1 Use a template

Example Requests

{
  "tid": "401",
  "kidList": [
    1,
    2
  ],
  "sceneDesc": "测试数据"
}

Return an example

{
  "errmsg": "ok",
  "errcode": 0,
  "priTmplId": "9Aw5ZV1j9xdWTFEkqCpZ7jWySL7aGN6rQom4gXINfJs"
}

# 5.2 Cloud function call example

Example Requests

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi({ convertCase: false }).subscribeMessage.addTemplate({
        "tid": '401',
        "kidList": [
          1,
          2
        ],
        "sceneDesc": '测试数据'
      })
    return result
  } catch (err) {
    return err
  }
}

Return an example

{
  "errMsg": "openapi.subscribeMessage.addTemplate:ok",
  "errCode": 0,
  "priTmplId": "9Aw5ZV1j9xdWTFEkqCpZ7jWySL7aGN6rQom4gXINfJs"
}

# 6. Error code

The following is a list of error codes for this interface, other error codes can refer to General error codes

Error codeError DescriptionSolutions
-1system errorThe system is busy, so the developer is asked to try again in a few minutes.
40001invalid credential  access_token isinvalid or not latestAppSecret error while getting access_token, or access_token is invalid.Developers should check that AppSecret is correct, or that they are calling interfaces for the appropriate Official Account
40400HTTP Message Not Compliant (HTTP Bad Request)Developer fixes
200001A system errorPlease try again later.
200011This account has been blocked.undefined
200012The maximum number of private templates has been reachedundefined
200013This template has been blockedundefined
200014Template tid parameter errorundefined
200020Keyword list kidList parameter errorundefined
200021Scene description sceneDesc parameter errorundefined
200022A template for understanding titles and keywords already existsChange titles and keywords and try again
200100The type of account is not legal.Please use the Weixin Mini Program account to call

# 7. Scope of application

How this interface can be invoked under different account types:
Weixin Mini Program Official Account Service Account MiniGame
Certification only Certification only
  • ✔: The account can call this interface
  • Authentication only: means that only authenticated accounts are allowed to be invoked by the enterprise entity, and accounts that are not authenticated or do not support authentication cannot be invoked.
  • Other account types that are not expressly stated may not be called on this interface without special instructions;