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!
Now we're ready to make an outbound call using the Twilio Java library.
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
Twimlparameter, you can provide a Url that returns TwiML Voice instructions.
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Thanks for calling!</Say> </Response>
And here's some TwiML you might use to respond to an incoming SMS message:
<?xml version="1.0" encoding="UTF-8"?> <Response> <Message>We got your message, thank you!</Message> </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.
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.
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:
- Automated phone surveys (Servlets, Spark, or Spring Framework samples)
- Anonymous phone calls (Servlets)
- Call tracking for lead attribution (Servlets)