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 to ease development.
In this quickstart, you will learn how to:
If you already have a Twilio account and a voice-enabled Twilio phone number you're all set here! Log in 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 to an account you already have.
The next thing you'll need is a voice-capable Twilio phone number. 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 page, check the "Voice" box and click "Search."
_10go version
You should see something like:
_10$ go version_10go 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. 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.
Create a new Go project from your terminal using:
_10go 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.
_10go 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.
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:
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.
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 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: