DEV Community

loading...
Cover image for How to send a sms in Node.js

How to send a sms in Node.js

siddharth151199 profile image siddharth ・1 min read

Hey Everyone! So we are going to take a quickstart to send a sms using node.js and twilio.This tutorial is easy because using twilio api we can send sms.Let's start.

Step1:

First create free account on twilio.After that you will be redirect on https://www.twilio.com/console. On dashboard screen you will get a number which is use to send sms.There is two most important key available named as ACCOUNT SID and AUTH TOKEN.
This keys we used in code.

Step2:

Now create new folder named as what you want and open terminal in that directory and write a command.

npm init

Hit the enter to given questions.Now install the packages that we need.

npm install twilio

Now create a new file named index.js and write lines of code.

const accountSid = 'your-account-sid-twilio';
const authToken = 'your-authToken-twilio';
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'Hello from node js',
     from: 'number-twilio',
     to: 'verified-number-twilio'
   })
  .then(message => console.log(message.sid))
  .catch(err => console.log(err));

Enter fullscreen mode Exit fullscreen mode

NOTE: accountsid,authtoken and number-twilio are available on recently created twilio account.SMS will send to only verified number on twilio.if you want to add new number then you can add on dashboard right side

Now run the file using command

node index.js

AND DONE
I hope you liked the article!
Thank You!

Discussion (0)

pic
Editor guide