Frequently Asked Questions
You can send SMS messages by creating a RedwoodJS Service and a GraphQL mutation that uses the Plivo Node.js SDK. The service function will interact with the Plivo API to send messages and store message details in a database. The GraphQL mutation will provide an interface for your web application to trigger sending messages.
The Plivo callback mechanism allows your RedwoodJS application to receive real-time delivery status updates for sent SMS messages. Plivo sends these updates as HTTP POST requests (webhooks) to a designated endpoint in your RedwoodJS application. This enables you to track message status (e.g., queued, sent, delivered, failed) and update your application accordingly.
Plivo requires signature validation to ensure the security and authenticity of the callback requests. This verification step confirms that the webhook originated from Plivo and prevents malicious actors from spoofing status updates. The X-Plivo-Signature-V3 header is used for this validation process.
First, install the Plivo Node.js library. Then, configure environment variables for your Plivo Auth ID, Auth Token, source phone number, and callback URL. Set up a Prisma schema to store message data. Create a RedwoodJS Service to handle sending messages and a RedwoodJS Function to handle incoming Plivo webhooks.
Use ngrok http 8911 to create a public URL pointing to your local Redwood API server. Update the PLIVO_CALLBACK_BASE_URL environment variable with your ngrok HTTPS URL and restart your RedwoodJS server. Plivo can then send webhooks to your local machine during development.
Prisma is used as the Object-Relational Mapper (ORM) to interact with your database. It allows you to define your database schema (including a Message model) and easily perform database operations (create, update, query) within your RedwoodJS services and functions to store and retrieve message information.
Update PLIVO_CALLBACK_BASE_URL every time you start ngrok for local development. When deploying to production, update it to your deployed API's public HTTPS URL. This ensures that Plivo can always reach your webhook endpoint.
While you can set up the system and test sending messages, Plivo callbacks require sending to valid phone numbers. Use a real phone number you have access to during testing to observe actual delivery statuses from Plivo's network.
The callback URL should consist of your PLIVO_CALLBACK_BASE_URL followed by the path to your RedwoodJS function, typically /plivoCallback. For example: https://your-api-url.com/plivoCallback. This assumes you named your Redwood function plivoCallback.
Signature validation is crucial for security. It verifies that incoming webhook requests genuinely originate from Plivo and prevents unauthorized or malicious actors from tampering with your message status data. Without this check, your application could be vulnerable to attacks.
You track message status by implementing a RedwoodJS function that handles the incoming Plivo webhook. This function parses the webhook payload, validates the signature, and updates the corresponding message's status in the database based on the data received from Plivo.
Common errors include invalid signatures (403 Forbidden) caused by incorrect configuration of the PLIVO_AUTH_TOKEN or requestUrl, and message not found issues, often due to timing discrepancies between the webhook arrival and the message creation in the database.
The callback function should check the "Content-Type" header. Parse the payload as JSON if "Content-Type" is "application/json"; parse using "URLSearchParams" if "Content-Type" is "application/x-www-form-urlencoded". Include robust logging for debugging unexpected scenarios.
Initialize the Plivo client in your service file using new Plivo.Client(process.env.PLIVO_AUTH_ID, process.env.PLIVO_AUTH_TOKEN). Ensure these environment variables are correctly set in your .env file and loaded into the RedwoodJS API side.
Content Loading Error
We encountered an error while processing this content.