Frequently Asked Questions
Use the Vonage Messages API with the @vonage/server-sdk
in your Node.js application. Initialize the Vonage client with your API key, secret, application ID, and private key. Then, use vonage.messages.send()
with the recipient's number, your Vonage virtual number, and the message text to send SMS messages.
The Vonage Messages API is a multi-channel communication platform that allows you to send and receive SMS, MMS, WhatsApp messages, and more. This guide demonstrates its use for sending SMS messages and receiving delivery status updates.
Webhooks provide a real-time, asynchronous mechanism for Vonage to push delivery status updates to your application. Instead of constantly polling the API, your application receives immediate notifications when the message status changes, like being delivered or failing.
Use a Vonage Application ID and its associated private key when using the Messages API. This ensures secure authentication using JWT (JSON Web Tokens), which are important when handling webhooks for security reasons.
Alphanumeric Sender IDs might be allowed, but you must check country-specific regulations. In some regions (e.g., the US), you must use a Vonage virtual number. If allowed, ensure it's pre-registered with Vonage.
Configure a 'Status URL' in your Vonage Application settings. This URL should point to a publicly accessible endpoint in your application (e.g., using ngrok for development). Vonage will send an HTTP POST request to this URL with delivery status updates.
ngrok creates a temporary public URL that tunnels to your local development server. This allows Vonage to send webhooks to your locally running application during development, essential for receiving delivery status updates.
Ensure your webhook endpoint returns a 2xx HTTP status code (ideally 200 OK or 204 No Content) as quickly as possible. This tells Vonage that you've received the webhook. Your endpoint logic should be idempotent to handle potential duplicate webhook deliveries.
Common statuses include 'submitted', 'delivered', 'failed', 'accepted', 'buffered', 'expired', and 'unknown'. 'Buffered' often means the recipient's device is offline. Each status describes a stage in the delivery process.
Verify that ngrok is running and correctly forwarding to your server's port, the Status URL in your Vonage Application settings is accurate (including the '/webhooks/status' path), and your server application is running without errors.
The @vonage/server-sdk
simplifies interaction with Vonage APIs in Node.js. It handles authentication, request construction, and response parsing for sending SMS messages and other Vonage services.
Sensitive data, such as API keys, secrets, and private keys, should never be hardcoded. Environment variables (like in a .env
file) offer a secure way to manage these credentials locally, keeping them out of your codebase.
A suggested schema includes columns for message_uuid
, to_number
, from_number
, message_body
, submission and status update timestamps, current status, error codes, and usage details.
Respond to the webhook request with a 200 OK immediately. Perform database updates or other time-consuming tasks asynchronously in the background to prevent blocking the webhook response and causing Vonage retries.
Use HTTPS, potentially IP whitelisting or secrets in URLs. Currently, simple signature validation is not supported for messages API webhooks. Cryptographically signed webhooks are more complex to implement for these particular APIs.
MessageBird SMS Delivery Status and Callbacks with Next.js and Supabase
Implement real-time SMS delivery tracking and webhook callbacks using MessageBird, Next.js API routes, and Supabase PostgreSQL for comprehensive message status monitoring.
This guide covers:
Technologies Used
messagebird
(official Node.js SDK)@supabase/supabase-js
(Supabase client)dotenv
(environment variables)How SMS Delivery Tracking Works
reportUrl
andreference
parametersid
reportUrl
Prerequisites
1. Setting Up Next.js Project with MessageBird
1.1. Create Next.js Application
Configuration:
src/
directory: No1.2. Install Dependencies
Packages:
messagebird
– Official MessageBird SDK (npm)@supabase/supabase-js
– Supabase clientdotenv
– Environment variables1.3. Configure Environment Variables
Create
.env.local
in project root:Security:
.env.local
is automatically ignored in.gitignore
. Never commit API keys.1.4. Set Up Supabase Database for SMS Tracking
.env.local
2. Creating Supabase Client
Create
lib/supabase.js
:3. Creating MessageBird Client Wrapper
Create
lib/messagebird.js
:4. Sending SMS Messages with Delivery Status Reports
Create
lib/sendSMS.js
:5. Creating MessageBird Webhook Handler for Delivery Status Updates
Create
pages/api/webhooks/status.js
:6. Creating Next.js API Endpoint to Send SMS
Create
pages/api/send-sms.js
:7. Exposing Local Server with ngrok for Webhook Testing
For development, use ngrok to expose your local Next.js server:
Copy the HTTPS forwarding URL (e.g.,
https://abc123.ngrok.io
) and update.env.local
:Restart Next.js server after updating environment variables.
8. Testing the Implementation
8.1. Send Test SMS via API
8.2. Verify Database Storage
Check Supabase dashboard > Table Editor > messages for the new record.
8.3. Monitor Webhook Delivery
Watch your Next.js console for incoming webhooks as the status changes (sent → delivered).
9. MessageBird Error Handling and Delivery Status Codes
9.1. SMS Delivery Status Values
scheduled
sent
buffered
delivered
expired
delivery_failed
9.2. Common Error Codes
Full error code list: https://developers.messagebird.com/api/sms-messaging#sms-error-codes
9.3. Webhook Retry Logic
MessageBird retries webhook delivery up to 10 times without a 200 OK response. Implement idempotent handlers:
10. Security Best Practices for SMS Webhooks
10.1. Protecting API Keys and Environment Variables
.env.local
to version control10.2. MessageBird Webhook Security and Validation
MessageBird sends webhooks via HTTP GET with query parameters. Implement these protections:
10.3. Supabase Row Level Security
Enable RLS policies to restrict database access:
11. Deploying Next.js SMS Application to Production
11.1. Deploy to Vercel
Update
.env.local
variables in Vercel:MESSAGEBIRD_ACCESS_KEY
NEXT_PUBLIC_SUPABASE_URL
NEXT_PUBLIC_SUPABASE_ANON_KEY
WEBHOOK_BASE_URL
(set to production domain)11.2. Alternative Deployment Platforms
12. SMS Delivery Monitoring and Analytics
12.1. Query SMS Delivery Statistics
12.2. Error Tracking
Integrate error monitoring:
13. Troubleshooting MessageBird SMS Delivery Issues
Common SMS Webhook and Delivery Problems
Webhook not received
WEBHOOK_BASE_URL
is a public HTTPS URLDatabase insert fails
SMS not sending
MESSAGEBIRD_ACCESS_KEY
Authentication errors
Conclusion
You've successfully built a production-ready SMS delivery tracking system using MessageBird webhooks, Next.js API routes, and Supabase database for real-time message status monitoring.
Key takeaways:
For production, add:
Learn more about implementing SMS API integrations and webhook handling best practices to enhance your messaging infrastructure.
Additional Resources