TUTORIAL

Send SMS with Node.js - 3 Simple Steps to Get Started with 46elks

If you know how to use Node.js you can send text messages with 46elks. Just follow these three simple steps to get started and in a few minutes you will receive an SMS that you have sent from Node.js.

🇸🇪 Är du i Sverige så rekommenderar vi att du läser den svenska versionen av denna guide.

1. Create a user

To send an SMS with 46elks you first have to create an account. Go to the registration page and fill in your details. You will then receive an automatic phone call to verify your new account.

To start using 46elks to send a text messages you will need to purchase some credits. The minimum purchase is 10 EUR, which is more than enough to get started.

2. Username and password

Once you have an account it’s time to login to the 46elks dashboard. This is where you can see all your important account information - like the username and password you need to send an SMS.

Tip: Click on the blurred text under API password to see your own password.

Every time you do something with 46elks, like sending an SMS, you have to provide your username and password via Basic HTTP Authentication. When you are using Node.js you need to set Basic Authentication username and password in the Authorization HTTP Header. Most of this is handled by Node.js, see the code example below.

3. Send an SMS

Finally, it’s time to write some javascript code. Your code needs to be able to do the following:

There are many ways to implement this with Node.js. Here are two examples - one without any external libraries and one with the popular request package (install with the command npm install request):

Node.js Node.js with Request lib

var https = require('https');
var querystring = require('querystring');
 
var API_USERNAME = 'YOUR USERNAME'; // Specify your API username
var API_PASSWORD = 'YOUR PASSWORD'; // Specify your API password

var post_data = querystring.stringify({
  from : 'Node', // The sender of the SMS, up to 11 characters
  to : 'YOUR PHONE NUMBER', // The number that will receive the text message
  message : 'It is easy to send SMS with 46elks', // The content of the text message
})

var post_options = {
  host: 'api.46elks.com',
  path: '/a1/sms',
  method: 'POST',
  auth: API_USERNAME + ':' + API_PASSWORD,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(post_data)
  }
}

var post_req = https.request(post_options, function(res) {
  var data = ''
  res.setEncoding('utf8')
  res.on('data', function (chunk) { data += chunk })
  res.on('end', function () {
    if (res.statusCode == 200) {
      console.log("Sent! The API responded:")
      console.log(JSON.parse(data))
    } else {
      console.log("Error:")
      console.log(data)
    }
  })
})

post_req.write(post_data)
post_req.end()    
var request = require('request');
 
request.post('https://api.46elks.com/a1/sms', {
  auth: {
    username: 'YOUR USERNAME', // Specify your API username
    password: 'YOUR PASSWORD' // Specify your API password
  },
  form: {
    from : 'Node', // The sender of the SMS, up to 11 characters
    to : 'YOUR PHONE NUMBER', // The number that will receive the text message
    message : 'It is easy to send SMS with 46elks', // The content of the text message
  }
}, function(err, res, body) {
  if (res.statusCode == 200) {
    console.log("Sent! The API responded:")
    console.log(JSON.parse(body))
  } else {
    console.log("Error:")
    console.log(body)
  }
})   

Tip: The phone number to send to should be formatted with country code and without spaces, dashes or parentheses. For example +468123123 and not 08-123123 or +46 (8) 123 123.

Copy one of the examples above to a file namned send.js. Then run the example with the command node send.js and your phone will buzz. Congratulations, you’ve just sent an SMS from Node.js.

Next step

You’ve just sent a text message with a text sender (i.e. not from a phone number). By doing this you can use SMS to remind customers of reservations, send confirmations of new orders, etc.

To send an SMS is the easiest way to get started with 46elks, but you can do so much more. For example you can receive SMS, automatically respond to an incoming SMS or handle voice calls. For this and more there are a load of Node.js code examples at GitHub.

If you have any questions then get in touch, we love talking to our users!