Twilio is launching a new Console. Some screenshots on this page may show the Legacy Console and therefore may no longer be accurate. We are working to update all screenshots to reflect the new Console experience. Learn more about the new Console.
In this guide, we'll show you how to use Programmable Voice to make outbound phone calls from your Java applications. It's pretty easy - all you'll need is the Twilio library for Java, a voice-capable Twilio phone number, your Twilio account credentials, and five minutes to have a boatload of fun at your keyboard. Let's get started!
(warning)
Warning
If you are sending SMS messages to the U.S. or Canada, before proceeding further please be aware of updated restrictions on the use of Toll-Free numbers for messaging, including TF numbers obtained by purchasing them. These restrictions do not apply to Voice or other uses outside of SMS messaging. Please click here for details.
In the Twilio console, search for and purchase an available phone number capable of making outbound calls. You'll use this phone number as the "From" phone number when you initiate an outbound call.
There are a few key parameters to drill into when making the outbound call.
"From" - the voice-enabled Twilio phone number you added to your account earlier
"To" - the person you'd like to call
"Twiml" - Instructions in the form
TwiML
that explains what should happen when the other party picks up the phone
"Url" - Optionally, instead of passing the
Twiml
parameter, you can provide a Url that returns
TwiML
Voice instructions.
What is TwiML?
TwiML is the Twilio Markup Language, which is just to say that it's an XML document with special tags defined by Twilio to help you build your SMS and voice applications. TwiML is easier shown than explained. Here's some TwiML you might use to respond to an incoming phone call:
_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Say>Thanks for calling!</Say>
_10
</Response>
And here's some TwiML you might use to respond to an incoming SMS message:
_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Message>We got your message, thank you!</Message>
_10
</Response>
Every TwiML document will have the root <Response> element and within that can contain one or more verbs. Verbs are actions you'd like Twilio to take, such as <Say> a greeting to a caller, or send an SMS <Message> in reply to an incoming message. For a full reference on everything you can do with TwiML, refer to our TwiML API Reference.
Make an outbound call with hosted TwiML
This uses the Url parameter
Java
_25
// Install the Java helper library from twilio.com/docs/java/install
_25
_25
import com.twilio.Twilio;
_25
import com.twilio.rest.api.v2010.account.Call;
_25
import com.twilio.type.PhoneNumber;
_25
_25
import java.net.URI;
_25
_25
public class Example {
_25
// Find your Account SID and Auth Token at twilio.com/console
_25
// and set the environment variables. See http://twil.io/secure
_25
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_25
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
Of course, the TwiML you use to make the outbound call doesn't need to be a static file like in this example. Server-side Java code that you control can dynamically render TwiML to use for the outbound call.
Where to next?
Great work! In a few lines of code, you've placed an outbound phone call from your Java code. If you're interested in learning more about building voice applications in Java, perhaps we could tempt you with a few tutorials? Tutorials walk through full sample applications that implement production Twilio use cases, like these: