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.

_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

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

Output

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

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.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.