Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Programmable Voice Quickstart for Go


With just a few lines of code, your Go application can make and receive phone calls with Twilio Programmable Voice.

This Go quickstart will teach you how to do this using our REST API and the Twilio Go helper library(link takes you to an external page) to ease development.

In this quickstart, you will learn how to:

  1. Sign up for Twilio and get your first voice-enabled Twilio phone number
  2. Set up your development environment to make and receive phone calls
  3. Make an outbound phone call which plays an MP3
  4. Receive and respond to an inbound phone call which reads a message to the caller using Text to Speech

Sign up for Twilio and get a phone number

sign-up-for-twilio-and-get-a-phone-number page anchor
(information)

Info

If you already have a Twilio account and a voice-enabled Twilio phone number you're all set here! Log in(link takes you to an external page) then feel free to jump to the next step.

Before you can make a call from Go, you'll need to sign up for a Twilio account or log in(link takes you to an external page) to an account you already have.

The next thing you'll need is a voice-capable Twilio phone number(link takes you to an external page). If you don't currently own a Twilio phone number with voice call functionality, you'll need to purchase one. After navigating to the Buy a Number(link takes you to an external page) page, check the "Voice" box and click "Search."

Search for voice capable numbers.

You'll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.

Now that you have a Twilio account and a programmable phone number, you have the basic tools you need to make a phone call.

You could use Twilio's HTTP API to make your phone calls, but we'll make things even more simple by using the Twilio module for Go(link takes you to an external page). Let's install that now.


Install Go and the Twilio Helper Library

install-go-and-the-twilio-helper-library page anchor
(information)

Info

If you've gone through one of our other Go Quickstarts already and have Go and the Twilio Go helper library installed, you can skip this step and get to the rest of the tutorial.

Before you can follow the rest of this tutorial, you'll need to have Go and the Twilio Go module installed.

Install Go

install-go page anchor

You can check if you already have Go installed on your machine by opening up a terminal and running the following command:


_10
go version

You should see something like:


_10
$ go version
_10
go version go1.19 darwin/amd64

If you don't have Go installed, head over to go.dev and download the appropriate installer for your system(link takes you to an external page). Once you've installed Go, return to your terminal, and run the command above once again. If you don't see the installed Go version, you may need to relaunch your terminal.

Initialize your project and install the Twilio Go Helper Library

initialize-your-project-and-install-the-twilio-go-helper-library page anchor

Create a new Go project from your terminal using:


_10
go mod init twilio-example

Once your project has been initialized, navigate into the newly created twilio-example directory and install the Twilio Go helper library module.


_10
go get github.com/twilio/twilio-go

This will install the twilio-go module so that your Go code in the current directory can make use of it.


Make an outgoing phone call with Go

make-an-outgoing-phone-call-with-go page anchor

Let's put that Twilio Go library to good use.

With a single API request, we can make an outbound call from the Twilio phone number we just purchased. Open a new file called makecall.go and type or paste in the following code:

Make an outbound call

make-an-outbound-call page anchor
Go

_33
// Download the helper library from https://www.twilio.com/docs/go/install
_33
package main
_33
_33
import (
_33
"fmt"
_33
"github.com/twilio/twilio-go"
_33
api "github.com/twilio/twilio-go/rest/api/v2010"
_33
"os"
_33
)
_33
_33
func main() {
_33
// Find your Account SID and Auth Token at twilio.com/console
_33
// and set the environment variables. See http://twil.io/secure
_33
// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
_33
client := twilio.NewRestClient()
_33
_33
params := &api.CreateCallParams{}
_33
params.SetUrl("http://demo.twilio.com/docs/voice.xml")
_33
params.SetTo("+123456789")
_33
params.SetFrom("+987654321")
_33
_33
resp, err := client.Api.CreateCall(params)
_33
if err != nil {
_33
fmt.Println(err.Error())
_33
os.Exit(1)
_33
} else {
_33
if resp.Sid != nil {
_33
fmt.Println(*resp.Sid)
_33
} else {
_33
fmt.Println(resp.Sid)
_33
}
_33
}
_33
}

Output

