Often we are required to send automated emails and SMS to notify the user about something. Common examples would be placing an order on an e-commerce website, online bookings, banking transactions, etc. Twilio provides a pretty user-friendly API to achieve this functionality. Let’s implement it using Node Expressjs. In this article, we will set up a simple front end and backend step by step.

First off visit Twilio official website and create your account. You can set up to 10 numbers from which you can receive SMS. The send to SMS number will be configured and Twilio will send an OTP to confirm the same. Once the account is the setup you’ll be able to see “accountSid” and “authToken” in your profile’s setting section.

twilio-cloud-communications-_-web-service-api-for-building-voice-and-sms-applications

Image URL: http://inheritxdev.net/knowledgebase/wp-content/uploads/2019/08/Twilio-Cloud-Communications-_-Web-Service-API-for-building-Voice-and-SMS-Applications.png

twilio-console-project-settings

Image URL: http://inheritxdev.net/knowledgebase/wp-content/uploads/2019/08/Twilio-Console-Project-Settings.png

Official website: https://www.twilio.com/docs/

In to NodeJS app we have to add npm module for twillio.

npm install twilio

then in our code we need to import module by

var twilio = require('twilio');

 code for the send SMS to mobile

var accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Your Account SID from www.twilio.com/console
var authToken = 'your_auth_token';   // Your Auth Token from www.twilio.com/console

var twilio = require('twilio');
var client = new twilio(accountSid, authToken);

client.messages.create({
    body: 'Hello from Node',
    to: '+12345678901',  // Text this number
    from: '+12345678901' // From a valid Twilio number
})
.then((message) => console.log(message.sid));

You may also like

Leave a Reply