Skip to content
Home » Developer Tutorials » How to Use NodeJS to Send SMS Messages and scheduling

How to Use NodeJS to Send SMS Messages and scheduling

Writer Name: Teniola Fatunmbi

Octopush is an SMS marketing platform that provides multiple SMS-related services for businesses to connect with their customers. Octopus integrates with services you already use, like WooCommerce and Salesforce, and allows you to send customers automated notifications about things like order and shipping statuses. You can also bulk send messages for marketing campaigns or other promotions.

Octopush provides a gateway to use for mobile marketing campaigns, freeing you up to focus on your business.

The Octopush API offers users an easy way to access Octopush’s services. These services include:

  • Sending text messages: Octopush allows you to send text messages to a list of recipients. For example, you might send people who’ve abandoned their shopping cart a reminder to complete the checkout process, or send recent customers a discount on their next purchase.
  • Scheduling text messages: By default, SMS are sent instantly. With Octopush, you can also schedule text messages to be delivered at a certain day and time.
  • Checking phone number validity: If you’re creating a marketing campaign, you want to be sure that you’re spending your marketing budget on reaching your customers—not on sending messages to a number that was disconnected months ago. Octopush allows you to check number validity in real time, allowing you to clean your database and ensure that your campaign dollars are well spent.
  • Sending voice SMS: Octopush allows you to send voice SMS to customers. All you have to do is create the text of the message, and it will automatically be converted to a voice SMS, using a voice in the gender and accent of your choice.

In this article, you’ll learn how to use Node.js and Octopush to send SMS texts, schedule messages, and set up replies.

Prerequisites

  • Working knowledge of Node.js
  • Node.js (version 12+)
  • A code editor to run the code snippets.
  • An Octopush API key and API login. To get this, you’ll need to create an Octopush account, after which you’ll find the API key and login on your dashboard.
  • Axios, which will be used to make requests to the endpoints provided by the API.

Implementing SMS Messages Using Node.js and Octopush API

Now that you understand what Octopush is, you’ll learn how to use Octopush’s API and Node.js to implement SMS messaging and scheduling.

It’s important to note that the configuration for interaction with the Octopush API is a bit different from when you’re using the library. The library consists of a set of functions that do the interaction under the hood. The API, on the other hand, needs you to specify the configurations, either as parameters, in the request headers, or both.

Setting Up SMS Sending

Create your project folder and install the required dependencies.

  • Open the project in your terminal.
  • Run npm install axios.

This process will create your package.json file automatically.

Next, you’ll set up your config file. This config file will contain all the constants in this demo, including a base URL, for cleaner API interactions.

Create a config.js file with the following contents:

