This topic describes how to use the Alibaba Cloud SDK for Node.js to call the SendMessageWithTemplate operation. This API operation allows you to send text messages to Mainland China.
Request parameters
Parameter | Type | Required | Example | Description |
---|---|---|---|---|
To | String | Yes | 45***0121 | The mobile phone number to receive the text message. Note that you must add the country code to the beginning of the mobile phone number. |
From | String | No | XXCompany | The ID of the sender. This parameter specifies the text message signature when you send the text message to Mainland China. |
TemplateCode | String | Yes | SMS_0000 | The ID of the text message template. You can view the ID of the text message template in the console of Short Message Service (SMS). |
TemplateParam | String | No | {"code":"1234","product":"ytx"} | The values of the variables that are included in the text message template. The values must use the JSON format. If line feeds are required in JSON-formatted data, the line feeds must meet the relevant requirements that are specified in the standard JSON protocol. |
SmsUpExtendCode | String | No | 90999 | The extension code of the upstream text message. This parameter is optional. You can skip this parameter based on your business requirements. |
Response parameters
Parameter | Type | Example | Description |
---|---|---|---|
To | String | 45***0121 | The mobile phone number that received the text message. Note that the country code is added to the beginning of the mobile phone number. |
MessageId | String | 100***03003003 | The ID of the text message. You can call the QueryMessage operation to query the delivery status of the text message based on the ID of the text message. |
ResponseCode | String | OK | The status code of the request. If the API operation returns OK, the request is successful. For more information about the other error codes, see Error codes. |
ResponseDescription | String | The text message is received. | The description of the status code. |
Segments | String | 1 | The number of the text messages that incurred fees. |
Install the core library of the Alibaba Cloud SDK for Node.js
- Method 1: Use the npm and Yarn package managers.
npm install @alicloud/pop-core
- Method 2: Download the openapi-core-nodejs-sdk library by using the following link: Core library of the Alibaba Cloud SDK for Node.js. The Alibaba Cloud SDK for Node.js allows you to call SMS API operations.
Sample requests
The considerations are described as follows:
- You must use the default value of the RegionId parameter: ap-southeast-1.
- You must use the default value of the endpoint parameter: https://dysmsapi.ap-southeast-1.aliyuncs.com.
- You must use the default value of the apiVersion parameter: 2018-05-01.
const Core = require('@alicloud/pop-core');
var client = new Core({
accessKeyId: '<accessKeyId>',
accessKeySecret: '<accessSecret>',
endpoint: 'https://dysmsapi.ap-southeast-1.aliyuncs.com',
apiVersion: '2018-05-01'
});
var params = {
"RegionId": "ap-southeast-1",
"To": "8615200000000",
"From": "SMS signature",
"TemplateCode": "SMS_225001",
"TemplateParam": "{\"code\":\"1234\"}",
"SmsUpExtendCode": "12345"
}
var requestOption = {
method: 'POST'
};
client.request('SendMessageWithTemplate', params, requestOption).then((result) => {
console.log(result);
}, (ex) => {
console.log(ex);
})