Rate this page:

Custom HTTP Clients for the Twilio Python Helper Library

If you are working with the Twilio Python Helper Library, and you need to be able to modify the HTTP requests that the library makes to the Twilio servers, you’re in the right place. The most common reason for altering the HTTP request is to connect and authenticate with an enterprise’s proxy server. We’ll provide sample code that you can drop right into your app to handle this use case.

Connect and authenticate with a proxy server

To connect and provide credentials to a proxy server that may be between your app and Twilio, you need a way to modify the HTTP requests that the Twilio helper library makes on your behalf to invoke the Twilio REST API.

On Python, the Twilio helper library uses the requests library under the hood to make the HTTP requests. The Twilio Helper Library allows you to provide your own httpClient for making API requests.

Custom HTTP Client

So the question becomes how do we apply this to a typical Twilio REST API example?

client = Client(account_sid, auth_token)

message = client.messages \
        body="Hey there!",

Where does an http_client get created and used?

Out of the box, the helper library creates a default HttpClient for you, using the Twilio credentials you pass to the init method. However, there’s nothing stopping you from creating your own and using that.

Once you have your own HttpClient, you can pass it to any Twilio REST API resource action you want. Here’s an example of sending an SMS message with a custom client:


        How to pass a TwilioRestClient

        Create your custom TwilioRestClient

        When you take a closer look at the constructor for HttpClient, you see that the http_client parameter is actually of type twilio.http.HttpClient.

        HttpClient is an abstraction that allows plugging in any implementation of an HTTP client you want (or even creating a mocking layer for unit testing).

        However, within the helper library, there is an implementation of twilio.http.HttpClient called TwilioHttpClient. This class wraps the twilio.http.HttpClient and provides it to the Twilio helper library to make the necessary HTTP requests.

        Call Twilio through the proxy server

        Now that we understand how all the components fit together, we can create our own HttpClient that can connect through a proxy server. To make this reusable, here’s a class that you can use to create this HttpClient whenever you need one.


              Custom HTTP Client

              In this example, we are using some environmental variables loaded at the program startup to retrieve various configuration settings:

              • Your Twilio Account Sid and Auth Token (found here, in the Twilio console)

              • A proxy address in the form of

              These settings are located in a file like .env like so:

              AUTH_TOKEN= your_auth_token

              Here’s the full console program that sends a text message and shows how it all can work together. It loads the .env file.


                    Call the Twilio REST API with a custom TwilioRestClient in Python

                    What else can this technique be used for?

                    Now that you know how to inject your own http_client into the Twilio API request pipeline, you could use this technique to add custom HTTP headers and authorization to the requests (perhaps as required by an upstream proxy server).

                    You could also implement your own http_client to mock the Twilio API responses so your unit and integration tests can run quickly without needing to make a connection to Twilio. In fact, there’s already an example online showing how to do exactly that with C#.

                    We can’t wait to see what you build!

                    Rate this page:

                    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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.


                          Thank you for your feedback!

                          Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                          Sending your feedback...
                          🎉 Thank you for your feedback!
                          Something went wrong. Please try again.

                          Thanks for your feedback!