Introduction
WhatsApp Messenger is the most used messaging platform in the world. Also used tools for businesses to connect with their users. Integrating WhatsApp into your project can enable you to send and receive messages via WhatsApp and is useful for customer services and businesses.
Here we use Twilio for WhatsApp API integration. Twillo is a platform that provides APIs for SMS, call, and WhatsApp messaging. Here we integrated WhatsApp sandbox and sent messages to WhatsApp.
Prerequisites
- JDK 11 or later
- Twilio account
- Maven or Gradle
Set up a Twilio account
- Create an account on Twilio.
- Set WhatsApp sandbox from Twilio console and active Whatsapp sandbox.
- Get Account SID, Auth token, and Twilio phone number from the Twilio console
Set up Springboot project
Step 1 : Add Twilio maven dependencies
Add the following dependencies to your application.
com.twilio.sdk
twilio
9.0.0
Step 2 : Configure Application Properties
Add the Twilio credentials into the application.properties :
twilio.accountSid=YOUR_ACCOUNT_SID
twilio.authToken=YOUR_AUTH_TOKEN
twilio.phoneNumber=YOUR_PHONE_NUMBER
Step 3 : Implement Twilio Services class
This class handles the sending messaging using Twilio API.
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class TwilioService {
@Value("${twilio.account.sid}")
private String accountSid;
@Value("${twilio.auth.token}")
private String authToken;
@Value("${twilio.whatsapp.number}")
private String fromWhatsAppNumber;
public TwilioService() {
// Initialize Twilio with account credentials
Twilio.init(accountSid, authToken);
}
public String sendWhatsAppMessage(String to, String messageBody) {
// Send a message via Twilio's API
Message message = Message.creator(
new PhoneNumber("whatsapp:" + to), // Recipient's WhatsApp number
new PhoneNumber(fromWhatsAppNumber), // Twilio WhatsApp number
messageBody) // Message body
.create();
return message.getSid(); // Return message SID to track status
}
}
Step 4 : Define controller.
Define the endpoint in the controller that will handle the sending message on WhatsApp.
import com.example.twilio.service.TwilioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class TwilioController {
@Autowired
private TwilioService twilioService;
@PostMapping("/send")
public String sendSms(@RequestParam String to, @RequestParam String message) {
return twilioService.sendWhatsAppMessage(to, message);
}
}
Step 5 : Test the API.
Run your spring-boot application and call the API by postmen or other curl. Here the message id you get from the twilio as a response.

Conclusion
Twilio WhatsApp Sendbox allows you to send WhatsApp messages with little setup for testing. Including WhatsApp in your application increases customer integration and communication. You can utilize a variety of other third-party APIs. You may include Twilio into your Java project with ease if you follow these rules. If you have integrated the WhatsApp API for actual use or production, please leave a comment below. I have incorporated the sandbox here for testing.