Send SMS with Twilio using Spring Boot

Hello Guys, I’m Senura Vihan Jayadeva. Today I’m going to show you how to send SMS with Twilio using Spring Boot.

What is Twilio?

Twilio is an American cloud communications platform as a service company based in San Francisco, California. Twilio allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.

Step 1

First of all, you have to create a Twilio account. For testing purposes, you can use a free account. But there you will be only able to send SMS for a specific phone number which you have given.

Go to https://www.twilio.com and click on the Sign up button. Then you will navigate to a page like this.

After submitting your details you will receive a verification email.

Then click on the Confirm Your Email link. Then you will navigate to a page like below. Here we have to provide a valid mobile number. Actually, for the free account, SMS will be sent only to this given number.

Then click on the Verify button. Then you have to fill the following fields as your requirement.

Then we will navigate the Twilio dashboard.

To use SMS, you will need a phone number from Twilio. On your trial account, you can get one free USA or Canada phone number. Now let’s click on the Get a trial phone number button.

Then you will able to see a popup box like below. Here you are getting a phone number. Then click on the Choose this Number.

If everything okay. In the dashboard, we will able to see Account SID, Auth Token, and Phone Number under the Project Info. We will keep these details for future use.

Step 2

Next, we have to initialize a spring boot application. If you don’t have an idea about spring boot follow the link below.

Make sure to add spring web, spring validation, and Twilio dependencies.

After adding those specific dependencies your pom.xml file should be like below.

Step 3

Next, create a class called TwilioConfiguration inside the com.twilio.demo package. Then implement the following code.

Then create another class called TwilioInitializer. Then implement the code below.

The next thing we have to do is create a modal for Sms. For that, you have to class called SmsRequest.

After implementing the SmsRequest class then we have to create an interface for define a method to send SMS. There we are going to pass SmsRequest class as a parameter.

Then inside the same package create a class called TwilioSmsSender. Then implement the following code.

Next what we have to do is create a service class called SmsService. Inside the SmsService we are going to implement the logic to send SMS.

Finally, we have to implement the controller class for processing incoming REST API requests. Create a class called SmsController and add the below code.

Step 4

Right, Now we are going to configure the application.properties file and there you have to provide the Account SID, Auth Token, and Phone Number.

You can get the above details from the Twilio dashboard.

Step 5

Now as the final step we should test the APIs using software like Postman.

Before tests, the APIs make sure to run the Spring Boot application first.

Github Link: https://github.com/senuravihanjayadeva/spring-sms-demo

This is the end of this tutorial. Hope you learned something. Thank you !.

Software Engineering undergraduate of Sri Lanka Institute of Information Technology | Physical Science Undergraduate of University of Sri Jayewardenepura

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store