Frequently Asked Questions
Use the `/send-sms` endpoint with a POST request containing the recipient's number ("to") in E.164 format and the message text ("text"). The Plivo Node.js SDK handles sending the SMS through the Plivo API after validating the input. Ensure your Plivo credentials are set up in the .env file and a Plivo SMS-enabled number is configured as the sender.
Send a POST request to the `/send-whatsapp` endpoint. The request body must include the recipient's number ('to'), template name ('templateName'), and template language ('templateLanguage'). Optionally include 'templateParams' for dynamic content within your pre-approved WhatsApp template. The server uses the Plivo Node.js SDK to interact with the Plivo API and send the WhatsApp message.
The `/receive-sms` endpoint acts as a webhook to receive incoming SMS messages sent to your Plivo number. When someone sends an SMS to your Plivo number, Plivo sends a POST request to this endpoint with details like the sender's number, message content, and a unique message ID. You can then process this information within your application.
The Plivo Auth ID and Auth Token are your credentials for authenticating with the Plivo API. These credentials verify your identity and grant access to your Plivo account resources, ensuring that only authorized requests can send messages or access your account information. They should be kept secure and never exposed publicly.
Use WhatsApp templates for business-initiated conversations, as they require pre-approval by WhatsApp/Meta for compliance and prevent spam. Regular SMS is suitable for transactional messages, alerts, two-factor authentication, and situations where you don't need pre-approval. Templates help maintain a consistent brand experience and streamline business communications.
The Plivo Node.js SDK simplifies interaction with the Plivo API from your Node.js applications. It provides convenient methods for common tasks like sending SMS and WhatsApp messages, making voice calls, and managing phone numbers, without having to construct raw API requests. The SDK handles authentication and other low-level details, making your code cleaner.
Create a `.env` file in your project's root directory. Inside this file, add your Plivo Auth ID, Auth Token, and Plivo phone number as `PLIVO_AUTH_ID`, `PLIVO_AUTH_TOKEN`, and `PLIVO_SENDER_ID`, respectively. Use the `dotenv` package in your Node.js application to load these variables into `process.env`.
Yes, use a tool like ngrok to create a public, temporary URL that tunnels to your local server. Configure this ngrok URL as the Message URL for your Plivo number in the Plivo Console. When someone sends an SMS to your Plivo number, Plivo will forward it to your local server via the ngrok tunnel.
The provided code includes comprehensive error handling using try-catch blocks around Plivo API calls. It catches potential errors during sending, logs them server-side for debugging, and returns appropriate error responses to the client. Error responses include a generic message to avoid exposing internal details in production and a more specific hint if available from Plivo.
Express.js is the web framework used to create the Node.js server and define API routes like `/send-sms` and `/send-whatsapp`. Express handles incoming HTTP requests, routes them to the correct handlers, and provides helpful middleware for tasks like parsing JSON or URL-encoded request bodies.
Open your terminal and navigate to the project directory. Then run `npm install express plivo dotenv`. This command installs Express.js for the web server, the Plivo Node.js SDK for interacting with the Plivo API, and the dotenv package for managing environment variables.
You'll need a Plivo account, a Plivo number enabled for WhatsApp (often requiring Facebook Business Manager linking and approval), and pre-approved WhatsApp message templates. The WhatsApp setup can take time due to the approval process. Ensure your chosen number in PLIVO_SENDER_ID is WhatsApp-activated before sending.
Content Loading Error
We encountered an error while processing this content.