· 5 years ago · Jul 20, 2020, 08:12 PM
1var https = require('follow-redirects').https;
2
3var options = {
4 'method': 'POST',
5 'hostname': 'api.sms.to',
6 'path': '/sms/send',
7 'headers': {
8 'Content-Type': 'application/json',
9 'Authorization': 'Bearer <YOUR_API_KEY_OR_ACCESS_TOKEN>'
10 },
11 'maxRedirects': 20
12};
13
14var req = https.request(options, function (res) {
15 var chunks = [];
16
17 res.on("data", function (chunk) {
18 chunks.push(chunk);
19 });
20
21 res.on("end", function (chunk) {
22 var body = Buffer.concat(chunks);
23 console.log(body.toString());
24 });
25
26 res.on("error", function (error) {
27 console.error(error);
28 });
29});
30
31var postData = "{\n \"message\": \"This is test\",\n \"to\": \"+9779856034616\",\n \"sender_id\": \"SMSto\",\n \"callback_url\": \"https://example.com/callback/handler\"\n}";
32
33req.write(postData);
34
35req.end();
36
37OR
38// Using API Key via Query Param
39var https = require('follow-redirects').https;
40
41var options = {
42 'method': 'GET',
43 'hostname': 'api.sms.to',
44 'path': '/sms/send?api_key={api_key}&to=9779856034616&message=test&sender_id=smsto',
45 'maxRedirects': 20
46};
47
48var req = https.request(options, function (res) {
49 var chunks = [];
50
51 res.on("data", function (chunk) {
52 chunks.push(chunk);
53 });
54
55 res.on("end", function (chunk) {
56 var body = Buffer.concat(chunks);
57 console.log(body.toString());
58 });
59
60 res.on("error", function (error) {
61 console.error(error);
62 });
63});
64
65var postData = "";
66
67req.write(postData);
68
69req.end();
70
71 RESPONSE:
72
73{
74 "message": "Message is queued for sending! Please check report for update",
75 "success": true,
76 "message_Id": "e7745289-7236-497f-acf2-f9cfd6a86f16"
77}