This project demonstrates how to use Spring Boot to send SMS messages and generate OTPs (One-Time Passwords) using Twilio. It includes integration with Redis to store OTPs after they are generated, utilizes Docker Compose for containerized deployments, and provides functionality to validate OTPs when users submit them for verification.
- Java 17: The programming language used for developing the application.
- Spring Boot 3.3.1: The framework used to build and run the application.
- Twilio SDK 7.34.0: The Twilio SDK for sending SMS messages.
- Spring Data Redis: Used for integrating Redis into the Spring application for OTP storage.
- Docker Compose: Used for containerizing the application and managing multiple containers.
- Maven: The build automation tool used for managing project dependencies and building the application.
- JDK 17 or higher
- Maven
- Docker and Docker Compose
- Active Twilio account with a valid email address
Before running your project, ensure you have completed the following steps:
- Implement Email in Twilio:
- Ensure your Twilio account is configured with a valid email address for sending SMS messages and OTP Number .
- we will connect our project with twilio with yml file by
TWILIO_ACCOUNT_SID: your_twilio_account_sid TWILIO_AUTH_TOKEN: your_twilio_auth_token TWILIO_PHONE_NUMBER: your_twilio_phone_number
Before running your application, handle sensitive information using environment variables. Follow these steps:
-
Create a .env File:
- Create a file named
.envin the root directory of your project. - Add your sensitive information to this file in the format
KEY=VALUE.
Example
.envfile:TWILIO_ACCOUNT_SID=your_twilio_account_sid TWILIO_AUTH_TOKEN=your_twilio_auth_token TWILIO_PHONE_NUMBER=your_twilio_phone_number REDIS_PASSWORD=your_redis_password
- Create a file named
- Endpoint:
POST localhost:8080/api/api/v1/sms - Description: Endpoint to send a SMS message to a specific phone number.
- Request Body:
{ "PhoneNumber": "+201001111114", "Message": "the massage From Mostafa " }
- Endpoint:
POST localhost:8080/api/v1/otp - Description: Endpoint to generate OTP and send it via SMS to a specified phone number.
- Request Body:
{ "PhoneNumber": "+201001111114" }
-
Endpoint:
POST localhost:8080/api/v1/otp/verify -
Description: Endpoint to verify the OTP entered ( OtpNumber ) by the user for a specific phone number.
-
Request Body:
{ "PhoneNumber": "+201001111114", "OtpNumber": "185653" }Note: The OTP number is valid for 6 minutes only. This time can be changed from the code.
// Example line in TwilioOtpSender class
private static final int TIME_TO_LIVE = 6; // in minutes



