Frequently Asked Questions
Build a Fastify API endpoint that accepts recipient numbers and message text, then integrates with the Infobip SMS API via its Node.js SDK. This allows your application to send high-volume SMS notifications, alerts, or marketing messages programmatically and reliably. The Fastify framework's performance and plugin architecture make it ideal for this purpose.
The Infobip Node.js SDK simplifies interaction with the Infobip SMS API, allowing you to send SMS messages programmatically within your Node.js application. It handles authentication, request formatting, and response parsing, making integration straightforward.
Fastify is chosen for its speed and extensible plugin system, which are beneficial for high-throughput applications like bulk SMS sending. Its lightweight nature and ease of use also contribute to efficient development and a smaller footprint.
For high-throughput SMS sending, a message queue like BullMQ or RabbitMQ is highly recommended. It decouples API requests from actual SMS delivery, allowing the API to respond quickly while background workers handle sending, retries, and rate limiting.
Yes, storing your Infobip API_KEY
and BASE_URL
in environment variables (.env
file locally, platform secrets in production) is crucial for security. This keeps sensitive information out of your codebase and allows for environment-specific configurations.
Implement a try...catch
block around the fastify.infobip.channels.sms.send
call to handle potential errors during the API request. Log error details using fastify.log.error
and forward relevant error information to the client, including Infobip's error codes if available.
The Infobip plugin initializes and encapsulates the Infobip Node.js SDK client, making it accessible throughout the Fastify application. It uses environment variables to configure the client and provides a reusable way to interact with the Infobip API.
Use the fastify-rate-limit
plugin to control the rate of incoming requests to your SMS API. This protects against abuse, helps manage costs, and prevents exceeding Infobip's rate limits. Configure the max
requests and timeWindow
according to your needs.
Always format phone numbers using the international E.164 format (e.g., +14155552671). While Infobip performs validation, ensuring correct formatting on your end minimizes errors like EC_INVALID_DESTINATION_ADDRESS.
Check Infobip's delivery reports (DLRs) either via webhooks (using the notifyUrl parameter) or by polling the DLR endpoint. These reports provide detailed status information, such as DELIVERED, FAILED, or error codes like EC_ABSENT_SUBSCRIBER, which help identify the reason for non-delivery.
Use environment variables, input validation, rate limiting, HTTPS, and authentication mechanisms like API keys or JWT. Consider using Helmet for setting security headers to protect against common web vulnerabilities.
SMS messages are limited by character encoding: GSM-7 allows 160 characters per segment, while UCS-2 (for non-GSM characters) allows 70. Longer messages are split into multiple segments and cost more. Infobip handles this segmentation automatically.
The 202 Accepted status indicates that the SMS broadcast request has been received and accepted for processing by Infobip. Actual delivery is asynchronous and happens later, the status of which can be tracked via delivery reports.
Use PaaS (Heroku, Render, Fly.io), containers (Docker, Kubernetes), or VMs (EC2, GCE). Ensure environment variables are set correctly in production and use process management tools like PM2. Implement a CI/CD pipeline for automated builds, tests, and deployments.
Use manual verification with curl for basic checks. Implement automated unit and integration tests using a testing framework like Tap or Jest. Mock the Infobip API calls in tests to isolate testing logic and simulate different scenarios like successes and errors.
Build a Bulk SMS API with Fastify and Infobip Node.js SDK
This guide details how to build a robust and scalable API service using Fastify and the Infobip Node.js SDK to send bulk SMS messages. You'll learn everything from project setup and core integration to error handling, security, and deployment best practices.
This service solves the common need for applications to send programmatic, high-volume SMS notifications, alerts, or marketing messages reliably. Fastify offers exceptional performance and a developer-friendly plugin architecture, making it ideal for high-throughput API applications. Infobip provides a powerful and globally reachable communication platform with well-documented APIs and SDKs, simplifying the integration process for sending SMS messages at scale.
Project Overview and Goals
Goal: Create a Fastify API endpoint that accepts a list of phone numbers and message text, then uses the Infobip API to send that message to all recipients efficiently.
Key Features:
Technology Stack:
@infobip-api/sdk
dotenv
@fastify/rate-limit
(optional but recommended)System Architecture:
Note: The following Mermaid diagram requires specific platform support or JavaScript libraries to render. It will appear as a raw code block in standard Markdown.
Prerequisites:
1. Project Setup: Initialize Your Fastify SMS Service
Initialize your Node.js project and install the necessary dependencies.
Create Project Directory:
Initialize Node.js Project:
This creates a
package.json
file.Install Dependencies:
fastify
– The core web framework@infobip-api/sdk
– The official Infobip Node.js SDK for interacting with their APIsdotenv
– To load environment variables from a.env
file for secure credential managementInstall Development Dependencies (Optional but Recommended):
nodemon
– Automatically restarts the server during development when files changepino-pretty
– Makes Fastify's default JSON logs more human-readable during developmentConfigure
package.json
Scripts: Open yourpackage.json
file and add or modify thescripts
section:start
– Runs the application directly with Nodedev
– Runs the application usingnodemon
for auto-reloading and pipes logs throughpino-pretty
"type": "module"
– Enables modern ES Module syntax (import
/export
)Create Project Structure:
src/
– Contains your main application codesrc/server.js
– The main entry point for your Fastify serversrc/routes/
– Holds route definitionssrc/plugins/
– Contains Fastify plugins (like your Infobip client setup).env
– Stores environment variables (API keys, etc.). Never commit this file!.gitignore
– Specifies intentionally untracked files that Git should ignoreConfigure
.gitignore
: Add the following lines to your.gitignore
file to prevent sensitive information and unnecessary files from being committed:Configure Environment Variables (
.env
): Open the.env
file and add your Infobip credentials. You can find these in your Infobip account dashboard under API Keys management. The Base URL is specific to your account.INFOBIP_API_KEY
– Your secret API keyINFOBIP_BASE_URL
– The specific API endpoint URL provided by Infobip for your accountSENDER_ID
– The phone number or alphanumeric ID that will appear as the sender of the SMS. This often needs to be registered or approved by Infobip depending on the destination country regulations.PORT
– The port your Fastify server will listen on. Defaults to 3000 if not set.LOG_LEVEL
– Controls the logging verbosityWhy Environment Variables? Storing credentials directly in code is a major security risk. Environment variables allow you to configure the application differently for development, testing, and production without changing the code, and keep secrets out of version control.
dotenv
loads these variables intoprocess.env
during development (when called early inserver.js
). In production, set these variables directly in your deployment environment.2. Implementing Core Functionality (Infobip Plugin)
Create a Fastify plugin to initialize and encapsulate the Infobip SDK client, making it reusable across your application.
Create the Infobip Plugin: Open
src/plugins/infobip.js
and add the following code:dotenv
being called inserver.js
to populateprocess.env
new Infobip(…)
: Initializes the SDK client using the API key and base URL.AuthType.ApiKey
explicitly tells the SDK how to authenticate.fastify.decorate('infobip', …)
: Attaches the initializedinfobipClient
to the Fastify instance (and request/reply objects) under the nameinfobip
. This makes it accessible in your route handlers viafastify.infobip
orrequest.infobip
.fp(infobipPlugin, …)
: Wraps the plugin function usingfastify-plugin
. This ensures that decorators added by this plugin (like.infobip
) are available globally within the Fastify instance, not just within the scope of the plugin itself.3. Building the API Layer (Broadcast Route)
Create the API endpoint that receives broadcast requests and uses your Infobip plugin.
Create the Broadcast Route Handler: Open
src/routes/broadcast.js
and add the following:broadcastSchema
using Fastify's built-in AJV support. This automatically validates incoming request bodies. Thepattern
uses double backslashes (\\
) as required within a JavaScript string literal to represent literal backslashes for the regex engine.to
array into thedestinations
array structure required by the Infobip SDK'ssend
methodsenderId
: Use theSENDER_ID
from the environment variables (loaded inserver.js
)fastify.infobip.channels.sms.send(payload)
makes the API call202 Accepted
along with thebulkId
and initial message statuses from Infobip's synchronous responsetry…catch
block handles potential errors, logging details and attempting to forward specific Infobip error information4. Integrating with Third-Party Services (Infobip Setup)
This section focuses on configuring the Fastify application to use the Infobip service correctly, building upon the initial setup in Section 1.
Obtain Infobip Credentials:
xxxxxx.api.infobip.com
).+14155550100
) based on your target countries and regulations. Check the "Numbers" or "Senders" section in the portal. Registration or approval might be required.Configure Environment Variables:
.env
file (for local development) or your production environment variables include the correctINFOBIP_API_KEY
,INFOBIP_BASE_URL
, andSENDER_ID
.Secure API Keys:
.gitignore
file (Section 1, Step 7) prevents committing the.env
file.env
file. Set the environment variables (INFOBIP_API_KEY
,INFOBIP_BASE_URL
,SENDER_ID
,PORT
,LOG_LEVEL
, etc.) directly through your hosting platform's configuration interface (e.g., Heroku Config Vars, AWS Secrets Manager, Kubernetes Secrets). This keeps secrets out of your codebase and version control.Plugin and Route Usage:
src/plugins/infobip.js
) readsINFOBIP_API_KEY
andINFOBIP_BASE_URL
fromprocess.env
to initialize the SDKsrc/routes/broadcast.js
) readsSENDER_ID
fromprocess.env
Fallback Mechanisms (Consideration):
5. Error Handling, Logging, and Retry Strategies
Fastify uses Pino for efficient logging. Refine the basic error handling you've already added.
Logging:
fastify.log.info
andfastify.log.error
calls add context.pino-pretty
(vianpm run dev
) enhances readabilityLOG_LEVEL
environment variable (info
,warn
,error
).Error Handling Strategy:
catch
block insrc/routes/broadcast.js
logs Infobip's detailed error (error.response.data
) and forwards the status code and details to the client500 Internal Server Error
Retry Mechanisms (Advanced):
202 Accepted
Testing Error Scenarios:
.env
or mocking the SDK in tests (Section 13)6. Creating a Database Schema and Data Layer (Optional Enhancement)
A database (PostgreSQL, MongoDB, etc.) with an ORM (Prisma, Sequelize) can add features like storing recipient lists, tracking broadcast jobs (
bulkId
, status), message templates, and delivery statuses (via webhooks). This adds complexity. For this guide, focus on direct API interaction.7. Security Best Practices for SMS APIs
Protect your API and credentials.
Environment Variables: Keep secrets out of code and Git. Use platform secrets management in production (Section 4).
Input Validation: Implemented via Fastify schema (Section 3). Ensures data conforms to expectations (e.g., E.164 pattern, length limits).
Rate Limiting: Protect against abuse and control costs:
npm install @fastify/rate-limit
src/server.js
:max
andtimeWindow
appropriatelyHTTPS: Enforce HTTPS in production (usually handled by load balancers or PaaS)
Authentication/Authorization: Protect the
/broadcast/sms
endpoint itself:Authorization: Bearer YOUR_KEY
) using@fastify/auth
or similarHelmet: Set security-related HTTP headers:
npm install @fastify/helmet
await fastify.register(helmet);
insrc/server.js
Dependency Updates: Regularly run
npm audit
and update dependencies (npm update
)Least Privilege (Infobip Key): Use Infobip API keys with the minimum required permissions if possible
8. SMS-Specific Considerations and Compliance
SMS specifics to consider:
+14155552671
). Rely on Infobip's validation but handle their specific errors (EC_INVALID_DESTINATION_ADDRESS
).EC_ILLEGAL_SENDER
errors.notifyUrl
parameter in API call) or poll the DLR endpoint (GET /sms/1/reports
) to get final status (DELIVERED, FAILED, etc.). Webhooks are preferred for real-time updates.sendAt
parameter to schedule messages appropriately for recipient time zones, especially for marketing.9. Performance Optimization for High-Volume SMS
Make the service handle high load:
/sms/2/text/advanced
endpoint with thedestinations
array is batching. Break very large lists (millions) into multiple API calls (e.g., 1,000 recipients per call), ideally managed via a job queue.202 Accepted
quickly; workers handle Infobip calls, retries, and rate limiting. This is the most impactful optimization for high throughput.LOG_LEVEL
.clinic.js
to detect event loop blocks429 Too Many Requests
errors.10. Monitoring, Observability, and Analytics
Understand service health and performance:
/health
endpoint checking essential services (Infobip client initialization, DB connection if used). Use for liveness and readiness probes (see example insrc/server.js
, Section 13).@fastify/metrics
to expose Prometheus metrics (/metrics
) for request rates, latency, error rates, queue size, and custom metrics (infobip_sms_sent_total
). Visualize in Grafana.@sentry/node
, etc.) for real-time error reporting and alerting.@autotelic/fastify-opentelemetry
) in complex microservice setupsbulkId
)11. Troubleshooting Common Infobip API Issues
Common issues:
Infobip API Key or Base URL missing…
– Check.env
or production environment variablesAUTHENTICATION_ERROR
– Invalid API key. Verify key and ensure it's active.BAD_REQUEST
– Invalid payload. Check details:EC_INVALID_DESTINATION_ADDRESS
(bad number format),EC_INVALID_SENDER_OR_FROM_FIELD
(bad sender ID), empty or excessively long text. Use E.164 format. Verify sender ID registration.MESSAGE_LIMIT_EXCEEDED
– Exceeded account MPS. Slow down requests (implement outbound rate limiting in workers).EC_ABSENT_SUBSCRIBER
,EC_ANTI_SPAM_REJECTION
,EC_INSUFFICIENT_FUNDS
). Verify number, check balance, sender ID validity, and country regulations. Contact Infobip support withmessageId
.package.json
. Review changelogs before upgrading.notifyUrl
) or polling if final status is needed12. Deployment and CI/CD Strategies
Get your service into production:
Build Step: Not needed for plain JavaScript unless using TypeScript (
npm run build
viatsc
) or bundling.Deployment Environments:
web: npm start
). Handles load balancing and HTTPS.Dockerfile
. EnsureNODE_ENV=production
,HOST=0.0.0.0
, and do not copy.env
into the image. Pass secrets via runtime environment.Process Management (PM2): Use PM2 for restarts, clustering, and logging in VM or non-containerized environments:
npm install -g pm2
pm2 start src/server.js --name fastify-infobip-sms -i max
(cluster mode)pm2 list
,pm2 logs
,pm2 reload
CI/CD Pipeline (GitHub Actions Example):
.github/workflows/deploy.yml
to automate build, test, and deploy on push tomain
npm ci
(clean install), lint, test (npm test
), deploy (e.g., trigger PaaS hookcurl -X POST ${{ secrets.RENDER_DEPLOY_HOOK_URL }}
,docker push
, etc.)RENDER_DEPLOY_HOOK_URL
,DOCKER_PASSWORD
) in GitHub Actions secretsRollback Procedures: Use platform features (PaaS deploy history, Docker image tags, Kubernetes rollbacks) or manual redeploy of previous versions. Consider blue-green or canary deployments.
13. Testing Your SMS Broadcast API
Ensure correctness and reliability:
Manual Verification (
curl
):npm run dev
curl
. Verify expected responses (202 Accepted withbulkId
, 400 Bad Request with error details).Automated Testing (Unit/Integration):
tap
(Fastify's default), Jest, or Node's test runnernpm install --save-dev tap
package.json
:"test": "tap test/**/*.test.js"
test/broadcast.test.js
)build
function: Modifysrc/server.js
to export abuild
function that creates the Fastify instance without starting it (see finalserver.js
code below)app.infobip.channels.sms.send
with a mock function (async (payload) => { … return mockResponse; }
orthrow mockError;
) to isolate tests from the actual Infobip API and control scenariosapp.inject()
: Use Fastify's injection mechanism to simulate HTTP requests directly against the app instanceTest Coverage:
tap --coverage-report=html
). Aim for high coverage.Verification Checklist:
/broadcast/sms
accepts valid POST requests?bulkId
?Frequently Asked Questions (FAQ)
How do I get an Infobip API key?
Log in to your Infobip Portal at https://portal.infobip.com/, navigate to the "Developers" or "API Keys" section, and create or retrieve your API key. You'll also find your account-specific Base URL on this page.
What is the maximum number of recipients per API call?
While Infobip's
/sms/2/text/advanced
endpoint supports batching multiple recipients, it's recommended to limit batches to around 1,000 recipients per API call for optimal performance. For larger lists, break them into multiple calls, ideally managed via a job queue.How do I track SMS delivery status?
SMS delivery is asynchronous. Use Infobip webhooks by adding the
notifyUrl
parameter to your API call for real-time delivery reports, or poll the DLR endpoint (GET /sms/1/reports
) using thebulkId
ormessageId
. Webhooks are the preferred method for production systems.Why are my messages being rejected with EC_ILLEGAL_SENDER?
This error indicates your Sender ID isn't approved or allowed in the destination country. Verify that your Sender ID is properly registered in your Infobip account and complies with the destination country's regulations. Some countries require pre-registration of sender IDs.
How can I reduce SMS costs for bulk messaging?
To reduce costs: (1) Keep messages under 160 characters for GSM-7 encoding to avoid multi-part messages, (2) Clean and validate your recipient lists to avoid failed deliveries, (3) Use appropriate message routing through Infobip's optimization features, and (4) Monitor your usage patterns to identify inefficiencies.
What should I do if I hit rate limits (429 errors)?
If you receive
429 Too Many Requests
errors, you're exceeding your account's MPS (Messages Per Second) limit. Implement outbound rate limiting in your worker processes using a job queue system like BullMQ, and consider contacting Infobip support to increase your account limits if needed.Next Steps and Additional Resources
After implementing your bulk SMS API, consider these enhancements:
For more information, consult the official Infobip API documentation and the Fastify documentation.
Final Code Structure & Server Entry Point
Ensure your
src/server.js
ties everything together: