The Twilio C# Helper Library

The twilio-csharp helper library lets you write C# code (or code in any .NET language) to make HTTP requests to the Twilio API.

This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-csharp project hosted on GitHub.

More Documentation

If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the .NET quickstarts.

The Rest API Documentation has examples written in C# for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the C# library.

You may also want to refer to the open source twilio-csharp specific documentation for more details about the functions and classes contained in the module.

Installation

Adding Twilio .NET Libraries to your .NET project

The best and easiest way to add the Twilio libraries to your .NET project is to use the NuGet package manager. NuGet is a Visual Studio extension that makes it easy to install and update third-party libraries and tools in Visual Studio.

NuGet is included in Visual Studio 2012 and later and available for download for Visual Studio 2010. You can find instructions for installing the NuGet extension on the NuGet.org website.

Once you have installed the NuGet extension, you can choose to install the Twilio libraries using either the Package Manager dialog, or using the Package Manager console.

Installing via the Package Manager Console

To install a Twilio library using the Package Manager console, first open the console, then Use the Install-Package command to install the different Twilio packages:

Install REST API wrapper:

PM> Install-Package Twilio

Install ASP.NET MVC helpers and REST API wrapper:

PM> Install-Package Twilio.Mvc

Installing via the Package Manager Dialog

To install a Twilio library using the Package Manager dialog, first open the dialog by right-clicking the References folder in your project and selecting the package manager option:

"Manage NuGet Packages" context menu

When the package manager dialog opens simply search the online catalog for ‘Twilio’. The screen shot below shows the results returned from the NuGet catalog:

Package Manager Dialog

Simply click the Install button next to the Twilio package you want to add to your project and watch as NuGet downloads the Twilio library package (and its dependencies) and adds the proper references to your project.

Twilio References

Testing your installation

Try sending yourself an SMS message, like this:

using System;
using Twilio;

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

      var twilio = new TwilioRestClient(accountSid, authToken);
      var message = twilio.SendMessage(
          "+12345678901", // From (Replace with your Twilio number)
          "+12345678901", // To (Replace with your phone number)
          "Hello from C#"
          );

      Console.WriteLine(message.Sid);
      Console.Write("Press any key to continue.");
      Console.ReadKey();
    }
  }
}

Using the Library

Try the .NET quickstarts and refer to the open source twilio-csharp specific documentation for details about the functions and classes contained in the library.

Exceptions

If the Twilio API returns a 400 or a 500 level HTTP response, objects returned by the twilio-csharp library will contain a non-null RestException object with information about the exception. 400-level errors are normal during API operation ("Invalid number", "Cannot deliver SMS to that number", for example) and should be checked and handled appropriately.

using System;
using Twilio;

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

      var twilio = new TwilioRestClient(accountSid, authToken);
      var message = twilio.SendMessage(
          "+12345678901", // From (Replace with your Twilio number)
          "+12345678901", // To (Replace with your phone number)
          "Hello from C#"
          );

      if (message.RestException != null)
      {
        var error = message.RestException.Message;
        Console.WriteLine(error);
        Console.Write("Press any key to continue.");
        Console.ReadKey();
      }
    }
  }
}

Handling Webhooks

Please refer to our guide on handling webhooks with ASP.NET MVC and C#.

Getting help

Still running into problems?

Installing the Next-Gen Version

The "next-gen" version of the Twilio library will markedly improve the developer experience. As we put the finishing touches on this new way of building helper libraries, we need to hear your feedback. Please let us know what you think.

To install the next-gen version of twilio-csharp use the Package Manager Console in Visual Studio as follows, calling Install-Package with the -Pre switch:

PM> Install-Package Twilio -Pre

Working with the helper library is a bit different than the current version. Here's an example of how to send an SMS:

using System;
using System.Threading.Tasks;
using Twilio;
using Twilio.Clients;
using Twilio.Creators.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()
    {
      var accountSid = "{{ account_sid }}"; // Your Account SID from twilio.com/console
      var authToken = "{{ auth_token }}";   // Your Auth Token from twilio.com/console

      TwilioClient.init(accountSid, authToken);
      var restClient = new TwilioRestClient(accountSid, authToken);

      var message = await
        new MessageCreator(
          accountSid,
          new PhoneNumber("+12345678901"),  // To number
          new PhoneNumber("+12345678901"),  // Twilio From number
          "Hello from C#"
        ).ExecuteAsync(restClient);

      Console.WriteLine(message.GetSid());
    }
  }
}

Visit the next-gen branch on GitHub for more information and to provide feedback.