_38
{
_38
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_38
"annotation": null,
_38
"answered_by": null,
_38
"api_version": "2010-04-01",
_38
"caller_name": null,
_38
"date_created": "Tue, 31 Aug 2010 20:36:28 +0000",
_38
"date_updated": "Tue, 31 Aug 2010 20:36:44 +0000",
_38
"direction": "inbound",
_38
"duration": "15",
_38
"end_time": "Tue, 31 Aug 2010 20:36:44 +0000",
_38
"forwarded_from": "+141586753093",
_38
"from": "+987654321",
_38
"from_formatted": "(415) 867-5308",
_38
"group_sid": null,
_38
"parent_call_sid": null,
_38
"phone_number_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_38
"price": "-0.03000",
_38
"price_unit": "USD",
_38
"sid": "CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_38
"start_time": "Tue, 31 Aug 2010 20:36:29 +0000",
_38
"status": "completed",
_38
"subresource_uris": {
_38
"notifications": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Notifications.json",
_38
"recordings": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Recordings.json",
_38
"payments": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Payments.json",
_38
"events": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Events.json",
_38
"siprec": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Siprec.json",
_38
"streams": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Streams.json",
_38
"user_defined_message_subscriptions": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessageSubscriptions.json",
_38
"user_defined_messages": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/UserDefinedMessages.json"
_38
},
_38
"to": "+123456789",
_38
"to_formatted": "(415) 867-5309",
_38
"trunk_sid": null,
_38
"uri": "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Calls/CAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.json",
_38
"queue_time": "1000"
_38
}

This code starts a phone call between the two phone numbers that we pass as arguments. The 'from' number is our Twilio number, and the 'to' number is who we want to call.

The URL argument points to some TwiML, which tells Twilio what to do next when our recipient answers their phone. This TwiML instructs Twilio to read a message using text to speech and then play an MP3.

Before this code works, though, we need to do prepare your environment a bit more to work with your Twilio account.

Set your credentials as environment variables

set-your-credentials-as-environment-variables page anchor

You'll notice that this code's main function creates a new Twilio client using the twilio.NewRestClient method, but no credentials are passed to it directly. The Twilio Go helper checks to see if your credentials are available as environment variables, and automatically consumes them for you.

To get and set these credentials, first go to https://www.twilio.com/console(link takes you to an external page) and log in. On this page, you'll find your unique Account SID and Auth Token, which you'll need any time you send messages through the Twilio client like this. You can reveal your auth token by clicking Show:

How to show your Auth Token from the Twilio Console.

Copy each value, and set them as environment variables in your terminal with the following commands (replacing the placeholders with your own values):


_10
export TWILIO_ACCOUNT_SID=<your-account-sid>
_10
export TWILIO_AUTH_TOKEN=<your-auth-token>

(information)

Info

Check out how to set environment variables(link takes you to an external page) for more information or other platform-specific syntax.

Replace the to and from phone numbers

replace-the-to-and-from-phone-numbers page anchor

Remember that voice-enabled phone number you bought just a few minutes ago? Go ahead and update the existing call to params.SetFrom to use that number instead, making sure to use E.164 formatting:

[+][country code][phone number including area code]

Next, replace the phone number in params.SetTo with your mobile phone number. This number can be for any phone that can receive calls, but it's a good idea to test with your phone so that you can see the magic happen! As above, you should use E.164 formatting for this value.

Save your changes and run the code by running the following command from your terminal:


_10
go run makecall.go

That's it! Your phone should ring with a call from your Twilio number, and you'll hear our short message for you. 😉

(warning)

Warning

If you're using a Twilio trial account, outgoing phone calls are limited to phone numbers you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs(link takes you to an external page). For other trial account restrictions and limitations, check out our guide on how to work with your free Twilio trial account.

Next, we'll learn how to respond to a call made to your Twilio phone number. First, we'll need to get a Gin server up and running.


Receive and respond to inbound voice calls with Go and Gin

receive-and-respond-to-inbound-voice-calls-with-go-and-gin page anchor

When your Twilio number receives an incoming phone call, it will send an HTTP request to a server you control. This callback mechanism is known as a webhook(link takes you to an external page). When Twilio sends your application a request, it expects a response in the TwiML XML format that tells it how to respond to the call.

