Implement SMS OTP / 2FA in Node.js Express with Infobip - code-examples -

Frequently Asked Questions

Implement 2FA by integrating the Infobip 2FA API into your Node.js Express app. Create API endpoints to send OTPs via SMS to the user's phone number and then verify the code they enter. This enhances security by adding an extra layer of verification beyond passwords.
The Infobip 2FA API is the core service used for generating, sending, and verifying one-time passwords (OTPs) through various channels like SMS, voice calls, or email. This guide focuses specifically on using SMS for delivering OTPs.
Infobip simplifies OTP implementation by handling the complexities of generation, delivery, and verification. It allows developers to focus on their application logic rather than managing SMS gateways and other infrastructure.
To send an OTP, make a POST request to the /2fa/2/pin endpoint of the Infobip API. Provide the application ID, message ID, and the user's phone number in the request body.
The `pinId` should be removed from your data store immediately after successful verification. This prevents its reuse and enhances security. It's important to use a persistent data store like Redis or a database for managing `pinId`s in production.
No, using in-memory storage like the example's `activePinIds` is unsuitable for production. This is because data is lost on server restarts, and it doesn't work with multiple server instances. Use Redis or a database instead.
Verify the OTP by making a POST request to /2fa/2/pin/{pinId}/verify, providing the `pinId` (received from the send OTP request) and user-entered OTP. The response will indicate whether verification was successful.
The recommended format for phone numbers when using Infobip is E.164. This international standard format ensures consistent and reliable delivery. An example is +14155552671.
Improve error handling by using specific HTTP status codes (400 for bad input, 500 for server errors, 401 for invalid OTPs) and logging detailed errors server-side while providing generic messages to the client.
Use rate limiting to prevent brute-force attacks, validate inputs thoroughly, secure your API credentials, and ensure that the pinId is not exposed to the client in responses.
Returning the `pinId` to the client is a security risk. It's a server-side identifier. Exposing it unnecessarily could lead to potential misuse or information leakage if not carefully handled client-side.
Redis is recommended for storing `pinId`s due to its speed and built-in support for Time-To-Live (TTL), which automatically expires entries. A relational database can also be used but requires manual cleanup of expired entries.
Set up Infobip by creating a 2FA application and a message template in your Infobip account. You then store the application ID, message ID, API key, and base URL securely in your application's environment variables.
This guide utilizes Node.js with Express.js for the web server, the Infobip 2FA API for OTP services, Axios for HTTP requests, and dotenv for managing environment variables.