How to schedule an SMS in Node.js

The simplest way to send SMS from Node.Js is to use the built in HTTP/Rest SMS api of Ozeki SMS Gateway. When you use this API, you will send SMS messages by issuing a HTTP Post request to the SMS gateway. The HTTP Post request will contain a message formatted in json format. The SMS gateway will send this SMS to the recipient phone, and it will return a HTTP 200 OK response to your request.

how to send scheduled sms message using node js
Figure 1 - How to send a scheduled SMS from Node.Js

JavaScript code to send sms to mobile

The JavaScript sms code sample below demonstrates how you can send SMS using the http rest sms api of Ozeki SMS Gateway using the JavaScript Ozeki.Libs.Rest library. This library is provided to you free of charge, and you may use it and modify it in any of your projects.

SendScheduledSms.mjs
import { Configuration, Message, MessageApi } from './Ozeki.Libs.Rest.js';


var configuration = new  Configuration();
configuration.Username = "http_user";
configuration.Password = "qwe123";
configuration.ApiUrl = "http://127.0.0.1:9509/api";

var msg = new Message();
msg.ToAddress = "+36201111111";
msg.Text = "Hello world!";
msg.TimeToSend = new Date(Date.UTC(2021, 5, 21, 14, 55, 0));

var api = new MessageApi(configuration);

const result = await api.Send(msg);

console.log(result.toString());
	

How to use the JavaScript sms example:

You can use the Message class to create the SMS and you can use the MessageApi class to send the SMS to the SMS gateway. The SMS gateway will forward your message to the mobile network either through a wireless connection or through the Internet.

Download SendScheduledSms.js

The source code explained in this article can be downloaded and used and modified free of charge.
Download: SendScheduledSms.js.zip (4.46Kb)

What is in the SendScheduledSms.js.zip file?

The SendScheduledSms.js.zip file contains the Ozeki.Libs.Rest library, which gives you all the tools necessary to send and receive SMS messages. You will also find a SendScheduledSms.mjs file in the project, which contains the example code to show you how to send an SMS. This example code is listed below.

Figure 2 - SendScheduledSms.js directory

This project has dependencies, so before you start you have to install the node-fetch package with the following command:

npm install node-fetch
	

Code 1 - Command to install the node-fetch package

.

How to send a scheduled SMS from Node.Js (Quick steps)

To send a scheduled sms from Node.Js:

  1. Install Ozeki SMS Gateway
  2. Connect Ozeki SMS Gateway to the mobile network
  3. Send a test sms from Ozeki GUI
  4. Create a HTTP sms api user
  5. Apache NetBeans
  6. Download the example project above
  7. Create the SMS by creating a new Message object
  8. Create an api to send your message
  9. Use the Send method to send your message
  10. Read the response message on the console
  11. Check the logs in the SMS gateway

Install Ozeki SMS Gateway and create an HTTP API user

To be able to send SMS from Node.Js, first you need to install Ozeki SMS Gateway. The SMS gateway can be installed on the same computer, where you develop your JavaScript code in Visual Studio Code or any other code editor. After installation, the next step is to connect Ozeki SMS Gateway to the mobile network. You can send a test sms from the Ozeki GUI to verify, that your mobile network connection works. The final step to prepare your environment is to create a HTTP sms api user. Create a user with a username of "http_user", and with a password of "qwe123" to make the example work without modification.

After the environment is setup, you can run your JavaScript code.

HTTP API url to use send sms from Node.Js

To send SMS from Node.Js, your JavaScript will have to issue an HTTP request to the SMS gateway. The API url is shown below. Note that the IP address (127.0.0.1) should be replaced to the IP address of your SMS gateway. If Ozeki SMS Gateway is installed on the same computer where the JavaScript sms application is running, this can be 127.0.0.1. If it is installed on a different computer, it should be the IP address of that computer.

http://127.0.0.1:9509/api?action=rest
	

HTTP authentication to use send sms from JavaScript

To authenticate the Node.Js sms client, you need to send the username and password in a base64 encoded string to the server in a HTTP request. The format used is: base64(username+":"+password). In JavaScript you can use the following code to do this encoding:

var usernamePassword = username + ":" + password;
return `Basic ${Base64.encode(usernamePassword)}`;
	

For example if you encode the username 'http_user' and the password 'qwe123', you will get the following base64 encoded string: aHR0cF91c2VyOnF3ZTEyMw==. To send

HTTP request header to send SMS from JavaScript

To send the SMS messages, you need to include the following lines as headers in the HTTP request. Note that we include a content type and an Authorization header.

Content-Type: application/json
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
	

HTTP request to send SMS from Node.Js

