Receiving MessageBird SMS Messages with Next.js and Vercel - code-examples -

Frequently Asked Questions

Use Next.js API routes to create a webhook endpoint. This endpoint will receive incoming SMS messages forwarded from your MessageBird virtual number via the MessageBird Flow Builder. This allows your Next.js application to process incoming texts and react accordingly.
MessageBird Flow Builder is used to route incoming SMS messages to your Next.js webhook. You configure the Flow Builder to trigger the webhook URL with the message data whenever an SMS is received on your designated MessageBird virtual number.
Next.js API routes provide a serverless function environment ideal for handling webhooks. They offer an easy way to create server-side logic without managing a separate backend server, simplifying development and deployment, especially on Vercel.
ngrok is essential during local development. It creates a public tunnel to your local server so MessageBird can reach your webhook endpoint while you are testing. For production, deploy to Vercel and use the Vercel app URL.
First, create a Next.js API route. Then, in your MessageBird Flow Builder, add an HTTP endpoint step. Paste your ngrok HTTPS URL (for local development) or your Vercel app URL (for production) along with the API endpoint path and your secret as a query parameter.
Use ngrok to create a public URL for your local development server. Configure MessageBird Flow Builder with this URL, then send an SMS to your MessageBird number. Observe logs in your Next.js terminal for successful webhook delivery. Also, you can simulate MessageBird POST requests with curl. Check the logs for validation success and received payloads.
This secret, stored in your .env.local file, adds security to your webhook. By including it as a query parameter in your webhook URL, you ensure only requests with the correct secret, presumably from MessageBird, are processed by your application.
The webhook sends data as application/x-www-form-urlencoded. Key fields include 'originator' (sender's number), 'payload' (the message content), 'recipient' (your virtual number), 'messageId', and 'createdDatetime'. Logging the full received data is recommended.
Use a strong, randomly generated secret as a query parameter in the URL. Verify this secret on your Next.js API route to ensure only MessageBird is triggering the webhook. Also, implement input sanitization, rate limiting, and possibly timestamp checks.
MessageBird typically handles long SMS (concatenated messages) automatically. You should receive the entire reassembled message in the 'payload' field of your webhook. Testing long messages is recommended.
Avoid using SQLite in production on serverless platforms like Vercel due to ephemeral file systems. Use a managed database service like Vercel Postgres, PlanetScale, or Supabase, especially if you need persistent data storage. For local development and testing, SQLite is convenient.
Prisma is an Object-Relational Mapper (ORM) that simplifies database interactions. It helps define your database schema, perform migrations, and access data easily within your API route handler. It's particularly useful for modeling and handling database relations for complex systems.
The guide recommends Prisma with SQLite for development and testing. For production, use a persistent, managed database solution. In your webhook handler, after validating the request, use Prisma Client to create a new database entry with the message details.
Implement thorough error handling and logging in your webhook handler. Catch potential errors, log them, and return appropriate status codes. Consider using a structured logging library like 'pino'. Critically, try to always return 200 OK to MessageBird quickly, even on errors, unless you specifically want MessageBird to retry.