Tracking MessageBird SMS Delivery Status with NestJS Webhooks - code-examples -

Frequently Asked Questions

Track MessageBird SMS delivery status using webhooks. Set up a webhook endpoint in your NestJS application that receives real-time status updates from MessageBird, such as 'sent', 'delivered', or 'failed'. This allows you to monitor message delivery beyond just the initial send confirmation.
The `reportUrl` parameter in the MessageBird API tells MessageBird where to send delivery status updates for a specific message. It should point to your webhook endpoint, which is typically structured as `your-base-url/status-endpoint`. This directs the updates to the correct location in your application.
A UUID (Universally Unique Identifier) is crucial for correlating status updates back to the original message. It acts as a unique reference ID, allowing you to link incoming webhook data with the corresponding message you sent, ensuring accurate tracking even with asynchronous delivery.
Secure your webhook endpoint by using HTTPS and considering additional measures like IP whitelisting (restricting access to MessageBird's IP addresses) or a shared secret embedded in the `reportUrl` and verified upon webhook receipt. Always sanitize incoming webhook data.
The originator is the sender ID that recipients see when they receive your SMS message. It can be a phone number or an alphanumeric string (depending on regulations and MessageBird configuration), and is set using the `originator` parameter when sending messages.
ngrok is useful during development to expose your local server to the internet so MessageBird can reach your webhook endpoint. For production, use your public server's URL, as ngrok is not suitable for long-term production use cases.
Store your MessageBird API Key securely as an environment variable (e.g., `MESSAGEBIRD_API_KEY`). Use `@nestjs/config` to access and use this key in your NestJS application, ensuring you do not expose sensitive information directly in your code.
Use the MessageBird Node.js SDK along with NestJS to send SMS messages. Create a service that interacts with the SDK and a controller with a `/send` endpoint to handle requests. Ensure to include the `reportUrl` for status updates and a `reference` (UUID) for tracking.
The 'accepted' status in MessageBird means that your SMS message has been accepted by MessageBird's platform for processing. It does not guarantee delivery to the recipient but indicates that MessageBird has received and will attempt to send the message. Further status updates will follow.
If webhooks aren't firing, check your ngrok setup for HTTPS URLs, ensure your `CALLBACK_BASE_URL` and `reportUrl` are correct, verify your application and endpoint are accessible (firewalls, etc.), and confirm your `/status` endpoint is defined as a POST method and returns a 200 OK quickly.
Ensure you are correctly passing the unique `reference` (UUID) when sending the SMS via the MessageBird API. This reference is essential for matching incoming webhook data to the correct outgoing message in your application.
MessageBird expects a swift 200 OK from your webhook endpoint to confirm receipt of the status update. If your endpoint takes too long to respond, MessageBird might retry, potentially leading to duplicate processing. Offload any time-consuming operations to a background queue.
A simple schema with a table to track messages and their latest status is often sufficient. Include fields for a unique ID, the MessageBird reference, recipient, body, status, timestamps, and optionally the raw webhook payload for debugging.
Make your status update processing idempotent, meaning it's safe to run multiple times with the same input without causing unintended side effects. Check the current status in the database before updating or use database constraints to prevent duplicates.