Developer Quick-Start: Build a production-ready SMS API with Plivo, Node.js, and Fastify. Learn setup, authentication, error handling, rate limiting, security best practices, and deployment with complete working code examples. 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.
Build a Node.js application using the Fastify framework to send SMS messages via the Plivo API. This guide covers project setup, core implementation, error handling, security considerations, and deployment basics.
By the end of this guide, you'll have a functional Fastify API endpoint capable of accepting requests and triggering SMS messages through Plivo, along with the foundational knowledge to expand upon it.
What Will You Build with This Guide?
Goal: Create a simple, robust, and scalable API endpoint using Fastify that sends SMS messages via the Plivo communication platform.
Problem Solved: Build a backend service that abstracts the Plivo SMS sending functionality, enabling other services or frontend applications to send SMS messages easily through a standard REST API call without needing direct access to Plivo credentials or SDKs.
Technologies:
- Node.js: A JavaScript runtime environment enabling server-side execution. Required: Node.js v20+ for Fastify v5 (or Node.js v14+ for Fastify v4, supported until June 30, 2025).
- Fastify: A high-performance, low-overhead web framework for Node.js, chosen for its speed, extensibility, and developer-friendly features like built-in validation. Current versions: Fastify v5 (Node.js v20+) or Fastify v4 (Node.js v14-v22, EOL June 2025).
- Plivo: A cloud communications platform providing APIs for SMS, voice, and more. You'll use their Node.js SDK (actively maintained on npm, latest stable version available at npmjs.com/package/plivo).
- dotenv: A zero-dependency module to load environment variables from a .env file into process.env, crucial for managing sensitive credentials securely.
System Architecture:
+-------------+ +---------------------+ +--------------+ +-----------------+
| Client (e.g.,| ----> | Fastify API Server | ----> | Plivo Node.js| ----> | Plivo API |
| Postman/App)| | (Node.js) | | SDK | | (Sends SMS) |
+-------------+ +---------------------+ +--------------+ +-----------------+
| | Send Request (POST) | Uses Credentials | Delivers Message
| | Validate Request | Constructs API Call |
| | Call Plivo SDK | Handles Response |
|<--------------------| Return Response |<----------------------| Returns Status
| | (Success/Error) | (e.g., Message UUID) |
+ +---------------------------------+-----------------------+
Prerequisites:
- Node.js and npm (or yarn): Ensure you have Node.js installed. For Fastify v5, install Node.js v20 or later. For Fastify v4, Node.js v14+ works. You can download it from nodejs.org. npm is included with Node.js.
- Plivo Account: Sign up for a Plivo account at plivo.com.
- Plivo Credentials: Obtain your Auth ID and Auth Token from the Plivo console dashboard at console.plivo.com/dashboard.
- Plivo Phone Number: Acquire an SMS-enabled Plivo phone number from the Phone Numbers section of the Plivo console. This will be your sender number (src). Note specific regulations for sender IDs in different countries; a Plivo number is generally required for sending to the US and Canada.
- Verified Destination Number (Trial Account): If using a Plivo trial account, you must verify any destination phone numbers in the Plivo console under Phone Numbers > Sandbox Numbers before you can send messages to them.
Final Outcome: A running Fastify server with a single API endpoint
(POST /api/sms/send) that accepts a destination number and message text, sends the SMS via Plivo,
and returns a confirmation or error.
1. How Do You Set Up Your Project?
Initialize your Node.js project and install the necessary dependencies.
- Create Project Directory: Open your terminal and create a new directory for your project, then navigate into it.
mkdir fastify-plivo-sms
cd fastify-plivo-sms
- Initialize Node.js Project: Initialize the project using npm, creating a
package.jsonfile. The-yflag accepts default settings.
npm init -y
- Install Dependencies: Install Fastify, the Plivo Node.js SDK, and
dotenv.
npm install fastify plivo dotenv
Version Note: This installs the latest stable versions. As of January 2025, Fastify v5 requires Node.js v20+. If you're using an older Node.js version (v14-v19), install Fastify v4 specifically:
npm install fastify@4 plivo dotenv
- Set Up Project Structure: Create a basic structure for organization.
mkdir src
mkdir src/routes
mkdir src/config
touch src/server.js
touch src/routes/sms.js
touch src/config/plivo.js
touch .env
touch .gitignore
- src/server.js: Main application entry point, Fastify server setup.
- src/routes/sms.js: Defines the routes related to SMS functionality.
- src/config/plivo.js: Initializes and configures the Plivo client.
- .env: Stores sensitive credentials (Plivo Auth ID, Token, Number). Never commit this file to version control.
- .gitignore: Specifies intentionally untracked files that Git should ignore.
- Configure
.gitignore: Addnode_modulesand.envto your.gitignorefile to prevent them from being checked into Git.
.gitignore
node_modules/
.env
*.log
- Configure Environment Variables (
.env): Open the.envfile and add your Plivo credentials and phone number. Replace the placeholder values with your actual credentials found in the Plivo console.
.env .env
# Plivo Credentials - Found on your Plivo Console Dashboard (console.plivo.com/dashboard/)
PLIVO_AUTH_ID=YOUR_PLIVO_AUTH_ID
PLIVO_AUTH_TOKEN=YOUR_PLIVO_AUTH_TOKEN
# Plivo Phone Number - An SMS-enabled number from your Plivo account
PLIVO_SENDER_NUMBER=+1XXXXXXXXXX # Use E.164 format (e.g., +12025551234)
- PLIVO\_AUTH\_ID: Your Plivo account identifier.
- PLIVO\_AUTH\_TOKEN: Your Plivo account secret token.
- PLIVO\_SENDER\_NUMBER: The SMS-enabled Plivo phone number you purchased, in E.164 format (+ followed by country code and number, max 15 digits total per ITU-T E.164 standard).
2. How Do You Implement SMS Sending?
Write the code to initialize the Plivo client and set up the Fastify server.
- Configure Plivo Client: Set up the Plivo client instance using the environment variables.
src/config/plivo.js
'use strict';
const plivo = require('plivo');
require('dotenv').config();
if (!process.env.PLIVO_AUTH_ID || !process.env.PLIVO_AUTH_TOKEN) {
console.error('Error: Plivo Auth ID or Auth Token not found in environment variables.');
console.error('Please check your .env file or environment configuration.');
process.exit(1);
}
const client = new plivo.Client(
process.env.PLIVO_AUTH_ID,
process.env.PLIVO_AUTH_TOKEN
);
module.exports = client;
- Why
dotenvhere? Load environment variables early to ensure the Plivo client can be initialized correctly. - Why the check? Explicitly checking for credentials prevents the application from starting in an invalid state and provides clear error messages.
- Create the SMS Sending Route: Define the Fastify route that will handle the SMS sending request.
src/routes/sms.js
'use strict';
const plivoClient = require('../config/plivo');
require('dotenv').config();
const sendSmsSchema = {
body: {
type: 'object',
required: ['to', 'text'],
properties: {
to: {
type: 'string',
description: 'Destination phone number in E.164 format (e.g., +15551234567). Max 15 digits per ITU-T E.164 standard.',
pattern: '^\\+[1-9]\\d{1,14}$'
},
text: {
type: 'string',
description: 'The content of the SMS message. Max 160 chars (GSM-7) or 70 chars (Unicode) per segment.',
minLength: 1,
maxLength: 1600
},
},
},
response: {
200: {
type: 'object',
properties: {
message: { type: 'string' },
message_uuid: { type: 'array', items: { type: 'string' } },
api_id: { type: 'string' },
},
},
},
};
async function smsRoutes(fastify, options) {
const senderNumber = process.env.PLIVO_SENDER_NUMBER;
if (!senderNumber) {
fastify.log.error('PLIVO_SENDER_NUMBER is not set in environment variables.');
}
fastify.post('/sms/send', { schema: sendSmsSchema }, async (request, reply) => {
if (!senderNumber) {
reply.code(500).send({ error: 'Server configuration error: Sender number not set.' });
return;
}
const { to, text } = request.body;
try {
fastify.log.info(`Attempting to send SMS to ${to} from ${senderNumber}`);
const response = await plivoClient.messages.create(
senderNumber,
to,
text
);
fastify.log.info(`SMS sent successfully. Message UUID: ${response.messageUuid}, API ID: ${response.apiId}`);
reply.code(200).send({
message: 'SMS sent successfully!',
message_uuid: response.messageUuid,
api_id: response.apiId,
});
} catch (error) {
fastify.log.error(`Error sending SMS via Plivo: ${error.message}`);
fastify.log.error(error.stack);
let statusCode = 500;
let errorMessage = 'Failed to send SMS due to an internal server error.';
if (error.statusCode) {
statusCode = error.statusCode >= 500 ? 502 : error.statusCode;
errorMessage = `Failed to send SMS. Plivo API error: ${error.message}`;
}
if (error.message && error.message.includes('rate limit')) {
statusCode = 429;
errorMessage = 'Rate limit exceeded. Please retry after a delay.';
}
reply.code(statusCode).send({
error: errorMessage,
details: error.message,
...(error.errorCode && { error_code: error.errorCode })
});
}
});
}
module.exports = smsRoutes;
- Why Fastify Schema? Fastify's built-in schema validation automatically validates incoming request bodies and serializes responses, reducing boilerplate code and improving security/reliability. The E.164 regex pattern validates phone number format according to ITU-T E.164 standard.
- Why
async/await? Simplifies handling asynchronous operations like the Plivo API call. - Why E.164 Pattern? The regex ^\\\\+[1-9]\\\\d{1,14}$ enforces ITU-T E.164 format: + sign, followed by 1-14 digits (country code 1-3 digits + subscriber number max 12 digits), total max 15 digits.
- Why
try...catch? Essential for handling potential errors during the API call to Plivo (network issues, invalid credentials, API errors, rate limits, etc.). - Rate Limit Handling: Plivo enforces rate limits (default 5 messages/second per account, 100 concurrent requests for MMS). The error handler now specifically catches rate limit errors and returns HTTP 429.
- Set Up the Fastify Server: Configure and start the Fastify server, registering the SMS routes.
src/server.js
'use strict';
require('dotenv').config();
const fastify = require('fastify')({
logger: true,
});
fastify.register(require('./routes/sms'), { prefix: '/api' });
fastify.get('/health', async (request, reply) => {
return { status: 'ok', timestamp: new Date().toISOString() };
});
const start = async () => {
try {
const port = process.env.PORT || 3000;
await fastify.listen({ port: port, host: '0.0.0.0' });
fastify.log.info(`Server listening on port ${port}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();
- Why
logger: true? Enables request/response logging and allows using fastify.log for custom logs. - Why
registerwithprefix? Organizes API routes under a common base path (e.g., /api/sms/send). - Why
0.0.0.0? Makes the server accessible from outside its container/machine in development or production (localhost/127.0.0.1 only allows local connections). - Why Health Check? A simple /health endpoint is standard practice for monitoring services and load balancers.
- Fastify v5 Note: If you're using Fastify v5, ensure you're running Node.js v20 or later. For Fastify v4 (supported until June 30, 2025), Node.js v14+ is sufficient.
- Add Start Script to
package.json: Add a script to easily start your server.
package.json (additions)
{
"scripts": {
"start": "node src/server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
}
3. How Do You Build a Complete API Layer?
Your core API endpoint (POST /api/sms/send) is already set up. Here's how to document and test
it.
API Endpoint Documentation:
- Method: POST
- Path: /api/sms/send
- Body (JSON):
- to (string, required): The destination phone number in E.164 format (e.g., +12025551234). Must match ITU-T E.164 standard: + followed by country code (1-3 digits) and subscriber number (max 12 digits), total max 15 digits.
- text (string, required): The content of the SMS message. Max 160 characters for GSM-7 encoding (standard characters) or 70 characters for UCS-2/Unicode encoding (special characters, emoji). Messages exceeding these limits are automatically split into multiple segments by Plivo.
- Success Response (200 OK):
- message (string): Confirmation message (e.g., "SMS sent successfully!").
- message\_uuid (array of strings): An array containing the unique identifier(s) assigned by Plivo to the message request.
- api\_id (string): Plivo's API request identifier for tracking.
- Example:
{
"message": "SMS sent successfully!",
"message_uuid": ["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"],
"api_id": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
}
- Error Responses:
- 400 Bad Request: Invalid input (missing fields, invalid phone number format, message too long).
{
"statusCode": 400,
"code": "FST_ERR_VALIDATION",
"error": "Bad Request",
"message": "body/to must match pattern \"^\\+[1-9]\\d{1,14}$\""
}
- 429 Too Many Requests: Plivo rate limit exceeded (default 5 messages/second per account).
{
"error": "Rate limit exceeded. Please retry after a delay.",
"details": "Plivo API rate limit error message"
}
- 500 Internal Server Error / 502 Bad Gateway: Server configuration issue or error communicating with Plivo.
{
"error": "Failed to send SMS due to an internal server error.",
"details": "Specific error message from Plivo or internal logic"
}
Testing with curl:
- Start the Server:
npm start
You should see output indicating the server is listening on port 3000 (or your configured PORT).
- Send a Request: Open another terminal window and run the following
curlcommand. Replace+1DESTINATIONNUMBERwith a verified phone number (if using a trial account) in E.164 format.
curl -X POST http://localhost:3000/api/sms/send \
-H "Content-Type: application/json" \
-d '{
"to": "+1DESTINATIONNUMBER",
"text": "Hello from Fastify and Plivo!"
}'
- Check Response: You should receive a JSON response like the success example above, and
the destination phone should receive the SMS shortly after. Check your server logs (
npm startterminal) for detailed request/response information and potential errors.
4. Integrating with Necessary Third-Party Services (Plivo)
We've already integrated Plivo, but let's reiterate the key configuration steps.
- Obtain Credentials:
- Navigate to your Plivo Console: https://console.plivo.com/
- Auth ID & Auth Token: These are prominently displayed on the main dashboard page after logging in.
- Plivo Phone Number: Go to Phone Numbers > Your Numbers. If you don't have one, click Buy Number, search for SMS-enabled numbers in your desired region, and purchase one. Copy the number in E.164 format (+ followed by country code and number, max 15 digits).
- (Trial Account) Sandbox Numbers: Go to Phone Numbers > Sandbox Numbers to add and verify destination numbers you want to send messages to during trial.
- Secure Credential Storage:
- Place your Auth ID, Auth Token, and Plivo Sender Number into the .env file in the project root.
- Crucially: Ensure .env is listed in your .gitignore file to prevent accidentally committing secrets to version control.
- Environment Variables Explained:
.env .env
# Plivo Credentials - Found on your Plivo Console Dashboard (console.plivo.com/dashboard/)
PLIVO_AUTH_ID=YOUR_PLIVO_AUTH_ID # Purpose: Identifies your account to Plivo. Format: String. How to get: Plivo Dashboard.
PLIVO_AUTH_TOKEN=YOUR_PLIVO_AUTH_TOKEN # Purpose: Authenticates your API requests. Format: String (Secret). How to get: Plivo Dashboard.
# Plivo Phone Number - An SMS-enabled number from your Plivo account
PLIVO_SENDER_NUMBER=+1XXXXXXXXXX # Purpose: The 'From' number for outgoing SMS. Format: E.164 String (max 15 digits: + + 1-3 digit country code + max 12 digit subscriber number). How to get: Plivo Console > Phone Numbers > Your Numbers.
- Rate Limiting and Message Queuing: Plivo implements intelligent rate limiting and message queueing:
- Default Limits: 5 messages/second per account for SMS; 100 concurrent API requests for MMS.
- Smart Queue: You can send requests to Plivo at any rate. Messages are queued and throttled automatically based on account-level configuration and destination country regulations.
- Scaling: To increase throughput, distribute traffic across multiple Plivo numbers to stay within per-number rate limits.
- Error Handling: Implement exponential backoff when receiving HTTP 429 (rate limit) errors.
- Fallback Mechanisms (Conceptual): For this basic guide, we don't implement automatic fallback. In a production system, if Plivo experiences downtime, you might consider:
- Retry Logic: Implement retries with exponential backoff (covered briefly in Error Handling).
- Circuit Breaker Pattern: Prevent repeated calls to a failing service.
- Alternative Provider: Integrate a secondary SMS provider and switch if Plivo fails (adds significant complexity).
5. Implementing Error Handling, Logging, and Retry Mechanisms
We've incorporated comprehensive error handling and logging throughout the application.
- Error Handling Strategy: Use
try...catchblocks around external API calls (Plivo). Catch errors, log them with details (fastify.log.error), and return appropriate HTTP status codes (4xx for client errors, 5xx for server/Plivo errors, 429 for rate limits) and informative JSON error messages to the client. Map Plivo errors to relevant HTTP statuses where possible. - Logging: Fastify's built-in Pino logger (
logger: true) provides efficient JSON-based logging for requests, responses, and errors. Custom logs (fastify.log.info,fastify.log.error) add context. In production, logs should be collected and forwarded to a centralized logging system (e.g., Datadog, ELK stack, Splunk).
- Plivo-Specific Error Handling:
- Authentication Errors (401): Invalid Auth ID or Auth Token. Verify credentials in .env.
- Invalid Parameters (400): Invalid phone number format, missing required fields. Check E.164 format compliance.
- Rate Limit Errors (429): Exceeded Plivo's rate limits (5 msg/sec default). Implement exponential backoff and retry logic.
- Insufficient Credits: Account has insufficient balance. Top up account.
- Network Errors (ENOTFOUND, ETIMEDOUT): Connectivity issues. Check network, firewalls, and Plivo's service status.
- Retry Mechanisms (Conceptual): The Plivo Node.js SDK does not have built-in automatic retries for sending messages upon failure. Implementing robust retries requires careful consideration:
- Identify Retryable Errors: Only retry on transient errors (e.g., network timeouts, Plivo 5xx errors, rate limits - 429), not permanent ones (e.g., invalid credentials - 401, invalid number format - 400).
- Exponential Backoff: Increase the delay between retries exponentially (e.g., 1s, 2s, 4s, 8s, 16s) to avoid overwhelming the Plivo API during intermittent issues or rate limiting.
- Max Retries: Limit the number of retries to prevent indefinite loops (e.g., 3-5 retries maximum).
- Idempotency: Ensure retrying a request doesn't result in duplicate messages. Plivo handles some level of idempotency, but designing your system carefully is still important.
- Implementation: You could use libraries like async-retry or p-retry, or build custom logic within your catch block. For this guide's scope, we only log the error and return appropriate status codes.
Example Retry Logic with Exponential Backoff (Conceptual):
const pRetry = require('p-retry');
async function sendSmsWithRetry(senderNumber, to, text) {
return pRetry(
async () => {
return await plivoClient.messages.create(senderNumber, to, text);
},
{
retries: 3,
onFailedAttempt: (error) => {
fastify.log.warn(`SMS send attempt ${error.attemptNumber} failed. ${error.retriesLeft} retries left.`);
},
shouldRetry: (error) => {
return error.statusCode >= 500 || error.statusCode === 429;
},
}
);
}
Testing Error Scenarios:
- Invalid Input: Send a request missing the to or text field, or with an invalid phone number format (e.g., 12345). Expect a 400 error from Fastify validation.
- Invalid Credentials: Temporarily change PLIVO\_AUTH\_ID or PLIVO\_AUTH\_TOKEN in .env and restart the server. Send a valid request. Expect a 401 Unauthorized error from the Plivo API.
- Invalid Sender Number: Use a number not associated with your Plivo account in PLIVO\_SENDER\_NUMBER. Expect an error from Plivo.
- Trial Account Limit: Send to an unverified number using a trial account. Expect a specific Plivo error related to sandbox restrictions.
- Rate Limit: Send rapid successive requests exceeding 5 messages/second. Expect HTTP 429 responses.
6. Creating a Database Schema and Data Layer
Not Applicable: This specific microservice focuses solely on the stateless action of sending an SMS via an external API. It does not require its own database to store state related to the messages (Plivo handles the message persistence and status tracking).
If you needed to track requests, log message attempts internally, or associate messages with users in your system, you would add a database (e.g., PostgreSQL, MongoDB) and a data access layer (e.g., using an ORM like Prisma or Sequelize, or a MongoDB driver).
7. Adding Security Features
- Input Validation: Implemented using Fastify's schema validation
(
sendSmsSchema). This prevents malformed requests and basic injection attempts in thetoandtextfields. The E.164 regex pattern (^\\+[1-9]\\d{1,14}$) enforces ITU-T E.164 standard format. Sanitize or strictly validate any user input that might be used in logs or other contexts. - Secure Credential Handling: Using
.envand.gitignoreprevents exposure of Plivo API keys. In production, use environment variables provided by the deployment platform or a dedicated secrets management system (e.g., HashiCorp Vault, AWS Secrets Manager, Azure Key Vault).
- Rate Limiting: Protect against brute-force attacks or abuse by limiting the number of
requests per IP address or user. Use the
@fastify/rate-limitplugin (formerlyfastify-rate-limit).
- Install: npm install @fastify/rate-limit
- Register in
server.js:
const rateLimit = require('@fastify/rate-limit');
fastify.register(rateLimit, {
max: 100,
timeWindow: '1 minute',
errorResponseBuilder: (request, context) => {
return {
statusCode: 429,
error: 'Too Many Requests',
message: `Rate limit exceeded. You can make ${context.max} requests per ${context.after}. Retry after ${context.ttl}ms.`
};
}
});
fastify.register(require('./routes/sms'), { prefix: '/api' });
- Security Headers: Use a plugin like
@fastify/helmetto set various HTTP headers (likeX-Frame-Options,Strict-Transport-Security,X-Content-Type-Options, etc.) to protect against common web vulnerabilities (XSS, clickjacking, etc.).
- Install: npm install @fastify/helmet
- Register in
server.js:
const helmet = require('@fastify/helmet');
fastify.register(helmet);
- HTTPS: Always run your API over HTTPS in production using a reverse proxy like Nginx, Caddy, or your cloud provider's load balancer to handle SSL/TLS termination. Never expose plain HTTP APIs to the internet in production.
- API Key Authentication (Production Enhancement): For production, add API key
authentication to your
/api/sms/sendendpoint to prevent unauthorized usage. Store API keys securely and validate them in a Fastify hook before processing requests.
8. Handling Special Cases Relevant to the Domain
- E.164 Format: Strictly enforce the E.164 format for phone numbers as required by Plivo.
The ITU-T E.164 standard specifies: + sign, followed by country code (1-3 digits), followed by
subscriber number (max 12 digits), total maximum 15 digits. Our schema validation uses the regex
^\\+[1-9]\\d{1,14}$to enforce this format. - Character Encoding (GSM-7 vs. UCS-2/Unicode):
- GSM-7 Encoding: Standard SMS uses the GSM-7 character set. A single segment contains 160 characters.
- UCS-2/Unicode Encoding: Using characters outside the GSM-7 set (like many emojis, accented characters, non-Latin scripts) automatically switches the message to UCS-2 encoding, which limits segments to 70 characters.
- Plivo handles this automatically, but be aware that messages containing even one Unicode character will be shorter per segment and may cost more if they split into more parts.
- Message Concatenation (Multipart SMS):
- If a message exceeds the character limit for a single segment (160 for GSM-7, 70 for UCS-2), Plivo automatically splits it into multiple linked segments (concatenated SMS).
- Header Overhead: Concatenated messages include a small header (6-7 bytes), reducing available characters per segment to 153 characters for GSM-7 or 67 characters for UCS-2.
- Each segment consumes SMS credits. A long message can result in multiple billed messages. The Plivo SDK and API handle the splitting and concatenation headers automatically.
- Sender ID Restrictions:
- US/Canada: Generally require using a Plivo long code (10-digit US number) or Toll-Free number as the sender ID (src). Alphanumeric sender IDs are often blocked by carriers.
- Other Countries: Rules vary significantly. Some countries allow alphanumeric sender IDs (e.g., MyCompany), which may need pre-registration with carriers or regulatory bodies. Check Plivo's country-specific documentation for sender ID regulations.
- Our code uses the PLIVO\_SENDER\_NUMBER environment variable, which should be set to a Plivo-provisioned phone number in E.164 format for US/Canada.
- Rate Limits (Plivo):
- Account-Level Default: 5 messages per second per account (as of 2025).
- MMS Concurrent Requests: 100 simultaneous API requests for MMS.
- Smart Queue: Plivo's intelligent queueing system manages delivery throttling based on destination country regulations and carrier requirements.
- Scaling Strategy: To exceed single-number throughput limits, distribute traffic across multiple Plivo phone numbers.
- Sending too rapidly can result in HTTP 429 (Too Many Requests) errors. Implement client-side throttling, queueing, or exponential backoff when handling bulk messages.
9. Implementing Performance Optimizations
- Fastify Choice: Using Fastify already provides a high-performance foundation due to its low overhead and efficient request lifecycle. Fastify can serve 70,000+ requests/second in benchmarks (as of Fastify v4).
- Asynchronous Operations: Using
async/awaitensures Node.js's event loop is not blocked during the Plivo API call, allowing the server to handle concurrent requests efficiently. - Logging: Pino (Fastify's default logger) is designed for high performance with minimal overhead. Avoid excessive or synchronous logging in hot paths.
- Caching: Not directly applicable for the action of sending a unique SMS. Caching might be relevant if you frequently look up user data before sending, but not for the Plivo API call itself.
- Connection Pooling: The Plivo Node.js SDK uses the underlying HTTP client (typically
Node.js
httpsmodule) which manages connection pooling automatically. Ensure keep-alive is enabled (default in Node.js). - Load Testing: Use tools like
k6,autocannon, orwrkto simulate traffic and identify bottlenecks under load. Monitor CPU, memory, and event loop lag during tests.
autocannon -m POST -H "Content-Type=application/json" -b '{"to":"+1DESTINATIONNUMBER", "text":"Load test message"}' http://localhost:3000/api/sms/send
- Profiling: Use Node.js built-in profiler (
node --prof) or tools like0x,clinic.js, or Chrome DevTools to analyze CPU usage and identify performance bottlenecks within your code.
10. Adding Monitoring, Observability, and Analytics
- Health Checks: The /health endpoint provides a basic mechanism for load balancers or monitoring systems (like UptimeRobot, Nagios, Datadog Synthetics) to check if the service is running.
- Performance Metrics: Instrument your application to track key metrics:
- Request latency (Fastify can help).
- Request rate (requests per second).
- Error rates (specifically Plivo API errors vs. internal errors).
- Node.js process metrics (CPU, memory, event loop lag).
- Use libraries like prom-client for Prometheus integration or platform-specific agents (Datadog, New Relic).
- Error Tracking: Integrate with services like Sentry, Bugsnag, or Datadog APM to capture, aggregate, and alert on application errors in real-time, providing more context than just logs.
- Structured Logging: The JSON logs produced by Pino are easily parseable by log aggregation platforms (ELK, Splunk, Datadog Logs, etc.), enabling searching, filtering, and dashboard creation based on log data (e.g., tracking SMS send rates, error types).
- Dashboards: Create dashboards in your monitoring/logging tool to visualize key metrics: SMS send volume over time, error rates, API latency percentiles, health check status.
- Alerting: Configure alerts based on critical thresholds:
- High error rate (e.g., >5% Plivo API errors).
- Increased latency (e.g., p95 latency > 1s).
- Health check failures.
- Server resource exhaustion (high CPU/memory).
11. Troubleshooting and Caveats
- Common Errors & Solutions:
Error: Plivo Auth ID or Auth Token not found...: Ensure PLIVO\_AUTH\_ID and PLIVO\_AUTH\_TOKEN are correctly set in your .env file and the file is being loaded (require('dotenv').config();).401 Unauthorized(from Plivo): Incorrect Auth ID or Auth Token. Double-check values in .env against the Plivo console.Invalid 'src' parameter/Invalid 'dst' parameter(from Plivo): Phone numbers are likely not in E.164 format or are otherwise invalid. Ensure numbers start with + and country code, max 15 digits total. Check PLIVO\_SENDER\_NUMBER in .env and the to number in the request.Message Sending disallowed to Sandbox Number(from Plivo): You are using a trial account and trying to send to a number not verified in Sandbox Numbers on the Plivo console. Add and verify the destination number.Message Sender 'src' number not owned by you or is invalid(from Plivo): The PLIVO\_SENDER\_NUMBER in .env is incorrect or not associated with your Plivo account.- Fastify
FST_ERR_VALIDATION(400 Bad Request): The request body doesn't match the schema defined in src/routes/sms.js (e.g., missing to or text, or they don't match the pattern/type requirements). ENOTFOUND/ETIMEDOUT: Network issues connecting from your server to the Plivo API. Check server connectivity, firewalls, DNS resolution, and Plivo's service status page.HTTP 429 Too Many Requests: Exceeded Plivo's rate limit (default 5 messages/second per account). Implement exponential backoff and retry logic. Consider distributing load across multiple Plivo numbers.
- Platform Limitations:
- Trial Account: Can only send to verified sandbox numbers. Limited throughput. Plivo branding may be added to messages. Insufficient credits may cause failures.
- Sender ID Rules: Vary significantly by country. US/Canada generally require Plivo numbers. Alphanumeric IDs may need registration elsewhere. Check Plivo's country-specific SMS guidelines.
- Rate Limits: Plivo imposes limits on API calls per second/minute (5 msg/sec default for SMS, 100 concurrent for MMS). High-volume sending requires handling potential rate limit errors (HTTP 429) with queuing and backoff strategies.
- Version Compatibility:
- Fastify v5: Requires Node.js v20 or later (as of 2025).
- Fastify v4: Supports Node.js v14-v22, but reaches EOL on June 30, 2025).
- Plivo SDK: Check npm (npmjs.com/package/plivo) for the latest version and compatibility notes.
- Ensure your Node.js version is compatible with the versions of Fastify and the Plivo SDK specified in package.json. Check the respective libraries' documentation for compatibility notes if issues arise.
- Edge Cases:
- Very long messages splitting into many segments (10+ segments).
- Messages with complex Unicode characters or emoji combinations.
- Destination numbers in regions with delivery issues, carrier blocks, or strict regulations.
- Sending to numbers during carrier maintenance windows.
- Workarounds:
- For trial accounts, always verify numbers first in Plivo console under Sandbox Numbers.
- For rate limits, implement client-side throttling, queueing (using Redis, RabbitMQ, or in-memory queues), or distribute load across multiple Plivo numbers.
- For very long messages, consider splitting manually or warning users about multi-segment costs.
12. Deployment and CI/CD
Basic Deployment (Conceptual):
- Build/Preparation: Ensure all dependencies are installed
(
npm ci --productioncan skip devDependencies for smaller deployment packages). - Environment Configuration: Set the necessary environment variables
(
PLIVO_AUTH_ID,PLIVO_AUTH_TOKEN,PLIVO_SENDER_NUMBER,PORT,NODE_ENV=production) on the target server or container environment. Do not deploy the.envfile directly; use the platform's environment variable management (e.g., Docker secrets, Kubernetes ConfigMaps/Secrets, cloud provider environment variables). - Node.js Version: Ensure the deployment environment has the correct Node.js version installed:
- Node.js v20+ for Fastify v5
- Node.js v14+ for Fastify v4 (EOL June 30, 2025)
- Process Management: Use a process manager like PM2 or rely on container orchestration
(Docker, Kubernetes) to run and manage the Node.js process (
node src/server.js).
- PM2 Example:
npm install -g pm2
pm2 start src/server.js --name fastify-plivo-sms
pm2 logs fastify-plivo-sms
pm2 monit
pm2 save
pm2 startup
- Reverse Proxy (Recommended): Set up Nginx, Caddy, or similar to act as a reverse proxy in front of your Fastify app. This handles:
- SSL/TLS termination (HTTPS) β Encrypts traffic
- Load balancing β Distributes requests across multiple instances if scaling horizontally
- Static file serving β If needed for other assets
- Rate limiting β Additional layer of protection
- Request buffering β Improves handling of slow clients
Docker Deployment Example:
# Dockerfile
FROM node:20-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install production dependencies
RUN npm ci --production
# Copy application code
COPY src ./src
# Expose port
EXPOSE 3000
# Set environment to production
ENV NODE_ENV=production
# Start application
CMD ["node", "src/server.js"]
CI/CD Pipeline (Conceptual):
A typical pipeline using services like GitHub Actions, GitLab CI, or Jenkins:
- Trigger: On push/merge to the main branch.
- Checkout Code: Get the latest source code from repository.
- Setup Node.js: Specify the required Node.js version (v20+ for Fastify v5).
- Install Dependencies: npm ci (uses package-lock.json for deterministic installs).
- Linting/Testing: Run code linters (e.g., ESLint) and automated tests (unit, integration).
- Security Scanning: Scan dependencies for vulnerabilities (npm audit, Snyk, etc.).
- Build (if necessary): If using TypeScript or a build step, compile the code.
- Package: Create a deployment artifact (e.g., Docker image, zip archive).
- Deploy: Push the artifact to the target environment (e.g., cloud provider, Kubernetes cluster, VM).
- Post-Deploy Steps: Run health checks, smoke tests, notify team on Slack/email.
- Rollback Strategy: Implement automated rollback if health checks fail post-deployment.
Example GitHub Actions Workflow:
name: CI/CD Pipeline
on:
push:
branches: [ main ]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '20'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
- name: Run tests
run: npm test
- name: Build Docker image
run: docker build -t fastify-plivo-sms:${{ github.sha }} .
- name: Push to registry
run: docker push fastify-plivo-sms:${{ github.sha }}
- name: Deploy to production
run: kubectl set image deployment/fastify-plivo-sms fastify-plivo-sms=fastify-plivo-sms:${{ github.sha }}
Summary
You've built a production-ready SMS API with Plivo, Node.js, and Fastify featuring:
- Modern Stack: Fastify v5 with Node.js v20+ for optimal performance (or Fastify v4 with Node.js v14+ for compatibility until June 2025).
- Verified Integration: Plivo Node.js SDK with proper authentication and E.164 format validation per ITU-T standard.
- Robust Error Handling: Comprehensive error mapping for Plivo API errors, rate limits (HTTP 429), and network issues.
- Security: Input validation, rate limiting, security headers, and secure credential management.
- Production-Ready: Health checks, structured logging, performance optimization guidelines, and deployment strategies.
- Rate Limit Awareness: Understanding of Plivo's 5 messages/second default limit and smart queue system.
Next Steps:
- Implement retry logic with exponential backoff for transient failures.
- Add API key authentication for production use.
- Set up monitoring and alerting with your preferred observability platform.
- Deploy to your cloud provider with proper environment variable management.
- Scale horizontally by distributing load across multiple Plivo numbers if needed.
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 β