Build the future of communications.
Start building for free

Protect Your Identity with Proxy Servers, Twilio, and C#


We share a lot in common with Bruce Wayne. By day, Bruce Wayne is a dashing billionaire running his family’s company. At night, he dons the cape and cowl to become Batman, fighting crime and driving fear into the hearts of his enemies. Think about it… Wearing the cape and cowl protects Bruce Wayne’s identity. You are probably thinking – how does this relate to me?

When you connect a device to the internet, it shares identifiable information with each of sites or services it connects to. However, when your device connects to a proxy server before reaching the internet, the proxy server protects your identity and exchanges completely different information about your connection, making you untraceable. It protects your identity just as Batman’s cape and cowl does for Bruce Wayne.

As a developer, I wonder – what happens when my code needs to run through a proxy server first before connecting to the internet? A developer asked the same question on StackOverflow. They wanted to place phone calls using Twilio’s API behind a proxy server with C#. We are going to build this in this blog post.

Challenge Ingredients

To complete this challenge, you’ll want to install:

Once you create your Twilio account and purchase a Twilio phone number, save the account SID, auth token, and phone number. We are going to need this later.

After you install the dependencies, open a terminal window. Use the command git clone to clone this C# application, which makes a phone call using your Twilio account and number. I am saving it to C:\Code\Proxy on my machine, but you can save it wherever you would like.

Make Phone Calls with C# and Twilio

Open the project folder in VS Code. This project has two code files – Helper.cs and Program.cs. We will update the variables in Helper.cs with your Twilio account information. Replace any values starting with “YOUR_” with your specific account information. Remember to use a phone number that can receive phone calls when you update the DESTINATION_NUMBER.

public static readonly string ACCOUNT_SID = "YOUR_ACCOUNT_SID_HERE";
public static readonly string AUTH_TOKEN = "YOUR_AUTH_TOKEN_HERE";
public static readonly string TWILIO_NUMBER = "YOUR_TWILIO_NUMBER_HERE";

Next open the Program.cs file. There are some important things to note here. First, the Twilio NuGet package has already been imported into our project. This package allows us to use Twilio’s API. To use it, we need to put these using statements at the top of our Program.cs file:

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

Second, we need to authenticate. This is a one-line method call that goes in our Main method: TwilioClient.Init(Helper.ACCOUNT_SID,Helper.AUTH_TOKEN);. How easy was that?

Third is making the phone call, which is also a single method call:

var callResult = CallResource.Create(
        to: new PhoneNumber(Helper.DESTINATION_NUMBER),
        from: new PhoneNumber(Helper.TWILIO_NUMBER),
        url: new Uri(""));

This method takes three arguments: the phone number to call, the phone number to place the call from, and a URL to the TwiML that tells Twilio what to do when the call connects.

To test this project, open the integrated terminal and type dotnet run followed by Enter. If this runs successfully, Twilio should be calling you right about now. Once you get a phone call, jump to the next section which puts the proxy in place.

Using Fiddler as a Proxy Server

Using a proxy server means we need to set it up somewhere, right? We are going to run a proxy server on our local machines thanks to Fiddler. Fiddler can intercept HTTP traffic sent to and from the machine it is running on. In other words it intercepts your traffic just like a proxy server would. If this is your first time using Fiddler, start it up, configure it to decrypt HTTPS requests, and browse to a few sites. You should see your machine’s internet traffic (unless you’re using Firefox). If you don’t see it, check out some troubleshooting tips. Time to write some code.

Creating a WebProxy in C#

To use a proxy server, let’s import the System.Net and System.Net.Http namespaces to our code file. This lets us use the HTTP classes needed to setup the proxy. Do this by simply pasting these two using statements at the top of the Program.cs file:

using System.Net;
using System.Net.Http;

Next, we will create a method that returns an HttpClient by pasting static HttpClient GetProxyClient(){ } statement underneath the Main method. This client will route all requests and responses through the proxy server. In our GetProxyClient method, we are going to create a WebProxy object, connect it to an HttpClientHandler object, and then return an HttpClient object that uses the client handler. Paste the code below into your method:

var proxy = new WebProxy("",8888)
    UseDefaultCredentials = false,
    Credentials = CredentialCache.DefaultCredentials

Now we can create our HttpClientHandler object and connect it to the proxy with the following:

var handler = new HttpClientHandler
    Proxy = proxy,
    PreAuthenticate = true,
    UseDefaultCredentials = false

Finally, returning our HttpClient object with the handler means pasting this statement at the bottom of the method: return new HttpClient(handler);. With this, our polished method looks like

static HttpClient GetProxyClient()
    var proxy = new WebProxy("",8888)
        UseDefaultCredentials = false,
        Credentials = CredentialCache.DefaultCredentials
    var handler = new HttpClientHandler
        Proxy = proxy,
        PreAuthenticate = true,
        UseDefaultCredentials = false
    return new HttpClient(handler);    

Now we have an HttpClient that references our web proxy. Great! Except we still need to connect to Twilio.

Calling Twilio with C# and a WebProxy

We are going to replace our authentication code with a few more lines of code. Jump back to the TwilioClient.Init(...) statement in the Main method. Replace that line with the below code:

var proxyClient = GetProxyClient();
var twilioClient = new Twilio.Clients.TwilioRestClient(
        httpClient: new Twilio.Http.SystemNetHttpClient(proxyClient));

What do these three lines do? First, we call GetProxyClient to get an HTTP client that uses our proxy server. Then we create a TwilioRestClient that takes our account SID, auth token, and our new proxy client. Finally, we connect our REST client to a static TwilioClient object. Once we set this, all web traffic in our code will flow through the proxy including our calls to Twilio.

To test this , let’s open Fiddler to have it monitor your traffic and run the application again by typing dotnet run in the integrated terminal. If you can see this in Fiddler, then that means we’ve succeeded!


We now have a basic console application that  makes phone calls with Twilio’s API and masks our identity thanks to our proxy server.. Check out the completed code on Github. Obviously this is just the start of my journey to becoming a superhero but hopefully you feel superpowered to try some more things out with your proxy server. If you run into issues, drop a note in the comments, on Twitter, or email. I can’t wait to see what you build!

Sign up and start building
Not ready yet? Talk to an expert.