Developer Quick-Start: Build WhatsApp messaging into your Next.js app with Vonage Messages API. Step-by-step guide covering webhooks, API routes, authentication, ngrok setup, and deployment. Includes TypeScript examples and security best practices. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
Quick Answer: How to Integrate Vonage WhatsApp with Next.js
Integrate Vonage WhatsApp messaging into your Next.js application by creating API routes in
pages/api/vonage/ to handle inbound messages and status webhooks. Install
@vonage/server-sdk, @vonage/messages, and @vonage/jwt packages.
Configure environment variables for your Vonage API credentials, application ID, and private key. Use ngrok to
expose your local development server for webhook testing. The Vonage Messages API (Application Programming
Interface) sends POST requests to your /inbound route when users message your WhatsApp number,
and your app sends replies using the Vonage Node SDK (Software Development Kit). Verify webhook signatures
using JWT (JSON Web Token) authentication for security.
This guide provides a step-by-step walkthrough for integrating Vonage's Messages API (Application Programming Interface) to send and receive WhatsApp messages within a Next.js application. You'll build a simple Next.js app with API routes that handle incoming WhatsApp messages via Vonage webhooks and send replies back using the Vonage Node SDK (Software Development Kit).
This setup enables you to build applications that interact directly with users on WhatsApp, opening possibilities for customer support bots, notification systems, interactive services, and more, leveraging the familiar Next.js development environment.
Project Goals:
- Set up a Next.js project configured for Vonage WhatsApp integration.
- Create API routes to handle incoming messages and delivery status updates from Vonage.
- Use the Vonage Node SDK to send WhatsApp messages in response to user input.
- Securely manage Vonage API credentials and webhook signatures.
- Deploy the application and configure production webhooks.
Technology Stack:
- Next.js: React framework for building the frontend and API routes (using Pages Router structure for examples).
- Node.js: Runtime environment for Next.js API routes and the Vonage SDK.
- Vonage Messages API: Service used to send and receive WhatsApp messages.
- Vonage Node SDK: Simplifies interaction with the Vonage APIs.
- WhatsApp Sandbox: Vonage's testing environment for WhatsApp messaging.
- ngrok: Tool to expose local development server to the internet for webhook testing.
System Architecture:
+-------------+ +----------+ +---------------+ +-------------------------+ +-----------------+ +----------+ +-------------+
| User | --> | WhatsApp | --> | Vonage | --> | ngrok (Dev) / | --> | Next.js API Route | --> | Vonage | --> | WhatsApp | --> | User |
| (WhatsApp) | | Network | | Messages API | | Vercel URL (Prod) | | (/api/vonage/inbound) | | Messages API| | Network | | (WhatsApp) |
+-------------+ +----------+ +---------------+ +-------------------------+ +-----------------+ +----------+ +-------------+
^ | ^
|---------------------------------------------------------------|-----------------------------------|
Reply Message Sent
+---------------+ +-------------------------+ +-----------------------+
| Vonage | --> | ngrok (Dev) / | --> | Next.js API Route |
| Messages API | | Vercel URL (Prod) | | (/api/vonage/status) | (Logs status)
+---------------+ +-------------------------+ +-----------------------+
(Status Update)
Prerequisites:
- Node.js: Version 20 or higher installed.
- npm or yarn: Package manager for Node.js.
- Vonage API Account: Sign up for free credit. You'll need your API Key and Secret.
- ngrok: Installed and authenticated for testing webhooks locally.
- WhatsApp Account: A personal WhatsApp account on a smartphone for testing.
1. Setting Up the Next.js Project
Start by creating a new Next.js application and installing the necessary dependencies.
- Create a Next.js App: Open your terminal and run:
npx create-next-app@latest vonage-whatsapp-nextjs
cd vonage-whatsapp-nextjs
Follow the prompts. This guide uses the pages/api directory structure for API routes.
- Install Dependencies:
Install the Vonage SDKs and dotenv for managing environment variables locally.
npm install @vonage/server-sdk @vonage/messages @vonage/jwt dotenv
- @vonage/server-sdk: Core SDK for authentication and client initialization.
- @vonage/messages: Specifically for using the Messages API.
- @vonage/jwt: For verifying webhook signatures (JWTs β JSON Web Tokens).
- dotenv: To load environment variables from a .env.local file during local development (Next.js has built-in support, but this ensures consistency).
- Configure Environment Variables:
Create a file named .env.local in the root of your project. This file is gitignored by default
in Next.js projects, keeping your secrets safe. Add the following variables: .env
# .env.local
# Vonage API Credentials (Found on Vonage Dashboard homepage)
VONAGE_API_KEY=YOUR_VONAGE_API_KEY
VONAGE_API_SECRET=YOUR_VONAGE_API_SECRET
# Vonage Application Credentials (Generated in Application setup)
VONAGE_APPLICATION_ID=YOUR_VONAGE_APPLICATION_ID
# Use the *relative path* from your project root to your downloaded private key file
VONAGE_PRIVATE_KEY=./private.key
# Vonage WhatsApp Sandbox Number (Found on Messages API Sandbox page)
# Use the format without leading + or 00, e.g., 14157386102
VONAGE_WHATSAPP_NUMBER=VONAGE_SANDBOX_WHATSAPP_NUMBER
# Vonage Signature Secret (Found in Vonage Dashboard -> Settings)
VONAGE_API_SIGNATURE_SECRET=YOUR_VONAGE_SIGNATURE_SECRET
# Base URL for Webhooks (Set dynamically or for local dev/manual setup)
# Example for ngrok: https://<your-id>.ngrok.app
# Example for Vercel: https://your-app-name.vercel.app
BASE_URL=http://localhost:3000
How to Obtain Each Value:
- VONAGE\_API\_KEY & VONAGE\_API\_SECRET: Go to your Vonage API Dashboard. They are displayed prominently at the top.
- VONAGE\_APPLICATION\_ID & VONAGE\_PRIVATE\_KEY:
- Navigate to Applications under Build & Manage in the Vonage Dashboard.
- Click Create a new application.
- Give it a name (e.g., "NextJS WhatsApp App").
- Under Capabilities, toggle Messages. You'll need to provide temporary webhook URLs (you'll update these later). Enter http://localhost/inbound for Inbound and http://localhost/status for Status for now.
- Click Generate public and private key. Crucially, download the
private.keyfile and save it in the root of your Next.js project. The public key is managed by Vonage. - Click Create application.
- The VONAGE\_APPLICATION\_ID will be displayed on the application's page. Copy it.
- For VONAGE\_PRIVATE\_KEY in .env.local, use the relative path from your project root to the downloaded key, e.g., ./private.key.
- VONAGE\_WHATSAPP\_NUMBER:
- Navigate to Messages API Sandbox under Build & Manage.
- Activate the WhatsApp Sandbox if you haven't already.
- The Sandbox phone number will be displayed (e.g., 14157386102). Use this number.
- Follow the instructions to allowlist your personal WhatsApp number by sending the specified message to the Sandbox number.
- VONAGE\_API\_SIGNATURE\_SECRET:
- Navigate to Settings in the Vonage Dashboard.
- Under API settings, find your Default Signature secret. Copy this value.
- BASE\_URL: Set this to your local development URL (http://localhost:3000) initially. You'll use ngrok to expose this publicly later. For deployment, this will be your production URL. This variable isn't used directly by the code but helps conceptualize the webhook URLs.
- Project Structure: Place your API logic within the
pages/api/directory. Create a subdirectory for Vonage webhooks:
mkdir -p pages/api/vonage
Also create a utility file for the Vonage client:
mkdir lib
touch lib/vonageClient.js
2. Implementing Core Functionality (API Routes)
Now, let's create the API routes to handle incoming messages and status updates from Vonage.
- Initialize Vonage Client: Create a reusable Vonage client instance.
import { Vonage } from "@vonage/server-sdk";
import path from "path";
const privateKeyPath = path.resolve(process.cwd(), process.env.VONAGE_PRIVATE_KEY);
let vonage;
try {
vonage = new Vonage(
{
apiKey: process.env.VONAGE_API_KEY,
apiSecret: process.env.VONAGE_API_SECRET,
applicationId: process.env.VONAGE_APPLICATION_ID,
privateKey: privateKeyPath,
},
{
apiHost: "https://messages-sandbox.nexmo.com",
}
);
console.log("Vonage client initialized successfully.");
} catch (error) {
console.error("Error initializing Vonage client:", error);
vonage = null;
}
export default vonage;
- We use path.resolve to ensure the path to the private key is correct, regardless of where the script is run from.
- We explicitly set apiHost to the sandbox URL. Remember to remove this or change it for production.
- Basic error handling is added for initialization.
- Create Inbound Message Handler: This API route will receive messages sent by users to your Vonage WhatsApp number.
import vonage from "../../../lib/vonageClient";
import { WhatsAppText } from "@vonage/messages";
const processedMessages = new Set();
const MAX_SET_SIZE = 1000;
async function sendWhatsAppReply(recipientNumber, messageText) {
if (!vonage) {
console.error("Vonage client not initialized. Cannot send message.");
throw new Error("Vonage client unavailable");
}
console.log(`Attempting to send reply to ${recipientNumber}`);
try {
const message = new WhatsAppText({
to: recipientNumber,
from: process.env.VONAGE_WHATSAPP_NUMBER,
text: messageText,
client_ref: `reply-${Date.now()}`,
});
const response = await vonage.messages.send(message);
console.log(`Message sent successfully to ${recipientNumber}:`, response);
return response;
} catch (error) {
console.error(`Error sending message to ${recipientNumber}:`, error.response ? error.response.data : error.message);
throw error;
}
}
export default async function handler(req, res) {
if (!vonage) {
console.error("Vonage client not available in handler.");
return res.status(500).json({ error: "Server configuration error" });
}
if (req.method !== "POST") {
console.log(`Received ${req.method} request, expected POST`);
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
console.log("Received inbound webhook:", JSON.stringify(req.body, null, 2));
try {
const { message_uuid, from, message } = req.body;
if (!message_uuid || !from || !from.number || !message || message.type !== "text") {
console.warn("Received incomplete, non-text, or unexpected message format:", req.body);
return res.status(200).end();
}
const senderNumber = from.number;
const incomingText = message.content.text;
if (processedMessages.has(message_uuid) || incomingText === "Message received.") {
console.log(`Skipping already processed or self-reply message: ${message_uuid}`);
return res.status(200).end();
}
if (processedMessages.size >= MAX_SET_SIZE) {
const oldestKey = processedMessages.values().next().value;
processedMessages.delete(oldestKey);
}
processedMessages.add(message_uuid);
console.log(`Received text message "${incomingText}" from ${senderNumber} (UUID: ${message_uuid})`);
const replyText = "Message received.";
await sendWhatsAppReply(senderNumber, replyText);
res.status(200).end();
} catch (error) {
console.error("Error processing inbound message:", error);
res.status(500).json({ error: "Failed to process message" });
}
}
- It imports the shared vonage client.
- It checks for POST requests.
- It extracts the sender's number (from.number) and the message content (message.content.text).
- Includes basic validation and explains why 200 OK is returned for ignored messages.
- It calls a helper function sendWhatsAppReply to send a "Message received." response.
- Crucially, it includes basic idempotency logic (processedMessages Set) with comments on its limitations and clearing mechanism (server restart).
- It responds with 200 OK to Vonage to acknowledge receipt. Errors return 500.
- Create Status Handler: This route receives status updates about messages you've sent (e.g., delivered, read, failed).
import { verifySignature } from "@vonage/jwt";
function verifyVonageSignature(req) {
try {
const authorizationHeader = req.headers.authorization;
if (!authorizationHeader || !authorizationHeader.startsWith("Bearer ")) {
console.error("Missing or invalid Authorization header");
return false;
}
const token = authorizationHeader.split(" ")[1];
const isValid = verifySignature(token, process.env.VONAGE_API_SIGNATURE_SECRET);
if (!isValid) {
console.warn("Invalid JWT signature received.");
}
return isValid;
} catch (error) {
console.error("Error during JWT verification:", error);
return false;
}
}
export default async function handler(req, res) {
if (req.method !== "POST") {
console.log(`Received ${req.method} request, expected POST`);
res.setHeader("Allow", ["POST"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
if (!verifyVonageSignature(req)) {
console.error("Unauthorized status webhook attempt blocked.");
return res.status(401).json({ error: "Invalid signature" });
}
console.log("JWT signature verified successfully for status webhook.");
try {
console.log("Received status webhook:", JSON.stringify(req.body, null, 2));
const { message_uuid, status, timestamp, to, error } = req.body;
if (!message_uuid || !status || !timestamp || !to || !to.number) {
console.warn("Received incomplete status update:", req.body);
return res.status(200).end();
}
console.log(`Status Update: UUID=${message_uuid}, Status=${status}, To=${to?.number}, Timestamp=${timestamp}`);
if (status === "delivered") {
console.log(`Message ${message_uuid} delivered successfully to ${to?.number}.`);
} else if (status === "read") {
console.log(`Message ${message_uuid} read by ${to?.number}.`);
} else if (status === "failed" || status === "rejected") {
console.error(`Message ${message_uuid} failed for ${to?.number}. Reason:`, error);
} else {
console.log(`Received status "${status}" for message ${message_uuid}.`);
}
res.status(200).end();
} catch (error) {
console.error("Error processing status update:", error);
res.status(500).json({ error: "Failed to process status update" });
}
}
- It includes a verifyVonageSignature helper using @vonage/jwt and your VONAGE\_API\_SIGNATURE\_SECRET. This step is vital for security.
- It logs the incoming status update. In a real application, you'd use this data to update message statuses in your database or trigger other workflows (see Section 6).
- It responds with 200 OK.
3. Configuring Webhooks with ngrok
To test the integration locally, Vonage needs to be able to reach your development server. We'll use ngrok for this.
- Start Your Next.js App:
npm run dev
Your app should be running, typically on http://localhost:3000.
- Start ngrok: Open a new terminal window and run ngrok, pointing it to your Next.js port (usually 3000):
ngrok http 3000
- Get Your Public URL: ngrok will display output similar to this:
Forwarding https://<some-random-id>.ngrok-free.app -> http://localhost:3000
Copy the https://... URL. This is your temporary public URL.
- Update Webhook URLs in Vonage:
- Application Webhooks:
- Go back to your application in the Vonage Dashboard (Applications -> Your App).
- Click Edit.
- Update the Messages capability webhooks:
- Inbound URL: <your-ngrok-url>/api/vonage/inbound
- Status URL: <your-ngrok-url>/api/vonage/status
- Click Save changes.
- Messages API Sandbox Webhooks:
- Go to the Messages API Sandbox page in the Vonage Dashboard.
- Scroll down to the Webhooks section.
- Update the webhooks:
- Inbound URL: <your-ngrok-url>/api/vonage/inbound
- Status URL: <your-ngrok-url>/api/vonage/status
- Click Save webhooks.
Important: Ensure the paths /api/vonage/inbound and
/api/vonage/status match the location of your API route files within your Next.js project's
pages/api/ directory.
4. Testing the Integration
- Send a WhatsApp Message: Using the personal WhatsApp account you allowlisted earlier, send any message (e.g., ""Hello"") to the Vonage WhatsApp Sandbox number.
- Check Your Logs:
- Next.js Terminal: You should see logs from your /api/vonage/inbound route, indicating the message was received and a reply was attempted (e.g., ""Received text message..."", ""Attempting to send reply..."", ""Message sent successfully..."").
- ngrok Terminal: You should see POST requests hitting your ngrok URL for /api/vonage/inbound and likely /api/vonage/status shortly after.
- Next.js Terminal (Status): After the reply is sent, you should see logs from /api/vonage/status showing the delivery status updates (e.g., ""Received status webhook..."", ""Status Update: UUID=..., Status=submitted..."", ""Status Update: UUID=..., Status=delivered..."").
- Check WhatsApp: You should receive the ""Message received."" reply on your personal WhatsApp account from the Sandbox number.
5. Error Handling and Logging Considerations
The provided code includes basic try...catch blocks and
console.log/console.error. For production:
- Structured Logging: Use a library like Pino or Winston to output logs in JSON format. This makes them easier to parse and analyze in log management systems (e.g., Datadog, Logtail, AWS CloudWatch).
- Centralized Error Tracking: Integrate an error tracking service like Sentry or Bugsnag to capture, aggregate, and alert on unhandled exceptions in your API routes.
- Specific Error Handling: Catch specific errors from the Vonage SDK (check their documentation for error types) and handle them appropriately (e.g., retries for transient network issues, logging specific failure reasons from the error object in status updates).
- Robust Idempotency: The simple Set is only suitable for demos. Use a persistent store (like Redis or a database) checking message\_uuid to reliably handle duplicate webhook deliveries in production.
6. Database Integration (Conceptual)
While this basic example doesn't use a database, a real-world application likely would:
- Store Message Logs: Log incoming and outgoing messages with their message\_uuid, sender/recipient, content, timestamp, and status.
- Manage Conversation State: For bots, store the user's current position in a conversation flow.
- Store User Data: Link WhatsApp numbers to user profiles in your system.
Example using Prisma (Conceptual):
- Setup Prisma:
npm install prisma --save-dev,npx prisma init, configure your database URL in.env. - Define Schema:
Prisma
// prisma/schema.prisma
model MessageLog {
id String @id @default(cuid())
messageUuid String @unique // Vonage message_uuid (for outbound status tracking)
direction String // ""inbound"" or ""outbound""
sender String
recipient String
content String?
status String // e.g., received (inbound), submitted, delivered, read, failed (outbound)
timestamp DateTime @default(now())
vonageError Json? // Store error details if failed (outbound)
// Optional: Add relation to Conversation or User models
// conversationId String?
// conversation Conversation? @relation(fields: [conversationId], references: [id])
}
- Apply Schema:
npx prisma migrate dev(ornpx prisma db pushfor prototyping) - Use in API Routes:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function logInboundMessage(uuid, sender, recipient, text) {
try {
await prisma.messageLog.create({
data: {
messageUuid: uuid,
direction: 'inbound',
sender: sender,
recipient: recipient,
content: text,
status: 'received',
},
});
} catch (dbError) {
console.error('DB Error logging inbound message:', dbError);
}
}
async function logOutboundMessage(uuid, recipient, text) {
try {
await prisma.messageLog.create({
data: {
messageUuid: uuid,
direction: 'outbound',
sender: process.env.VONAGE_WHATSAPP_NUMBER,
recipient: recipient,
content: text,
status: 'submitted',
},
});
} catch (dbError) {
console.error('DB Error logging outbound message:', dbError);
}
}
async function updateMessageStatusInDb(uuid, status, errorDetails) {
try {
await prisma.messageLog.update({
where: { messageUuid: uuid },
data: {
status: status,
vonageError: errorDetails ? errorDetails : undefined,
},
});
} catch (dbError) {
if (dbError.code === 'P2025') {
console.warn(`DB Warn: Status update for unknown messageUuid: ${uuid}`);
} else {
console.error('DB Error updating message status:', dbError);
}
}
}
- This refined example shows separate logging for inbound and outbound messages.
- Crucially, it logs the message\_uuid returned by Vonage when sending the outbound message.
- The status handler then uses prisma.messageLog.update (not updateMany) with where: { messageUuid: message\_uuid } to update the status of that specific outbound message log entry.
7. Security Best Practices
- Verify Signatures: Always verify the JWT signature on the /status webhook using verifySignature and your VONAGE\_API\_SIGNATURE\_SECRET. This prevents attackers from spoofing status updates.
- Secure Inbound Webhook (Optional): While the /inbound webhook doesn't use Vonage JWTs by default, consider adding your own security layer if the handled data is sensitive. Options include:
- Basic Authentication over HTTPS.
- Checking a custom shared secret passed in a header.
- IP address allowlisting (if Vonage provides stable IPs or ranges).
- Secure Secrets: Never commit .env.local or your private.key file to source control. Use environment variables in your deployment environment (see Section 9).
- Input Validation: Sanitize and validate data received in webhooks (req.body). Check expected data types, lengths, and formats. Libraries like zod can help define schemas for robust validation.
- Rate Limiting: Implement rate limiting on your API routes (e.g., using nextjs-rate-limiter or platform features like Vercel's) to prevent abuse or accidental loops.
- HTTPS: Always use HTTPS for your webhook URLs (ngrok provides this, and platforms like Vercel enforce it).
8. Troubleshooting and Caveats
- Webhooks Not Reaching Server:
- Check ngrok status and URL. Is ngrok running? Is the URL correct?
- Verify the exact webhook URL (including /api/vonage/...) is configured correctly in both the Vonage Application and Sandbox settings. A typo is common.
- Check firewall rules if self-hosting.
- Inspect the ngrok web interface (http://127.0.0.1:4040) for request/response details and errors.
- 401 Unauthorized on
/status: Incorrect VONAGE\_API\_SIGNATURE\_SECRET or issue with JWT verification logic. Double-check the secret in Vonage Settings -> API settings and your .env.local / deployment environment variables. Ensure the header format is correct (Bearer <token>). - Error Sending Message: Check VONAGE\_API\_KEY, VONAGE\_API\_SECRET, VONAGE\_APPLICATION\_ID, and VONAGE\_PRIVATE\_KEY path/content in .env.local / environment variables. Ensure the recipient number is correctly formatted (E.164) and allowlisted in the Sandbox. Check Vonage Dashboard logs (Logs -> Messages API) for specific API errors. Ensure the vonageClient initialized correctly (check startup logs).
- Infinite Loops: Ensure your reply message content doesn't accidentally trigger your inbound logic again. Implement robust idempotency checks using message\_uuid and a persistent store for production. The demo Set is insufficient for reliable duplicate prevention.
- Sandbox Limitations: Only works with allowlisted numbers. Uses a shared Vonage number. Watermarks may appear on messages. Not for production traffic. Lower throughput than production.
- Moving to Production: Requires purchasing a Vonage number, setting up a WhatsApp Business Account (WABA), potentially getting approval for message templates (if initiating conversations outside the 24-hour window), and updating API credentials/webhook URLs. Remove the apiHost sandbox override in lib/vonageClient.js.
Frequently Asked Questions About Vonage WhatsApp Integration with Next.js
How do I set up Vonage WhatsApp integration in Next.js?
Set up Vonage WhatsApp integration by installing @vonage/server-sdk,
@vonage/messages, and @vonage/jwt packages using npm. Create a Vonage application in
the Vonage Dashboard with Messages capability enabled, download the private key file, and configure
environment variables in .env.local for your API credentials (VONAGE_API_KEY,
VONAGE_API_SECRET, VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY). Create
API routes in pages/api/vonage/inbound.js and pages/api/vonage/status.js to handle
incoming messages and delivery status updates. Initialize the Vonage client in
lib/vonageClient.js using your credentials and the Messages API sandbox URL for testing.
How do webhooks work with Vonage Messages API and Next.js?
Vonage sends POST requests to your Next.js API routes when WhatsApp events occur. The inbound webhook
(/api/vonage/inbound) receives messages sent by users to your WhatsApp number, containing
message_uuid, from.number, and message.content.text in the request
body. The status webhook (/api/vonage/status) receives delivery status updates (submitted,
delivered, read, failed) for messages you've sent. Configure these webhook URLs in your Vonage Application
settings and Messages API Sandbox. Use ngrok during local development to expose your localhost server to the
internet so Vonage can reach your webhooks.
How do I verify Vonage webhook signatures for security?
Verify Vonage webhook signatures using JWT (JSON Web Token) authentication with the @vonage/jwt
package's verifySignature function. Extract the token from the Authorization header
(format: Bearer <token>), then verify it using your
VONAGE_API_SIGNATURE_SECRET from the Vonage Dashboard Settings β API settings. Implement this
verification in your status webhook handler to ensure requests genuinely come from Vonage and prevent spoofing
attacks. Return 401 Unauthorized for invalid signatures. The inbound webhook doesn't use JWT by
default, but you can add custom security layers like Basic Authentication or IP allowlisting if needed.
What causes "Vonage client not initialized" errors?
"Vonage client not initialized" errors occur when the Vonage SDK fails to initialize in
lib/vonageClient.js. Common causes include incorrect environment variables
(VONAGE_API_KEY, VONAGE_API_SECRET, VONAGE_APPLICATION_ID), wrong
VONAGE_PRIVATE_KEY file path (use relative path like ./private.key), missing or
malformed private key file, or the private key file not being accessible at runtime. Check your console logs
for "Error initializing Vonage client" messages, verify all environment variables are set correctly in
.env.local, ensure the private.key file exists in your project root, and confirm
file permissions allow Node.js to read the private key.
How do I prevent infinite message loops with Vonage WhatsApp?
Prevent infinite loops by implementing idempotency checks using the message_uuid field from
incoming webhooks. Store processed message UUIDs in a Set (for development) or persistent store like Redis or
a database (for production). Before processing an incoming message, check if its message_uuid
already exists in your processed messages store. Also check if the incoming message text matches your bot's
reply text (e.g., "Message received.") to avoid processing your own responses. The demo code includes a basic
Set-based implementation with size limits, but production applications require persistent storage that
survives server restarts.
What's the difference between Vonage Sandbox and production WhatsApp?
The Vonage WhatsApp Sandbox is a free testing environment with limitations: only allowlisted phone numbers
(added by sending a specific message to the sandbox number) can send/receive messages, uses a shared Vonage
phone number, may display watermarks on messages, has lower throughput limits, and isn't suitable for
production traffic. Production requires purchasing a dedicated Vonage phone number, setting up a WhatsApp
Business Account (WABA) with Meta, potentially submitting message templates for approval (for conversations
initiated outside the 24-hour response window), updating your API credentials and webhook URLs, and removing
the apiHost: "https://messages-sandbox.nexmo.com" override in your Vonage client initialization.
How do I send WhatsApp messages using Vonage in Next.js?
Send WhatsApp messages by creating a WhatsAppText instance from @vonage/messages
with to (recipient number in E.164 format like
+14155551234), from (your Vonage WhatsApp number), and text (message
content) properties. Call vonage.messages.send(message) which returns a Promise containing the
message_uuid for tracking. Implement proper error handling using try-catch blocks to handle rate
limits, invalid numbers, or network errors. Store the returned message_uuid in your database to
correlate with delivery status updates received via the status webhook. Always use E.164 phone number format
(+country code + number without spaces or special characters).
What environment variables do I need for Vonage WhatsApp integration?
Required environment variables include VONAGE_API_KEY and VONAGE_API_SECRET (from
Vonage Dashboard homepage), VONAGE_APPLICATION_ID (from your Vonage Application page),
VONAGE_PRIVATE_KEY (relative path to downloaded private key file like
./private.key), VONAGE_WHATSAPP_NUMBER (your sandbox or production WhatsApp number
without + or 00), and VONAGE_API_SIGNATURE_SECRET (from Vonage Dashboard β Settings β API
settings). For deployment platforms like Vercel, create a VONAGE_PRIVATE_KEY_CONTENT variable
containing the actual private key file contents as a multi-line string, since file paths don't work in
serverless environments. Store these in .env.local for local development and in your deployment
platform's environment variables settings for production.
How do I deploy a Vonage WhatsApp Next.js app to Vercel?
Deploy to Vercel by pushing your code to Git (ensure .env.local and private.key are
in .gitignore), importing the repository in Vercel, and configuring environment variables in
Settings β Environment Variables. For the private key, create a VONAGE_PRIVATE_KEY_CONTENT
variable with the file's contents instead of using a file path. Modify lib/vonageClient.js to
check for process.env.VONAGE_PRIVATE_KEY_CONTENT first, falling back to the file path for local
development. After deployment, update your Vonage Application and Messages API webhook URLs to your Vercel
production URL (https://your-app-name.vercel.app/api/vonage/inbound and
/api/vonage/status). Remove or comment out the sandbox apiHost override for
production use. Test by sending a message to your production WhatsApp number and checking Vercel's runtime
logs.
What are common errors when integrating Vonage WhatsApp with Next.js?
Common errors include webhooks not reaching your server (check ngrok status, verify webhook URLs in both
Vonage Application and Sandbox settings, inspect ngrok web interface at http://127.0.0.1:4040),
401 Unauthorized on status webhook (incorrect VONAGE_API_SIGNATURE_SECRET or wrong header
format), message sending failures (verify API credentials, check E.164 phone number format, ensure recipient
is allowlisted in sandbox, check Vonage Dashboard β Logs β Messages API for specific errors), infinite message
loops (implement robust idempotency with persistent storage), and private key errors during deployment (use
environment variable for key contents instead of file path on serverless platforms). Always check console logs
and Vonage Dashboard logs for detailed error messages.
SMS Integration Guides:
- Vonage Node.js Next.js SMS Guide β Send SMS messages with Vonage and Next.js
- Twilio Node.js Next.js SMS Guide β Alternative SMS provider integration
- E.164 Phone Number Format β International phone number formatting standard
9. Deployment (Example: Vercel)
- Push to Git: Commit your code (ensure .env.local and private.key are in .gitignore) and push it to a Git provider (GitHub, GitLab, Bitbucket).
- Import Project in Vercel: Connect your Git repository to Vercel.
- Configure Environment Variables: In the Vercel project settings -> Settings -> Environment
Variables, add all the variables from your .env.local file (VONAGE\_API\_KEY, VONAGE\_API\_SECRET,
VONAGE\_APPLICATION\_ID, VONAGE\_API\_SIGNATURE\_SECRET, VONAGE\_WHATSAPP\_NUMBER). Do not add
VONAGE_PRIVATE_KEYas a file path. Instead:
- Read the content of your local private.key file.
- Create an environment variable in Vercel named VONAGE\_PRIVATE\_KEY\_CONTENT (or similar) and paste the multi-line key content exactly as its value.
- Modify lib/vonageClient.js to read the key content directly from this environment variable when deployed:
import { Vonage } from '@vonage/server-sdk';
import path from 'path';
const privateKeyValue = process.env.VONAGE_PRIVATE_KEY_CONTENT
? process.env.VONAGE_PRIVATE_KEY_CONTENT.replace(/\\n/g, '\n')
: path.resolve(process.cwd(), process.env.VONAGE_PRIVATE_KEY);
let vonage;
try {
vonage = new Vonage(
{
apiKey: process.env.VONAGE_API_KEY,
apiSecret: process.env.VONAGE_API_SECRET,
applicationId: process.env.VONAGE_APPLICATION_ID,
privateKey: privateKeyValue,
},
{
}
);
console.log('Vonage client initialized successfully.');
} catch (error) {
console.error('Error initializing Vonage client:', error);
vonage = null;
}
export default vonage;
- Deploy: Trigger a deployment in Vercel.
- Update Production Webhooks: Once deployed, Vercel provides a production URL (e.g., https://your-app-name.vercel.app). Update the Vonage Application and production Messages API webhooks (not the Sandbox ones, unless you intend to keep using it) to use this URL:
- Inbound URL: https://your-app-name.vercel.app/api/vonage/inbound
- Status URL: https://your-app-name.vercel.app/api/vonage/status
- Test Production: Send a message to your production Vonage WhatsApp number (if configured) and verify the flow. Check Vercel's runtime logs for any errors.
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β