How to Send SMS with Twilio Node.js SDK

January 23, 2023

There will be cases in which you might want to send SMS to your customers like a critical notification or OTP or something like that.

Twilio is a good choice for such cases and it has a wide range of integration with different apps as well it’s been used by companies like Uber for sending SMS to their end customers.

Here’s a little snippet on how to send SMS using Javascript (Node.js)

Install Twilio SDK using NPM

npm i twilio

Then, we’ll need ACCOUNT_SID and AUTH_TOKEN. You can get it from the dashboard.

And here is the snippet on sending SMS

import twilio from 'twilio';

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

const sendSms = async (to, message) => {
    return client.messages
        .create({
          from: process.env.TWILIO_PHONE_NUMBER,
          to,
          body: message,
        })
}

export default sendSms;

Happy Sending SMS!


👋 I'm Ashik Nesin • Principal Software Engineer 👨‍💻• I write about web development and share insights into the behind-the-scenes of the projects that I'm building.

© 2023 Nesin Technologies LLP