const config = {
API_LOGIN: '',
API_KEY: '',
SENDER: '

This will allow access to the config object and base URL anywhere in the demo project.

You also need to create an index.js file in the project folder to test the code implementations.

Do a Credit Check

Before you can send an SMS, you’ll want to check your credit balance with Octopush. Octopush uses a straightforward, price-per-message structure, but to ensure that your messages are sent successfully, you’ll need to ensure you have sufficient credit on your account.

This is the first function to be carried out:

const creditCheck = async() => {
try {
const res = await axios.
get(`${baseURL}/wallet/check-balance`, { headers: {
'api-key': config.API_KEY,
'api-login': config.API_LOGIN
}});

console.log(res.data)
} catch(err){
console.log(err);
}
};
// call the function
creditCheck();

To test this, or any other implementation in this guide, run node index.js in the terminal.

You can also specify the credit account you want to check by adding product_name and country_code to the request parameters:

const params = { country_code: 'EU', product_name: 'sms_premium' };
const res = axios.get(URL, { params, headers });

After verifying that you have enough credit for SMS transactions, you can go ahead with SMS sending.

Sending SMS

Octopush allows you to send an SMS immediately, or to schedule it to be sent later. You can also enable the receipt of replies for SMS you’ve sent. In this section, you’ll learn how to do all three of these things.

Instant SMS Sending

Instant SMS sending allows you to send a message immediately to one or more people. The recipients will be defined as an array in the body of the request.

The recipients array is an array of objects containing each recipient’s details in the following format:

{
phone_number: ,
first_name: ,
last_name: 
}

This array can contain anywhere from one to five hundred contacts.

To send a message, you’ll need the following parameters:

  • Text of the message
  • Recipients array, as shown above
  • Sender
  • Type: [sms_premium(default), sms_low_cost]
  • Purpose

In action, it looks like this:

// For instant SMS sending
const recipients = [
{
'phone_number': '+111222233334444',
'first_name': 'Teniola',
'last_name': 'Fatunmbi'

},
{
'phone_number': '+222333344445555',
'first_name': 'Jacob',
'last_name': 'Brown'
}
]
const sendInstantSMS = async() => {
try{
const res = await axios.post(`${baseURL}/sms-campaign/send`,
{
headers: {
'api-key': config.API_KEY,
'api-login': config.API_LOGIN
}},
{
text: 'I'm sending an SMS through Octopush!',
recipients: recipients,
sender: config.SENDER,
type: 'sms_premium',
purpose: 'wholesale'
}

);
console.log(res.data);
} catch(err){
console.log(err.response.data);
}
};
// call the function
sendInstantSMS();

Scheduled SMS

Scheduled SMS sending allows you to send a message at a specified date and time, sent in the ISO format of YYYY-MM-DD HH:MM:SS. It works in the same manner as the instant SMS sending, except that an additional parameter (send_at) is included.

const sendScheduledSMS = async() => {
try{
const res = await axios.post(`${baseURL}/sms-campaign/send`,

{
headers: {
'api-key': config.API_KEY,
'api-login': config.API_LOGIN
}

},

{
text: 'I'm sending an SMS with Octopush…later.',
recipients: recipients,
sender: config.SENDER,
type: 'sms_premium',
purpose: 'wholesale',

send_at: '2022-01-20 07:40:00'
}
);
console.log(res.data);
} catch(err){
console.log(err.response.data);
};

// call the function
sendScheduledSMS();

Receiving Replies in SMS Sending

SMS sending with Octopush, by default, does not support replies from the recipient. This can be enabled by setting the with_replies parameter to true.

By specifying the with_replies option, replies from the recipients are sent to your Octopush dashboard. The availability of replies depends on the country to which the original message was sent, as not all of them support replies yet. If replies are not supported in your target country, you can still enable them by using a virtual number.

const setReplies = async() => {
try{
const res = await axios.post(`${baseURL}/sms-campaign/send`,
{
headers: {
'api-key': config.API_KEY,
'api-login': config.API_LOGIN
}

},
{
text: 'This is the instant sms test',
recipients: recipients,
sender: config.SENDER,
type: 'sms_premium',
purpose: 'wholesale',
send_at: '2022-01-20 07:40:00',
with_replies: true
}
);
console.log(res.data);
} catch(err){
console.log(err.response.data);
}
}

// call the function
sendReplies();

If you’d like to get replies sent to your email address rather than your Octopush dashboard, you’re able to configure that from your dashboard. To do so, follow the steps below:

  • Go to your dashboard.
  • Select HTTP callback options from the sidebar.
  • Click Edit my callback parameters.
  • Enter the email address at which you’d like to receive replies in the “Email to receive inbound SMS” field.
  • Click Save my callback parameters.

Congratulations! You’ve just implemented SMS sending with Octopush.

Although this tutorial on SMS sending with Octopush shows you how it works in a sample project, it can also be done by creating a file or folder to hold the functions, then exporting them for use. The implementation is up to you.

Quick tip: You can use object destructuring to make accessing the config details neater.

Conclusion

Octopush’s API allows you to send SMS by specifying the processes as parameters. This helps to keep your codebase maintainable and also clean. This article has shown you how you can seamlessly implement SMS sending through Octopush SMS, and with minimal code requirements. Octopush allows you to quickly and easily harness the power of SMS messaging for your marketing campaigns, enabling you to focus your attention on what really matters: your business.