The Twilio C#/.NET Helper Library

Deprecation notice: New functionality will only be added to the new library (C# 5.x). The old library (C# 4.x) will be officially supported until 5/31/2017. After that day, Twilio will stop providing bug fixes and Support might ask you to upgrade before debugging issues. The old library will remain available indefinitely on GitHub.

The Twilio C#/.NET SDK makes it easy to interact with the Twilio API from your .NET application. The most recent version of the library can be found on NuGet. The Twilio C# SDK supports .NET applications written in C#, VB.Net, and F# that utilize the .NET Framework version 3.5 or higher or any .NET runtime supporting .NET Standard v1.4. If you are interested in migrating to the newer 5.x version of the Twilio C# SDK from the 4.x version, check out this guide.

Installing with NuGet

With Visual Studio IDE

From within Visual Studio, you can use the NuGet GUI to search for and install the Twilio NuGet package. Or, as a shortcut, simply type the following command into the Package Manager Console:

Install-Package Twilio

With .NET Core command line tools

 If you are building with the .NET Core command line tools, then you can run the following command from within your project directory:

dotnet add package Twilio

Testing your installation

Try sending yourself an SMS message, like this:

Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";   

            TwilioClient.Init(accountSid, authToken);

            var message = MessageResource.Create(
                to: new PhoneNumber("+15558675309"),
                from: new PhoneNumber("+15017250604"),
                body: "Hello from C#");

            Console.WriteLine(message.Sid);
            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }
    }
}
Send an SMS message to test your helper library installation.
C#/.NET Helper Library Example

Send an SMS message to test your helper library installation.

We also have an example in VB.NET and an example in F# if you use one of those languages.

If you are using the .NET Framework version 4.5.1 or higher or .NET Core 1.0 or higher, you can also make API calls asynchronously:

Loading Code Samples...
Language
using System;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SendSms().Wait();
            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }

        static async Task SendSms()
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";   

            TwilioClient.Init(accountSid, authToken);

            var message = await MessageResource.CreateAsync(
                to: new PhoneNumber("+15558675309"),
                from: new PhoneNumber("+15017250604"),
                body: "Hello from C#");

            Console.WriteLine(message.Sid);
        }
    }
}
Send an SMS message asynchronously to test your helper library installation.
C#/.NET Helper Library Async Example

Send an SMS message asynchronously to test your helper library installation.

Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);
    }
}
Authenticate C# Twilio client
Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Exceptions;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";

            TwilioClient.Init(accountSid, authToken);

            try
            {
                var message = MessageResource.Create(
                    to: new PhoneNumber("+15558675309"),
                    from: new PhoneNumber("+15017250604"),
                    body: "Hello from C#");

                Console.WriteLine(message.Sid);
            }
            catch (ApiException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine($"Twilio Error {e.Code} - {e.MoreInfo}");
            }


            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }
    }
}
Handle exceptions in C# helper library
Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var to = new PhoneNumber("+14155551212");
        var from = new PhoneNumber("+15017250604");
        var call = CallResource.Create(to, from,
            url: new Uri("http://demo.twilio.com/docs/voice.xml"));

        Console.WriteLine(call.Sid);
    }
}
Create a new record in C#
Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var call = CallResource.Fetch("CA42ed11f93dc08b952027ffbc406d0868");

        Console.WriteLine(call.To);
    }
}
Get existing record in C#
Loading Code Samples...
Language
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var calls = CallResource.Read();

        foreach (var call in calls)
        {
            Console.WriteLine(call.Direction);
        }
    }
}
Iterate through records with C#

DateTime values

All DateTime values in the C# helper library are in your local timezone (Kind=Local). Learn more details here on how to convert this to UTC or set UTC as your default.

More documentation

Once you're up and running with the C# SDK, you'll find code samples using the latest version in our REST API docs and in the documentation for every Twilio product. You can also find auto-generated library docs for the latest SDK here.

Accessing the 4.x version of the SDK

The most recent version of the C# SDK is not API compatible with the previous 4.x version of the C# SDK you may have used in previous Twilio applications. The older version will continue to work, and you will continue to find sample code for this version throughout our documentation. Should you need to install this version with NuGet, you can do so with the following command:

Install-Package Twilio -Version 4.7.2

Note that the 4.x version of the SDK is not compatible with .NET Core.

Getting help

We'd love to hear your feedback on the C# SDK, and help you past any issues you may encounter. Feel free to drop us a line, and we'll make sure to get you sorted!

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";   

            TwilioClient.Init(accountSid, authToken);

            var message = MessageResource.Create(
                to: new PhoneNumber("+15558675309"),
                from: new PhoneNumber("+15017250604"),
                body: "Hello from C#");

            Console.WriteLine(message.Sid);
            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }
    }
}
using System;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SendSms().Wait();
            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }

        static async Task SendSms()
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";   

            TwilioClient.Init(accountSid, authToken);

            var message = await MessageResource.CreateAsync(
                to: new PhoneNumber("+15558675309"),
                from: new PhoneNumber("+15017250604"),
                body: "Hello from C#");

            Console.WriteLine(message.Sid);
        }
    }
}
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);
    }
}
using System;
using Twilio;
using Twilio.Exceptions;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your Account SID from twilio.com/console
            var accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            // Your Auth Token from twilio.com/console
            var authToken = "auth_token";

            TwilioClient.Init(accountSid, authToken);

            try
            {
                var message = MessageResource.Create(
                    to: new PhoneNumber("+15558675309"),
                    from: new PhoneNumber("+15017250604"),
                    body: "Hello from C#");

                Console.WriteLine(message.Sid);
            }
            catch (ApiException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine($"Twilio Error {e.Code} - {e.MoreInfo}");
            }


            Console.Write("Press any key to continue.");
            Console.ReadKey();
        }
    }
}
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var to = new PhoneNumber("+14155551212");
        var from = new PhoneNumber("+15017250604");
        var call = CallResource.Create(to, from,
            url: new Uri("http://demo.twilio.com/docs/voice.xml"));

        Console.WriteLine(call.Sid);
    }
}
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var call = CallResource.Fetch("CA42ed11f93dc08b952027ffbc406d0868");

        Console.WriteLine(call.To);
    }
}
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Example
{
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var calls = CallResource.Read();

        foreach (var call in calls)
        {
            Console.WriteLine(call.Direction);
        }
    }
}