Send SMS with Node.js, Express, and Plivo: A Developer Guide - code-examples -

Frequently Asked Questions

This guide provides a step-by-step process for sending SMS messages from a Node.js application using the Express framework and Plivo API. It involves setting up a project, installing dependencies like Express and Plivo, configuring environment variables, and implementing the send SMS logic within an Express route. You'll create an endpoint that accepts the destination number and message text to trigger SMS sending via Plivo.
Plivo is a cloud communications platform that provides APIs for various communication services including sending and receiving SMS messages. In this tutorial, Plivo's SMS API is utilized to send text messages from your Node.js application. You'll need a Plivo account, Auth ID, Auth Token, and a Plivo phone number to use the API.
Dotenv helps manage environment variables securely, loading credentials from a .env file into process.env. This prevents your sensitive API keys from being exposed in your codebase or committed to version control. It is crucial for keeping your Plivo Auth ID and Auth Token secret.
Environment variables should be validated at the start of your application, before using them. In the server.js file, the code checks for PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, and PLIVO_SENDER_NUMBER right after loading the .env file. If these are not present, the server exits with an error.
Your Plivo sender number is configured using an environment variable. Set a PLIVO_SENDER_NUMBER variable in your .env file with your Plivo phone number (in E.164 format). This number must be an SMS-enabled number purchased through your Plivo account.
The /send-sms endpoint expects a POST request with a JSON body containing two fields: 'to' and 'text'. The 'to' field should contain the recipient's phone number in E.164 format (e.g., +14155551234). The 'text' field should contain the SMS message content as a string.
The example code includes comprehensive error handling. It validates input parameters and catches errors during the Plivo API call. If an error occurs, it logs the error details and returns an appropriate error response to the client, including the HTTP status code and error message from Plivo.
Alphanumeric sender IDs are supported by Plivo but primarily for sending outside the US and Canada. Using one depends on local regulations for the recipient's country, and might involve registration with Plivo. For US/Canada SMS, you need a Plivo phone number as the sender ID.
After starting your Node server, use a tool like curl or Postman to send POST requests to http://localhost:3000/send-sms. Replace placeholders in the example request with the recipient's phone number and your desired message. Check your server logs and the recipient's phone to confirm message delivery.
With a Plivo trial account, you can only send messages to phone numbers verified as sandbox numbers in your Plivo console under Phone Numbers -> Sandbox Numbers. This is important for testing and verifying your integration.
Plivo supports GSM and Unicode. GSM allows 160 characters, but Unicode messages (including emojis) have a lower limit of 70 characters per segment. Plivo automatically concatenates longer messages into multiple segments.
When deploying to production environments like Heroku or AWS, do not commit your .env file. Instead, utilize the platform's specific mechanisms to set your PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, and PLIVO_SENDER_NUMBER directly within the environment.
A process manager like PM2 helps ensure your Node.js application runs continuously and restarts automatically in case of failures. This is essential for the reliability of your SMS sending service in production.