Frequently Asked Questions
Use the Vonage Messages API and Node.js SDK (@vonage/messages) with an Express server. Create an API endpoint that accepts recipient number and image URL, then uses the SDK to send the MMS message via Vonage.
It's a unified API from Vonage that lets you send various message types, including SMS, MMS, and WhatsApp messages, all through a single interface.
Vonage uses webhooks to send delivery receipts and status updates for your outgoing MMS messages, even if you're only sending and not receiving messages in your app. These webhooks must return a 200 OK status quickly.
Use the @vonage/messages SDK when building Node.js applications that send MMS or other messages through the Vonage Messages API. Don't use the older @vonage/server-sdk for this specific API.
Vonage's MMS sending with the Messages API is primarily for US numbers sending to US recipients. International MMS support is limited, so check the Vonage documentation for specifics on supported routes.
In the Vonage Dashboard, create a new application, enable the 'Messages' capability, generate public/private keys, link your US MMS number, and configure webhook URLs for status updates and inbound messages.
The imageUrl must be a publicly accessible URL directly to the image file (like .jpg, .png, or .gif). Vonage servers need to fetch the image from this URL, so it can't be behind a login or require special headers.
Trial accounts can only send to numbers you've verified and whitelisted in your Vonage Dashboard settings. Add the recipient's number to your 'Allowed Numbers' list.
Configure a webhook URL for status updates in your Vonage Application settings and implement the /webhooks/status endpoint in your Express app to process the delivery status data sent by Vonage.
The private.key file contains the private key of your Vonage application, used for authentication and secure communication with the Vonage API. Keep this file secure and never commit it to version control.
Double-check your VONAGE_API_KEY and VONAGE_API_SECRET in your .env file. Ensure they match the credentials from your Vonage API Dashboard and that the .env file is being loaded correctly by your application.
Set the Default SMS API to 'Messages API' in your Vonage Account settings *before* sending MMS. This ensures compatibility with the @vonage/messages SDK and correct webhook formatting.
Use ngrok to create a public URL for your local server, configure the ngrok URL as your webhook URL in your Vonage Application, and then send test requests to your local endpoint using tools like curl or Postman.
It's the file path to your private.key file relative to the root of your project. Ensure it's correct, as the Vonage SDK needs to load this key for authentication.
Multimedia Messaging Service (MMS) enables sending messages that include media like images, GIFs, or videos, offering richer communication than standard SMS. This guide provides a complete walkthrough for building a Node.js application using the Express framework to send MMS messages via the Vonage Messages API.
We will build a simple Express API endpoint that accepts a recipient phone number and an image URL, then uses the Vonage Node.js SDK to send an MMS message. This guide covers everything from project setup and Vonage configuration to implementation, error handling, and testing.
Goal: Create a robust Node.js service capable of sending MMS messages reliably using Vonage.
Technology Stack:
@vonage/messages
): The official library for interacting with the Vonage Messages API in Node.js applications..env
file.System Architecture:
The system follows this flow:
Prerequisites:
1. Setting Up the Project
Let's create the project directory, initialize Node.js, and install the necessary dependencies.
Create Project Directory: Open your terminal or command prompt and create a new directory for your project, then navigate into it.
Initialize Node.js Project: Initialize the project using npm. The
-y
flag accepts default settings.This creates a
package.json
file.Install Dependencies: Install Express for the web server,
@vonage/messages
for the Vonage SDK, anddotenv
for managing environment variables.express
: Web framework for creating the API endpoint.@vonage/messages
: The correct Vonage SDK specifically for the Messages API (handles SMS, MMS, WhatsApp, etc.). Do not use the older@vonage/server-sdk
for sending MMS via the Messages API.dotenv
: Loads environment variables from a.env
file intoprocess.env
.Create Project Files: Create the main application file and a file for environment variables.
index.js
: Will contain our Express application code..env
: Will store sensitive credentials (API keys, etc.)..gitignore
: Specifies files that Git should ignore (like.env
andnode_modules
).Configure
.gitignore
: Addnode_modules
and.env
to your.gitignore
file to prevent committing them to version control.Note: We also add
private.key
here, which we'll generate later.2. Vonage Account Configuration
Before writing code, we need to configure our Vonage account and application correctly.
Get API Key and Secret:
.env
file.Purchase a US MMS-Capable Number:
Set Default SMS API to
Messages API
:Messages API
. This determines the format of webhooks and ensures compatibility with the@vonage/messages
SDK. Save the changes.Create a Vonage Application: Vonage Applications act as containers for your communication settings and handle authentication using public/private key pairs for certain APIs like Messages.
private.key
file will be downloaded. Save thisprivate.key
file securely in your project's root directory (the one containingindex.js
). Do not commit this file to Git.https://<unique-id>.ngrok.io
). Use this URL for your webhooks:https://<unique-id>.ngrok.io/webhooks/status
https://<unique-id>.ngrok.io/webhooks/inbound
200 OK
status quickly, otherwise Vonage will retry sending the webhook.Note the Application ID: After creating the application, you'll be redirected to its overview page. Copy the Application ID. You'll need this for the
.env
file.Link Your Vonage Number: On the application overview page, scroll down to the ""Linked numbers"" section. Find the US MMS-capable number you purchased earlier and click the Link button next to it. This connects incoming messages and associates outgoing messages sent via this application with that number.
3. Secure Configuration with
.env
Store your sensitive credentials and configuration details in the
.env
file. Never commit this file to version control.Populate your
.env
file with the information gathered:YOUR_...
) with your actual credentials.VONAGE_PRIVATE_KEY_PATH
: Ensure this path correctly points to your downloadedprivate.key
file../private.key
assumes it's in the same directory asindex.js
.VONAGE_MMS_SENDER_NUMBER
: Use the E.164 format (e.g.,12015550123
).4. Implementing the Core Functionality (Express API)
Now, let's write the Node.js code using Express to create an API endpoint for sending MMS.
Code Explanation:
express
,@vonage/messages
,dotenv
). Loads environment variables usingdotenv.config()
.Messages
client using credentials fromprocess.env
within atry...catch
block to handle potential initialization errors (e.g., bad key path)./send-mms
Endpoint (POST):POST /send-mms
.recipientNumber
,imageUrl
, andcaption
from the request body (req.body
). Performs basic checks to ensure required fields are present and formats are plausible (digit-based number, valid URL). Returns a400 Bad Request
if validation fails.MMSImage
object. This is the specific payload type for sending image MMS via the Messages API. It requires:to
: The recipient's phone number. E.164 format (e.g.,+12125551234
) is recommended for international compatibility, but Vonage often handles US numbers without the+
.from
: Your Vonage US MMS number from the.env
file.image
: An object containing:url
: A publicly accessible URL pointing directly to the image file (.jpg, .png, .gif supported). Vonage needs to fetch the image from this URL.caption
: Optional text to accompany the image.vonageMessages.send(mmsPayload)
within atry...catch
block. This method returns a promise that resolves with the API response or rejects with an error.send
call is successful and returns amessage_uuid
, it logs success and sends a200 OK
response back to the client including the UUID.catch
block executes.catch
block logs the detailed error.error.response.data
)./webhooks/status
and/webhooks/inbound
. These simply log the incoming webhook data and immediately return200 OK
. This is crucial because Vonage expects a quick success response for webhooks. In a real application, you would add logic here to process delivery statuses or handle incoming messages..env
(or 3000). Logs messages indicating the server is running and the endpoint is available.5. Testing the API
You can test the
/send-mms
endpoint using tools likecurl
or Postman.Start the Server: Open your terminal in the project directory and run:
You should see output indicating the server is listening on port 3000.
Test with
curl
: Open a new terminal window. Replace placeholders with a valid recipient number (remember trial account restrictions) and a publicly accessible image URL.RECIPIENT_PHONE_NUMBER
with the target phone number (e.g.,15551234567
). If using a trial Vonage account, this number must be added to your allowed list in the Vonage Dashboard (under your profile/settings).placekitten.com
URL for testing or any other direct link to a public image.Expected Output (
curl
): If successful, you should receive a JSON response like this:Check Server Logs: Look at the terminal where
node index.js
is running. You should see logs indicating the request was received and the Vonage API response.Check Recipient Phone: The recipient number should receive the MMS message with the image and caption shortly. Delivery times can vary.
Testing Errors:
recipientNumber
orimageUrl
. You should get a400 Bad Request
response..env
file with incorrect API keys and restart the server. Send a request; you should get an authentication error (likely 401).6. Troubleshooting and Caveats
imageUrl
must point directly to the image file and be accessible from the public internet without requiring logins or special headers. Vonage servers need to fetch this image. Test the URL in an incognito browser window.@vonage/messages
): Ensure you are using@vonage/messages
and initializing it correctly, not the older@vonage/server-sdk
'svonage.message.sendSms
or similar methods, which do not support MMS via the Messages API standard flow.200 OK
quickly. ngrok handles the public accessibility during development. Your placeholder routes inindex.js
handle the200 OK
response. Failure to respond correctly can lead to Vonage disabling webhooks for your application.VONAGE_PRIVATE_KEY_PATH
in your.env
file exactly matches the location of yourprivate.key
file relative to where you runnode index.js
. The SDK expects a path to the file.Messages API
: Re-verify the account-level setting mentioned in Step 2.3. Using the wrong default API (e.g., ""SMS API"") will cause incompatibility with the@vonage/messages
SDK and webhook formats.express-rate-limit
for production.7. Deployment and CI/CD (Conceptual)
.env
file. Use the hosting provider's mechanism for setting environment variables securely.Procfile
(for Heroku) or build settings correctly start the server (node index.js
).Dockerfile
to containerize the application, manage dependencies, and securely inject environment variables during container runtime.pm2
to run the Node.js application reliably in the background. Configure environment variables via system environment or.env
(with appropriate file permissions)./webhooks/status
and/webhooks/inbound
endpoints. Ensure these endpoints are secured (e.g., via signature verification if needed, although basic200 OK
is often sufficient for status/inbound if processing happens elsewhere).8. Verification and Further Steps
Verification Checklist:
.env
Messages API
in Vonage Settingsprivate.key
saved securely.env
npm init
)express
,@vonage/messages
,dotenv
).gitignore
configured correctly.env
file populated with all credentials and pathsindex.js
code implemented as shownnode index.js
)curl
or Postman to/send-mms
200 OK
response received from API withmessage_uuid
Next Steps:
/webhooks/status
to store or react to delivery receipts (e.g.,delivered
,failed
). Implement/webhooks/inbound
if you need to receive MMS/SMS replies.express-rate-limit
), and sanitize all inputs thoroughly.You now have a functional Node.js Express application capable of sending MMS messages using the Vonage Messages API. Remember to handle credentials securely and consult the Vonage Messages API documentation for more advanced features and details.