Sending SMS with Java with Twilio
Time to read: 2 minutes
Did you know you can code a Java app in under 5 minutes to send an SMS using the Twilio API? Probably closer to 2, once you've got the tools installed. You'll need:
- An installation of Java 8 or newer - I recommend SDKMAN! for installing and managing Java versions.
- A Java IDE - I like IntelliJ IDEA but if you have a favourite that's cool too.
- A Twilio account (if you don't have one yet, sign up for a free account here and receive $10 credit when you upgrade)
- A Twilio phone number that can send and receive SMS
The easiest way to call the Twilio API is with the Twilio Java Helper Library. In this I'll give examples using the Apache Maven build tool which will download and manage the dependency and packaging the project. You can get up to speed with Maven in 5 Minutes, and other tools like Gradle will work too, if that's what you prefer.
If you prefer watching to reading then check out my Twilio 2 Minute Demo video:
Creating a new project
I'll assume you're starting from scratch with an empty directory, so the first step is to create a new project.
A new Maven Project
Create a new Maven project in IntelliJ IDEA like this:
I like to use the IDE's built-in New Project wizard, but it's also possible to create the same from the command line by creating a folder for your source code in the standard directory layout with mkdir -p src/main/java
and creating a file called pom.xml
with this content:
Before getting to coding we need to tell Maven to use Java 8, and to download the Twilio Helper Library. Add the following to the pom.xml
as the last thing before </project>
:
Writing the code
Maven uses src/main/java
as the place to put your Java source code. Inside that, create a package called com.example
and a class in there called TwilioSendSms
. Your directory structure should be:
Start off the TwilioSendSms
class with a main
method:
Where it says // code will go in here
, you need to do two things:
- Authenticate the Twilio client
- Call the API to send an SMS
These will take one line of code each.
Authenticate the Twilio client
You'll need your ACCOUNT_SID
and AUTH_TOKEN
from your Twilio Console. To help keep these secret I'd always recommend reading these from environment variables rather than hard-coding them. I use the EnvFile plugin for IntelliJ IDEA, and there are alternatives for other IDEs or you can set them in your OS. Once they are set in the environment, add this code in the main()
function body:
Send your first SMS
At last - the code for sending a text message! Add this to the main
method, below the init
code you just added:
[this code (with imports) on GitHub]
The phone numbers you pass to the PhoneNumber
constructors need to be real phone numbers, in E.164 format. Once you've added them in, run the code and voilà!
🎉🎉🎉 Congratulations 🎉🎉🎉
Wrapping up
Now you know how to send an SMS using the Twilio API, the next thing might be receiving and responding to SMS from Java.
It's time to put your imagination to work. You can send daily reminders, weather forecasts, alerts for parking spaces, or anything else you can imagine.
Whatever you're building, I'd love to hear about it - get in touch
I can't wait to see what you build!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.