In this guide we'll show you how to manipulate live phone calls using Java. We'll also cover how to retrieve information about in progress and completed calls from your Twilio account.
The simplest way to control the flow of a Twilio phone call is with TwiML itself.
You can use the "action" parameters of verbs like <Gather> and <Record> to tell Twilio to get new instructions from your applications during a call. You can also use the <Redirect> verb to explicitly tell Twilio to fetch new TwiML.
But sometimes you need to change a live phone call outside of Twilio's normal request-response cycle. For those cases you can update the Call to tell Twilio to immediately change the TwiML it's using in a phone call.
_22// Install the Java helper library from twilio.com/docs/java/install
_22import com.twilio.Twilio;
_22import com.twilio.rest.api.v2010.account.Call;
_22import com.twilio.type.Twiml;
_22public class Example {
_22 // Find your Account SID and Auth Token at twilio.com/console
_22 // and set the environment variables. See http://twil.io/secure
_22 public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_22 public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_22 public static void main(String[] args) {
_22 Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_22 Call call = Call.updater("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
_22 new com.twilio.type.Twiml("<Response><Say>Ahoy there</Say></Response>"))
_22 System.out.println(call.getTo());
_37 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "api_version": "2010-04-01",
_37 "date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37 "date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "direction": "inbound",
_37 "end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "forwarded_from": "+141586753093",
_37 "from": "+14158675308",
_37 "from_formatted": "(415) 867-5308",
_37 "parent_call_sid": null,
_37 "phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37 "status": "completed",
_37 "subresource_uris": {
_37 "notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37 "recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37 "payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37 "events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37 "siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37 "streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37 "user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37 "user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37 "to": "+14158675309",
_37 "to_formatted": "(415) 867-5309",
_37 "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
In order to update the call you will need to use the CallSid. Twilio returns this when you initiate an outgoing call, and also includes the CallSid in its request to your application for an incoming call's initial TwiML.
As an alternative to updating your call with TwiML directly, you may also redirect a Call to a new URL that responds with your requested TwiML.
_24// Install the Java helper library from twilio.com/docs/java/install
_24import com.twilio.Twilio;
_24import com.twilio.http.HttpMethod;
_24import com.twilio.rest.api.v2010.account.Call;
_24public class Example {
_24 // Find your Account SID and Auth Token at twilio.com/console
_24 // and set the environment variables. See http://twil.io/secure
_24 public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_24 public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_24 public static void main(String[] args) {
_24 Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_24 Call call = Call.updater("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
_24 .setMethod(HttpMethod.POST)
_24 .setUrl(URI.create("http://demo.twilio.com/docs/voice.xml"))
_24 System.out.println(call.getTo());
_37 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "api_version": "2010-04-01",
_37 "date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37 "date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "direction": "inbound",
_37 "end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "forwarded_from": "+141586753093",
_37 "from": "+14158675308",
_37 "from_formatted": "(415) 867-5308",
_37 "parent_call_sid": null,
_37 "phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37 "status": "completed",
_37 "subresource_uris": {
_37 "notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37 "recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37 "payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37 "events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37 "siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37 "streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37 "user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37 "user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37 "to": "+14158675309",
_37 "to_formatted": "(415) 867-5309",
_37 "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
Twilio will end phone calls for you when it encounters a <Hangup> verb or when it runs out of TwiML to process. But you can also end phone calls whenever you like by passing a "completed" status to a CallSid in progress.
_19// Install the Java helper library from twilio.com/docs/java/install
_19import com.twilio.Twilio;
_19import com.twilio.rest.api.v2010.account.Call;
_19public class Example {
_19 // Find your Account SID and Auth Token at twilio.com/console
_19 // and set the environment variables. See http://twil.io/secure
_19 public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_19 public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_19 public static void main(String[] args) {
_19 Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_19 Call call = Call.updater("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
_19 .setStatus(Call.UpdateStatus.COMPLETED).update();
_19 System.out.println(call.getTo());
_37 "account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "api_version": "2010-04-01",
_37 "date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_37 "date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "direction": "inbound",
_37 "end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_37 "forwarded_from": "+141586753093",
_37 "from": "+14158675308",
_37 "from_formatted": "(415) 867-5308",
_37 "parent_call_sid": null,
_37 "phone_number_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "sid": "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_37 "start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_37 "status": "completed",
_37 "subresource_uris": {
_37 "notifications": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Notifications.json",
_37 "recordings": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Recordings.json",
_37 "payments": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Payments.json",
_37 "events": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Events.json",
_37 "siprec": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Siprec.json",
_37 "streams": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Streams.json",
_37 "user_defined_message_subscriptions": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessageSubscriptions.json",
_37 "user_defined_messages": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/UserDefinedMessages.json"
_37 "to": "+14158675309",
_37 "to_formatted": "(415) 867-5309",
_37 "uri": "/2010-04-01/Accounts/ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Calls/CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.json",
Check out Modifying Live Calls in the reference docs for more details.