In this guide we'll show you how to manipulate live phone calls using C#. We'll also cover how to retrieve information about in progress and completed calls from your Twilio account.
The code snippets in this guide are written using modern C# language features and require the .NET Framework version 4.5 or higher. They also make use of the Twilio C# SDK.
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.
1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;78class Program {9public static async Task Main(string[] args) {10// Find your Account SID and Auth Token at twilio.com/console11// and set the environment variables. See http://twil.io/secure12string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");13string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");1415TwilioClient.Init(accountSid, authToken);1617var call = await CallResource.UpdateAsync(18twiml: new Twilio.Types.Twiml("<Response><Say>Ahoy there</Say></Response>"),19pathSid: "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");2021Console.WriteLine(call.Sid);22}23}
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"annotation": null,4"answered_by": null,5"api_version": "2010-04-01",6"caller_name": null,7"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",8"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",9"direction": "inbound",10"duration": "15",11"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",12"forwarded_from": "+141586753093",13"from": "+14158675308",14"from_formatted": "(415) 867-5308",15"group_sid": null,16"parent_call_sid": null,17"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"price": "-0.03000",19"price_unit": "USD",20"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",22"status": "completed",23"subresource_uris": {24"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",25"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",26"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",27"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",28"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",29"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",30"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",31"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",32"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"33},34"to": "+14158675309",35"to_formatted": "(415) 867-5309",36"trunk_sid": null,37"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",38"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.
1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;78class Program {9public static async Task Main(string[] args) {10// Find your Account SID and Auth Token at twilio.com/console11// and set the environment variables. See http://twil.io/secure12string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");13string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");1415TwilioClient.Init(accountSid, authToken);1617var call = await CallResource.UpdateAsync(18method: Twilio.Http.HttpMethod.Post,19url: new Uri("http://demo.twilio.com/docs/voice.xml"),20pathSid: "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");2122Console.WriteLine(call.Sid);23}24}
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"annotation": null,4"answered_by": null,5"api_version": "2010-04-01",6"caller_name": null,7"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",8"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",9"direction": "inbound",10"duration": "15",11"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",12"forwarded_from": "+141586753093",13"from": "+14158675308",14"from_formatted": "(415) 867-5308",15"group_sid": null,16"parent_call_sid": null,17"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"price": "-0.03000",19"price_unit": "USD",20"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",22"status": "completed",23"subresource_uris": {24"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",25"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",26"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",27"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",28"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",29"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",30"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",31"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",32"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"33},34"to": "+14158675309",35"to_formatted": "(415) 867-5309",36"trunk_sid": null,37"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",38"queue_time": "1000"39}
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.
1// Install the C# / .NET helper library from twilio.com/docs/csharp/install23using System;4using Twilio;5using Twilio.Rest.Api.V2010.Account;6using System.Threading.Tasks;78class Program {9public static async Task Main(string[] args) {10// Find your Account SID and Auth Token at twilio.com/console11// and set the environment variables. See http://twil.io/secure12string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");13string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");1415TwilioClient.Init(accountSid, authToken);1617var call = await CallResource.UpdateAsync(18status: CallResource.UpdateStatusEnum.Completed,19pathSid: "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");2021Console.WriteLine(call.To);22}23}
1{2"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",3"annotation": null,4"answered_by": null,5"api_version": "2010-04-01",6"caller_name": null,7"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",8"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",9"direction": "inbound",10"duration": "15",11"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",12"forwarded_from": "+141586753093",13"from": "+14158675308",14"from_formatted": "(415) 867-5308",15"group_sid": null,16"parent_call_sid": null,17"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",18"price": "-0.03000",19"price_unit": "USD",20"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",21"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",22"status": "completed",23"subresource_uris": {24"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",25"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",26"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",27"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",28"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",29"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",30"transcriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Transcriptions.json",31"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",32"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"33},34"to": "+14158675309",35"to_formatted": "(415) 867-5309",36"trunk_sid": null,37"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",38"queue_time": "1000"39}
Check out Modifying Live Calls in the reference docs for more details.
Check out our tutorials to see full implementations of Twilio Voice in C# with ASP.NET.