To submit the SMS, your Node.Js application will send an HTTP request similar to the one below. Note, that this request contains a HTTP header part and a http body part. The HTTP body is a JSON encoded data string. It contains the recipient's number and the message's text.

POST /api?action=sendmsg HTTP/1.1
Connection: close
Content-Length: 319
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate
Authorization: Basic aHR0cF91c2VyOnF3ZTEyMw==
Host: 127.0.0.1:9509
User-Agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
{
	"messages":	[
		{
			"message_id":	"7f9016d5-1e23-d85d-b425-30a435af529e",
			"to_address":	"+36201111111",
			"text":	"Hello world!",
			"create_date":	"2021-06-14T10:16:21",
			"vaild_date":	"2021-06-21T10:16:21",
			"time_to_send":	"2021-06-14T10:20:00",
			"submit_report_requested":	true,
			"delivery_report_requested":	true,
			"view_report_requested":	true
		}
	]
}
	

HTTP response received by the JavaScript sms example

Once the SMS gateway receives this request, it will generate a HTTP response. The HTTP response will contain a status code, to indicate whether the SMS submit request was successful or not. It will also return a JSON encoded structure to provide you useful details about the message's submission.

HTTP/1.1 200 OK
User-Agent: OZEKI 10.3.120 (www.myozeki.com)
Content-Type: application/json; charset=utf8
Last-Modified: Mon, 14 Jun 2021 09:12:00 GMT
Server: 10/10.3.120
Transfer-Encoding: chunked
{
	"http_code": 200,
	"response_code": "SUCCESS",
	"response_msg": "Messages queued for delivery.",
	"data": {
	  "total_count": 1,
	  "success_count": 1,
	  "failed_count": 0,
	  "messages": [
	    {
	      "message_id": "7f9016d5-1e23-d85d-b425-30a435af529e",
	      "from_station": "%",
	      "to_address": "+36201111111",
	      "to_station": "%",
	      "text": "Hello world!",
	      "create_date": "2021-06-14 10:16:21",
	      "valid_until": "2021-06-21 10:16:21",
	      "time_to_send": "2021-06-14 10:20:00",
	      "submit_report_requested": true,
	      "delivery_report_requested": true,
	      "view_report_requested": false,
	      "tags": [
	        {
	          "name": "Type",
	          "value": "SMS:TEXT"
	        }
	      ],
	      "status": "SUCCESS"
	    }
	  ]
	}
}
	

How to send SMS from Node.Js using the sms api (Video tutorial)

This video shows you how to download and use the SendScheduledSms.js project. Once you added the needed files to your project, you might notice that a there is a file called Ozeki.Lbis.Rest.js. This is the file that contains the MessageApi and all the stuffs you need to send an SMS using JavaScript.

Video 1 - How to download Ozeki.Libs.Rest and set up your project (Video tutorial)

Node.Js sms example: SendScheduledSms.mjs (in Notepad)

On Figure 3, you can see the source code of the SendScheduledSMS project, opened on Notepad. You can use it to edit the code if you are familiar with the Node.js programming language. Change the values of the variables to send a custom message to a custom address.

javascript example to send scheduled sms
Figure 3 - SendScheduledSms.mjs in Notepad

Node.Js sms example: SendScheduledSms.mjs (in Visual Studio Code)

If you wish to use an IDE with color-coding, extensions, and helpful documentation, use the Visual Studio Code (Figure 4). It is a free-to-use IDE that will help you build better programs. If you are familiar with the Node.js programming language, feel free to adjust the values of the variables to send a custom message to a custom address.

javascript example to send scheduled sms
Figure 4 - SendScheduledSms.mjs in Visual Studio Code

How to check that the SMS has been accepted by the HTTP user

After the SMS has been submitted, it is a good idea to check your SMS gateway, to see what it has received. You can check the log by opening the HTTP user's details from the Ozeki SMS Gateway management console. The following video shows you what to look for.

Video 2 - How to send scheduled SMS messages with the JavaScript code above (Video tutorial)

How to check that the SMS has been sent to the mobile network

The final step in verifying the procedure is to take a look at the logs of the mobile network connection. You might have to turn on logging in the configuration of the connection before you send the message to see the logs. If logging is enabled, you will see the phone number and the text of the message you have sent.

Conclusion

The main function of this article was to show you how to send scheduled SMS messages from Node.js using the HTTP user of the Ozeki SMS Gateway. If you have followed the steps attentively, you should easily schedule SMS messages with your Node.js program. To share information most effectively you must put a lot of emphasis on timing because it can keep the message traffic balanced and promotes smoother communication.

Continue reading guidance on the Ozeki webpage for more information about how to develop your messaging system. Find other Node.js tutorials like How to receive an SMS in Node.js and much more.

Download the Ozeki SMS Gateway now and let the work begin!

More information