Rate this page:

Custom HTTP Clients for the Twilio Go Helper Library

If you are working with the Twilio Go 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 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 when invoking Twilio's REST API.

In Go, the Twilio helper library uses the native net/http package under the hood to make the HTTP requests. The Twilio Helper Library allows you to provide your own Client for making API requests.

The following example shows a typical request without a custom Client.

twilioClient := twilio.NewRestClient()

params := &twilioApi.CreateMessageParams{}
params.SetBody("Hey there!")

resp, err := twilioClient.Api.CreateMessage(params)

Out of the box, the helper library creates a default Client for you, using the Twilio credentials from your environment variables or that you pass in directly. However, there’s nothing stopping you from creating your own client and using that.

Once you have your own Client, you can pass it to any Twilio REST API resource action you want.

Create and use your custom Client

When you take a closer look at the input parameters for twilio.RestClient, you see that the Client parameter is actually of type client.BaseClient.

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

Now that you understand how all the components fit together, you can create your own Client 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.

Here’s an example of sending an SMS message with a custom client:


        Custom Go HTTP Client

        In this example, you use environment 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 either exported manually by yourself in the terminal, or located in a file such as .env, like so:


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

        What else can this technique be used for?

        Now that you know how to inject your own 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 do so by overriding the SendRequest method, and adding any desired pre-processing to your requests and responses, like so:


              Override SendRequest for custom pre-processing or header adjustments

              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!