Twilio - Sending SMS in NodeJS

Send SMS to your users using Twilio in NodeJS in just a couple of steps.

Twilio provides programmable communication tools for making and receiving phone calls, sending and receiving text messages, and performing other communication functions using its web service APIs.

Here is how you can send an SMS using Twilio in NodeJS.

  1. Create a Twilio account here https://www.twilio.com/try-twilio.

  2. Folder Overview

    • Server.js is the entry point for our app.

    • app.js configures our application and dependencies for development

    • router defines API endpoints for our application.

    • controllers contain an async function which will be called once our app hits an endpoint.

    • config.env contains API KEYS for Twilio Configurations.

  1. Create a NodeJS server using express

  1. Create app.js file and make app configurations

    • Includes setting up the routing for our send SMS endpoint

    • Adding Morgan for logging and setting up our middleware.

  1. Configure routing

    • Creating a POST route that will be responsible for sending an SMS to a user.

  1. Creating our Twilio SMS function to send SMS

    • Install Resend npm package - npm i twilio

    • Create a Twilio client using your generated API KEY and auth_token

      • const client = require('twilio')(credentials.accountSid, credentials.authToken);

  1. Controller for sending the SMS

Conclusion

I think that Twilio is an excellent tool for sending SMS. It does have all the expected features for general app development, and it can be extended with the add-ons found in the marketplace. I have not yet used it for long-term projects since my side projects don't last long enough, but so far, I've been happy with it from the tinkerer's perspective. I would rather not spend my leisure coding time dealing with infrastructure.

Once again, thanks for reading, and I hope you found this helpful.