Codementor Events

Send a Text/SMS using Node.js

Published Jul 15, 2017Last updated Jan 23, 2019

So you have learned Node.js and you want to do something out of the ordinary by allowing your app to communicate with users real time. You could build an app for an event promoter that sends texts every Friday/Saturday evening promoting an event and even handle text responses or you could just build an app that texts every friend of yours once a week to stay in touch.

To send a text programmatically your code needs to make an XHR request to an SMS gateway that will read the text details from your request and then initiate a text to the recipient's phone number 
       
Free CRM PureText offers a Texting/SMS gateway to which you can make an HTTP GET/POST request with details such as recipient number and body of the text and Free CRM PureText will deliver the text to the recipient for you.

In order to identify that this HTTP GET/POST request belongs to you, you'll need to include your API token in the HTTP GET/POST request. Register for a free API token here

A thing to note is that you cannot just use any phone number as the 'From' number. The rule of thumb is the number you want to use as a 'From' number must be registered through the same company who's Texting/SMS gateway you choose to use. 
To send a text message using Node.js you don't need a dedicated short code. Dedicated short codes are expensive and take 2-3 months to get approved, instead, you can now use local 10 digit VOIP numbers or also referred to as long codes to send a text/SMS programmatically from your Node.js app.

So let's get started, to do a quick POC you'll need:

  1. An API token key  (free API token here)
  2. A 'From' number through your SMS gateway (Get a free one here)

_ Copy Paste the following code in your IDE and replace the API Token and From Number values from the ones in your dashboard_

/*
for demo and quick start place this code in your app.js or server.js
please run 'npm install puretext --save' in terminal
*/

  const puretext = require('puretext');
  require('request');

  let text = {
      // To Number is the number you will be sending the text to.
      toNumber: '+X-XXX-XXX-XXXX',
      // From number is the number you will buy from your admin dashboard
      fromNumber: '+14157992515',
      // Text Content
      smsBody: 'Sending SMS using Node.js',
      //Sign up for an account to get an API Token
      apiToken: 'testaccount'
  };

  puretext.send(text, function (err, response) {
    if(err) console.log(err);
    else console.log(response)
  })
Discover and read more posts from Nick
get started
post comments2Replies
Yash
3 years ago

I think.the free API link.provided by you isn’t free anymore and is charging a minimun of $10

Oleg Makarov
6 years ago

Awesome article! Also, in case you need to make some tests, especially when sending your messages internationally, you can use TelQ’s SMS testing tool: https://telqtele.com/sms-testing-platform/