# Use a template
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 Name | type | Required to fill in | Example | Introductions |
|---|---|---|---|---|
| access_token | string | yes | ACCESS_TOKEN | Interface invocation credentials, using access_token , authorizer_access_token |
# Request BodyRequest Payload
| Parameter Name | type | Required to fill in | Introductions |
|---|---|---|---|
| tid | string | yes | Template title id, can be obtained through the interface, you can also log in Weixin Mini Program background view to get |
| kidList | numarray | yes | A 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 |
| sceneDesc | string | yes | Description of the service scenario, within 15 words |
# 3. Return Parameters
# Response Payload
| Parameter Name | type | Introductions |
|---|---|---|
| errcode | number | Error code |
| errmsg | string | Error message |
| priTmplId | string | Added to the template id under account, required to send Weixin Mini Program subscription message |
# 4. Note
- Template title id can be accessed via interface or viewed in background
- Keyword combination needs 2-5 words
- 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 code | Error Description | Solutions |
|---|---|---|
| -1 | system error | The system is busy, so the developer is asked to try again in a few minutes. |
| 40001 | invalid credential access_token isinvalid or not latest | AppSecret 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 |
| 40400 | HTTP Message Not Compliant (HTTP Bad Request) | Developer fixes |
| 200001 | A system error | Please try again later. |
| 200011 | This account has been blocked. | undefined |
| 200012 | The maximum number of private templates has been reached | undefined |
| 200013 | This template has been blocked | undefined |
| 200014 | Template tid parameter error | undefined |
| 200020 | Keyword list kidList parameter error | undefined |
| 200021 | Scene description sceneDesc parameter error | undefined |
| 200022 | A template for understanding titles and keywords already exists | Change titles and keywords and try again |
| 200100 | The type of account is not legal. | Please use the Weixin Mini Program account to call |
# 7. Scope of application
| 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;