Skip to contentSkip to navigationSkip to topbar
On this page

Programmable Voice quickstart for Go


This quickstart will show you how to build an application with Twilio's Go Helper Library(link takes you to an external page).

Your application will make an outbound phone call and handle inbound phone calls. It will use the Twilio Voice API to make the outbound call, and TwiML to respond to the inbound call by reading a message using text-to-speech.


Get a phone number

get-a-phone-number page anchor

Log in to your Twilio account(link takes you to an external page) or sign up for a new one(link takes you to an external page).

If you need a Twilio phone number, buy one in the Twilio Console:

  1. Go to Phone Numbers > Manage > Buy a Number(link takes you to an external page).
  2. Under Capabilities, select Voice.
  3. Click Search. The page displays a list of available voice-capable phone numbers.
  4. Click Buy next to a phone number to add it to your account.

To check if you have Go installed on your machine, open a terminal and run the following command:

go version

If Go is installed, you should see output similar to:

go version go1.24.4 darwin/arm64

If Go is not installed, install it(link takes you to an external page) and open a new terminal window before continuing.


Create a new directory for this project and navigate to it using the command line. Then, create a Go module using the following command:

go mod init voice-quickstart

Install the Go Helper Library

install-the-go-helper-library page anchor

Install the Twilio Go Helper Library using the following command:

go get github.com/twilio/twilio-go

Make an outgoing phone call with Go

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

Create a new file named makecall.go with the following code:

Make an outbound callLink to code sample: Make an outbound call
1
// Download the helper library from https://www.twilio.com/docs/go/install
2
package main
3
4
import (
5
"fmt"
6
"github.com/twilio/twilio-go"
7
api "github.com/twilio/twilio-go/rest/api/v2010"
8
"os"
9
)
10
11
func main() {
12
// Find your Account SID and Auth Token at twilio.com/console
13
// and set the environment variables. See http://twil.io/secure
14
// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
15
client := twilio.NewRestClient()
16
17
params := &api.CreateCallParams{}
18
params.SetUrl("http://demo.twilio.com/docs/voice.xml")
19
params.SetTo("+123456789")
20
params.SetFrom("+19876543210")
21
22
resp, err := client.Api.CreateCall(params)
23
if err != nil {
24
fmt.Println(err.Error())
25
os.Exit(1)
26
} else {
27
if resp.Sid != nil {
28
fmt.Println(*resp.Sid)
29
} else {
30
fmt.Println(resp.Sid)
31
}
32
}
33
}

This code starts a phone call between the two phone numbers that you pass as arguments. The SetFrom number is your Twilio number, and the SetTo number is the number you want to call.

The SetUrl argument points to a TwiML file that tells Twilio what to do when the call recipient answers their phone. This TwiML instructs Twilio to read a message using text-to-speech and then play an MP3.

Set your credentials as environment variables

set-your-credentials-as-environment-variables page anchor
macOS TerminalWindows command linePowerShell
  1. Go to the Twilio Console(link takes you to an external page).
  2. Copy your Account SID and set it as an environment variable using the following command. Replace YOUR_ACCOUNT_SID with your actual Account SID.
    export TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SID
  3. Copy your Auth Token and set it as an environment variable using the following command. Replace YOUR_AUTH_TOKEN with your actual Auth Token.
    export TWILIO_AUTH_TOKEN=YOUR_AUTH_TOKEN

Replace the to and from phone numbers

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

Replace the SetFrom number with your Twilio phone number, and the SetTo number with your personal phone number. Ensure both numbers are in E.164 format.

Save your changes and run the code:

go run makecall.go

Your phone should ring with a call from your Twilio number. Answer it, and you'll hear a short message.

(information)

Twilio trial account limitations

Twilio trial accounts limit outgoing phone calls to phone numbers you have verified with Twilio. To verify your phone numbers, use your Twilio Console's Verified Caller IDs(link takes you to an external page). To learn about other trial account limitations, see how to work with your free Twilio trial account.


Take a phone call with your Twilio number

take-a-phone-call-with-your-twilio-number page anchor

To handle incoming phone calls, you'll need a web application that can accept HTTP requests from Twilio.

Install the Gin framework(link takes you to an external page) by running the following commands:

go get -u github.com/gin-gonic/gin
go mod tidy

Create a file named server.go with the following code:

1
package main
2
3
import (
4
"net/http"
5
6
"github.com/gin-gonic/gin"
7
"github.com/twilio/twilio-go/twiml"
8
)
9
10
func main() {
11
router := gin.Default()
12
13
router.POST("/answer", func(context *gin.Context) {
14
say := &twiml.VoiceSay{
15
Message: "Hello from your pals at Twilio! Have fun.",
16
}
17
18
twimlResult, err := twiml.Voice([]twiml.Element{say})
19
if err != nil {
20
context.String(http.StatusInternalServerError, err.Error())
21
} else {
22
context.Header("Content-Type", "text/xml")
23
context.String(http.StatusOK, twimlResult)
24
}
25
})
26
27
router.Run(":1337")
28
}

Save the file. Then, start the server with the following command:

go run server.go

Expose your local server to the internet with ngrok

expose-your-local-server-to-the-internet-with-ngrok page anchor

If you don't already use ngrok, download and install it(link takes you to an external page).

Open a new command line window or tab and start ngrok with this command:

ngrok http 1337

You should see output similar to:

1
Session Status online
2
Version 3.23.2
3
Web Interface http://127.0.0.1:4040
4
Forwarding https://ff4660c91380.ngrok.app -> http://localhost:1337
5
6
Connections ttl opn rt1 rt5 p50 p90
7
0 0 0.00 0.00 0.00 0.00

Configure your Twilio webhook

configure-your-twilio-webhook page anchor
  1. Go to the Active Numbers(link takes you to an external page) page in the Twilio Console.
  2. Click your Twilio phone number.
  3. Visit the Configure tab and find the Voice Configuration section.
  4. In the A call comes in row, select Webhook. In the URL field, enter your ngrok public URL with /answer appended (for example, https://ff4660c91380.ngrok.app/answer).
  5. Click Save configuration.

Ensure that ngrok and your Go server are still running.

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.


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>.

Let's build something amazing.