Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Modify Calls In Progress With Java


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.

We'll use Twilio's Java SDK(link takes you to an external page) in these examples to interact with the Twilio REST API's Calls endpoint.


Modifying Twilio phone calls in progress with Java

modifying-twilio-phone-calls-in-progress-with-java page anchor

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.

Update an in progress Call with new TwiML instructions

update-an-in-progress-call-with-new-twiml-instructions page anchor
Java

_22
// Install the Java helper library from twilio.com/docs/java/install
_22
_22
import com.twilio.type.Twiml;
_22
import com.twilio.Twilio;
_22
import com.twilio.rest.api.v2010.account.Call;
_22
_22
public 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
_22
public static void main(String[] args) {
_22
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_22
Call call = Call.updater("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_22
.setTwiml(new com.twilio.type.Twiml(
_22
"<Response><Say>Ahoy there</Say></Response>"))
_22
.update();
_22
_22
System.out.println(call.getSid());
_22
}
_22
}

Output

_39
{
_39
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"annotation": null,
_39
"answered_by": null,
_39
"api_version": "2010-04-01",
_39
"caller_name": null,
_39
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_39
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"direction": "inbound",
_39
"duration": "15",
_39
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"forwarded_from": "+141586753093",
_39
"from": "+14158675308",
_39
"from_formatted": "(415) 867-5308",
_39
"group_sid": null,
_39
"parent_call_sid": null,
_39
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"price": "-0.03000",
_39
"price_unit": "USD",
_39
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_39
"status": "completed",
_39
"subresource_uris": {
_39
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
_39
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
_39
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
_39
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
_39
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
_39
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
_39
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
_39
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
_39
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
_39
},
_39
"to": "+14158675309",
_39
"to_formatted": "(415) 867-5309",
_39
"trunk_sid": null,
_39
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
_39
"queue_time": "1000"
_39
}

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.

Redirect an in progress Call to a new URL

redirect-an-in-progress-call-to-a-new-url page anchor
Java

_23
// Install the Java helper library from twilio.com/docs/java/install
_23
_23
import java.net.URI;
_23
import com.twilio.Twilio;
_23
import com.twilio.rest.api.v2010.account.Call;
_23
import com.twilio.http.HttpMethod;
_23
_23
public class Example {
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_23
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_23
_23
public static void main(String[] args) {
_23
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_23
Call call = Call.updater("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_23
.setMethod(HttpMethod.POST)
_23
.setUrl(URI.create("http://demo.twilio.com/docs/voice.xml"))
_23
.update();
_23
_23
System.out.println(call.getSid());
_23
}
_23
}

Output

_39
{
_39
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"annotation": null,
_39
"answered_by": null,
_39
"api_version": "2010-04-01",
_39
"caller_name": null,
_39
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_39
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"direction": "inbound",
_39
"duration": "15",
_39
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"forwarded_from": "+141586753093",
_39
"from": "+14158675308",
_39
"from_formatted": "(415) 867-5308",
_39
"group_sid": null,
_39
"parent_call_sid": null,
_39
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"price": "-0.03000",
_39
"price_unit": "USD",
_39
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_39
"status": "completed",
_39
"subresource_uris": {
_39
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
_39
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
_39
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
_39
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
_39
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
_39
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
_39
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
_39
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
_39
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
_39
},
_39
"to": "+14158675309",
_39
"to_formatted": "(415) 867-5309",
_39
"trunk_sid": null,
_39
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
_39
"queue_time": "1000"
_39
}

Hanging up a call in progress

hanging-up-a-call-in-progress page anchor

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.

Terminate a Running Call

terminate-a-running-call page anchor
Java

_20
// Install the Java helper library from twilio.com/docs/java/install
_20
_20
import com.twilio.Twilio;
_20
import com.twilio.rest.api.v2010.account.Call;
_20
_20
public class Example {
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment variables. See http://twil.io/secure
_20
public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
_20
public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
_20
_20
public static void main(String[] args) {
_20
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
_20
Call call = Call.updater("CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_20
.setStatus(Call.UpdateStatus.COMPLETED)
_20
.update();
_20
_20
System.out.println(call.getTo());
_20
}
_20
}

Output

_39
{
_39
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"annotation": null,
_39
"answered_by": null,
_39
"api_version": "2010-04-01",
_39
"caller_name": null,
_39
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_39
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"direction": "inbound",
_39
"duration": "15",
_39
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_39
"forwarded_from": "+141586753093",
_39
"from": "+14158675308",
_39
"from_formatted": "(415) 867-5308",
_39
"group_sid": null,
_39
"parent_call_sid": null,
_39
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"price": "-0.03000",
_39
"price_unit": "USD",
_39
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_39
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_39
"status": "completed",
_39
"subresource_uris": {
_39
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
_39
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
_39
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
_39
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
_39
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
_39
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
_39
"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",
_39
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
_39
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
_39
},
_39
"to": "+14158675309",
_39
"to_formatted": "(415) 867-5309",
_39
"trunk_sid": null,
_39
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
_39
"queue_time": "1000"
_39
}

Check out Modifying Live Calls in the reference docs for more details.


That's how you can modify a call in progress using Java. Check out our tutorials to see full implementations of Twilio Voice in Java.


Rate this page: