You are currently viewing Sending SMS Messages with Twilio and Spring Boot

Sending SMS Messages with Twilio and Spring Boot


  • Java installed on your machine (version 8 or higher)
  • Spring Boot installed
  • Twilio account (sign up at

Step 1: Create a Spring Boot Application

If you haven’t already set up a Spring Boot project, you can do so using Spring Initializr ( or your preferred method.

Step 2: Add Twilio Dependency

Add the Twilio dependency to your pom.xml if you are using Maven:

    <version>8.23.0</version> <!-- Check for the latest version -->

Or if you are using Gradle, add to your build.gradle:

implementation 'com.twilio.sdk:twilio:8.23.0' // Check for the latest version

Step 3: Configure Twilio

You’ll need your Twilio Account SID and Auth Token to authenticate. These can be found on your Twilio dashboard after logging in.

Create a TwilioConfig class to hold your Twilio credentials:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.twilio.Twilio;

public class TwilioConfig {

    private String accountSid;

    private String authToken;

    public void twilioInit() {
        Twilio.init(accountSid, authToken);

Make sure to include these properties in your or application.yml:


Step 4: Create SMS Service

Create a service to handle sending SMS messages. Let’s call it SmsService.

import com.twilio.type.PhoneNumber;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

public class SmsService {

    private String twilioPhoneNumber;

    public void sendSms(String to, String messageBody) {
        try {
            Message message = Message.creator(
                    new PhoneNumber(to),
                    new PhoneNumber(twilioPhoneNumber),
            System.out.println("Message sent successfully: " + message.getSid());
        } catch (Exception e) {
            System.err.println("Error sending SMS: " + e.getMessage());

Make sure to include your Twilio phone number in the


Step 5: Create a Controller

Create a controller to expose an API endpoint for sending SMS messages.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class SmsController {

    private final SmsService smsService;

    public SmsController(SmsService smsService) {
        this.smsService = smsService;

    public String sendSms(@RequestBody SmsRequest smsRequest) {
        smsService.sendSms(smsRequest.getTo(), smsRequest.getMessage());
        return "SMS sent successfully";

Step 6: Create a Request Object

Create a simple request object to receive the SMS details.

public class SmsRequest {

    private String to;
    private String message;

    // Getters and Setters

Step 7: Run Your Application

Now you can run your Spring Boot application. Ensure it starts up without errors.

Step 8: Test the Endpoint

Using a tool like Postman or cURL, send a POST request to http://localhost:8080/send-sms with a JSON body like this:

    "to": "+1234567890",
    "message": "Hello from Twilio and Spring Boot!"

Replace +1234567890 with the phone number you want to send the SMS to. You should receive a response indicating success, and the SMS should be sent to the provided number.


You’ve now created a Spring Boot application that can send SMS messages using Twilio. This is a basic example, and in a real-world application, you would want to add error handling, validation, and possibly store/send more complex message formats. But this should give you a good starting point!

Leave a Reply