Send MMS with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage Messages API with the @vonage/server-sdk and Express. Set up a Node.js project, install dependencies, configure your Vonage account, build the /send-mms endpoint and handle requests containing the recipient number, image URL, and caption. The API interacts with Vonage to send the MMS message.
The Vonage Messages API is a unified platform for sending messages through various channels including MMS. This API allows developers to programmatically send rich media content, like images and text, within the US, particularly useful for sending notifications, alerts, and marketing messages.
MMS failures can be due to several reasons: incorrect Vonage API credentials, the recipient's number not being whitelisted (for trial accounts), an invalid or inaccessible image URL, or issues with the 'from' number not being correctly linked in the Vonage Dashboard. Check your Vonage setup in the dashboard and verify number settings.
The Vonage Messages API is ideal when your application needs to send text messages, rich media messages like MMS (within the US), or messages to other channels like WhatsApp or Viber, programmatically. This provides flexibility compared to manual sending or using separate APIs.
Vonage's Messages API primarily supports MMS within the US. International MMS or P2P MMS via virtual numbers is not reliably supported through this API. Other solutions may be required for international use cases.
In your Vonage Dashboard, create a new application, generate public and private keys (saving the private key securely), enable the "Messages" capability, set up webhook URLs for status and inbound messages (Mockbin for initial testing), copy the Application ID, and link your MMS-capable US Vonage number to this application. Then update .env with all your vonage variables.
Vonage MMS supports JPG, JPEG, and PNG image formats. The image URL provided must be publicly accessible via HTTP or HTTPS for Vonage to retrieve it. Ensure images are optimized for size to improve delivery speed and cost.
Implement `try...catch` blocks around the `vonageMessages.send()` call in your code to handle potential errors. Check the error response for specific status codes and messages to determine the cause, such as authentication issues or invalid parameters. Log errors for debugging and monitoring.
The `.env` file stores environment variables, such as your Vonage API credentials and configuration settings. This keeps sensitive data separate from your code, improving security and making it easier to manage settings across different environments.
You can use tools like `curl` or Postman to send test requests to your `/send-mms` endpoint with sample recipient numbers, image URLs, and captions. Verify that the endpoint returns a 202 Accepted status for successful submissions and that the MMS message is received by the test number.
In a production environment, a process manager like PM2 ensures your Node.js application runs continuously, restarts automatically if it crashes, and provides tools for managing multiple processes and monitoring resource usage.
Secure the `/send-mms` endpoint using authentication middleware (e.g., API keys, JWT) to prevent unauthorized access. Implement input validation to sanitize and ensure proper data formatting, and add rate limiting to protect against abuse and manage costs.
The "Non-Whitelisted Destination" error usually occurs with Vonage trial accounts. Add the recipient's phone number to your allowed test numbers in the Vonage Dashboard under "Account" > "Test numbers" to resolve this issue.
The `client_ref` parameter is optional and can be used for internal tracking on your side. You can generate a unique identifier and include it in the MMS payload to match it with data stored in your application's database, but relying solely on Vonage's `message_uuid` is usually sufficient for tracking purposes.
Handle temporary network issues or Vonage service disruptions by implementing retry mechanisms with exponential backoff on the client-side or using a library like `async-retry` on your server, specifically for 5xx errors. Avoid retrying on 4xx client errors.