To handle incoming phone calls we'll need a lightweight web application to accept incoming HTTP requests from Twilio. While you can use any number of web frameworks to make and receive calls from your application, we'll use Go with the Gin framework(link takes you to an external page) in this quickstart.

On the command line in your current directory, run the following command:


_10
go get -u github.com/gin-gonic/gin

Create a file called server.go and use the following code to create a server that can handle incoming messages.

Generate TwiML to Say a message

generate-twiml-to-say-a-message page anchor
Go

_28
package main
_28
_28
import (
_28
"net/http"
_28
_28
"github.com/gin-gonic/gin"
_28
"github.com/twilio/twilio-go/twiml"
_28
)
_28
_28
func main() {
_28
router := gin.Default()
_28
_28
router.POST("/answer", func(context *gin.Context) {
_28
say := &twiml.VoiceSay{
_28
Message: "Hello from your pals at Twilio! Have fun.",
_28
}
_28
_28
twimlResult, err := twiml.Voice([]twiml.Element{say})
_28
if err != nil {
_28
context.String(http.StatusInternalServerError, err.Error())
_28
} else {
_28
context.Header("Content-Type", "text/xml")
_28
context.String(http.StatusOK, twimlResult)
_28
}
_28
})
_28
_28
router.Run(":1337")
_28
}

Run this server with the following command:


_10
go run server.go

Now, all you have to do is expose this server to Twilio.


Allow Twilio to talk to your application

allow-twilio-to-talk-to-your-application page anchor

Before you can instruct Twilio on what to do in response to an incoming call, you first need to expose your server to the public. When you run your local development server, the odds are very high it is only accessible from your local network. But don't worry—we'll show you an easy way to test your server.

Many Twilio products and services use webhooks to communicate with your application. For example, when Twilio receives an incoming call, it reaches out to a specific URL you provide to search for instructions on how to handle the response.

The small piece of code in server.go is an example of one instruction you can use to 'speak' back to the caller. We just have to find a way to expose this server to the public.

While there are many ways to make this code public (for example by deploying it to a host), we recommend a tool called ngrok. When you start ngrok, it provides a unique URL on the ngrok.io domain and forwards incoming requests to your local development environment.

The architecture looks like this:

How ngrok helps Twilio reach your local server.

If you don't already use ngrok, head over to their download page(link takes you to an external page) and grab the appropriate binary for your operating system. Once downloaded, unzip the package.

If you're working on a Mac or Linux, you're all set. If you're on Windows, follow our guide on how to install and configure ngrok on Windows. For more info on ngrok, including some great tips and tricks, check out this in-depth blog post(link takes you to an external page).

Expose your local server with ngrok

expose-your-local-server-with-ngrok page anchor

Once downloaded, make sure your server is running if it isn't already with:


_10
go run server.go

Your local application must be running locally for ngrok to do its magic.

Then open a new terminal tab or window and start ngrok with this command:


_10
ngrok http 1337

If your local server is running on a different port, replace 1337 with the correct port number.

You should see output similar to this:

ngrok-node-voice-1337port.

Copy your public URL from this output and paste it into your browser. You should see your Gin application's "Hello from your pals at Twilio!" message if you append /answer to that URL.

Configure your Twilio webhook

configure-your-twilio-webhook page anchor

Now, you must take that public URL and configure this as a webhook for one of your phone numbers(link takes you to an external page) in the console. Go back to the console, select your phone number, change "A CALL COMES IN" to Webhook, and then put in the external URL to your service, such as "https://069a61b7.ngrok.io/answer(link takes you to an external page)", as shown above.

Make sure to click save, and head back to your terminal. Make sure ngrok is still running in one tab, and in another, run go run server.go if it is not running already.

Make a phone call to your Twilio phone number. Within a few moments, you'll see an HTTP request in your ngrok console, and you'll hear a short message once the call connects.

And there you… go—you've successfully made and received a call with Go.


Now you know the basics of making and responding to phone calls with Go.

This app only used the <Say> TwiML verb to read a message to the caller using text to speech. With different TwiML verbs, you can create other powerful constructs and call flows. Try a few, such as <Record>, <Gather>, and <Conference>.

We can't wait to see what you build!


Rate this page: