Send SMS with Fastify and Infobip - code-examples -

Frequently Asked Questions

Set up a Fastify project with the Infobip SDK, create a /send-sms route, and configure the Infobip client with your API key and base URL. The route handler should extract recipient, sender, and message details from the request body and pass them to the Infobip SDK's send method. This abstracts the API interaction into a service layer within your Fastify app.
Free Infobip accounts can only send SMS messages to the phone number verified during registration. Attempts to send to other numbers will fail. This is a common issue for developers starting with the free trial, so ensure your recipient matches your registered number.
Fastify is a high-performance Node.js web framework known for its speed, extensibility, and excellent developer experience. It provides a robust foundation for building a production-ready SMS service with features like schema validation and easy plugin integration.
Rate limiting is crucial for preventing abuse, controlling costs, and protecting your application from overload. You should implement rate limiting as soon as possible, especially before deploying to production. The fastify-rate-limit plugin makes this easy.
No, using a free trial for production is not recommended due to the limitation of only being able to send to your registered number. Upgrade to a paid Infobip account for full functionality and remove sending restrictions.
The @infobip-api/sdk simplifies interaction with the Infobip API by handling authentication, API requests, and response parsing. It streamlines the SMS sending process within your Fastify application.
Infobip requires international format without the + sign or leading 00. The provided example code uses JSON Schema validation, and stricter validation with libphonenumber-js is recommended for more robust handling of diverse input formats.
Store your Infobip API key and base URL in a .env file and load them using the dotenv package. Ensure .env is added to your .gitignore file to prevent accidental commits. In production, use your deployment environment's secure secret management system.
Implement a try-catch block around the Infobip SDK call within your /send-sms route handler. Parse specific error details from error.response.data and return appropriate error responses to clients. Provide generic error messages for unexpected errors, and log more detailed information for troubleshooting.
Common errors include missing or incorrect API keys/base URLs in the .env file, attempting to send SMS to numbers other than your registered number on a free trial, incorrect phone number formatting, or network issues connecting to the Infobip API. The article covers troubleshooting steps for each.
401 errors usually indicate an invalid or expired API key. Verify that the API key in your .env file matches the active key in your Infobip dashboard and that your base URL is correct.
Consider message encoding if you're sending messages with non-GSM-7 characters (like emojis or certain accented letters). The Infobip API supports Unicode but may have different character limits and cost implications for multi-part messages.
Fastify's Pino logger provides a base. Log key events like requests, successes, and errors with context. In production, configure log levels, forward to a centralized system, and monitor metrics like API latency, error rates, and Infobip response codes.
Use exponential backoff with a library like async-retry. Only retry on specific transient error types (e.g., 503 Service Unavailable, network timeouts) and avoid retrying 4xx errors or successful submissions with uncertain status. Retries are not included in the example code for safety and simplicity.