Programmable Voice quickstart for Go
This quickstart will show you how to build an application with Twilio's Go Helper Library.
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.
Log in to your Twilio account or sign up for a new one.
If you need a Twilio phone number, buy one in the Twilio Console:
- Go to Phone Numbers > Manage > Buy a Number.
- Under Capabilities, select Voice.
- Click Search. The page displays a list of available voice-capable phone numbers.
- 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 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 Twilio Go Helper Library using the following command:
go get github.com/twilio/twilio-go
Create a new file named makecall.go
with the following code:
1// Download the helper library from https://www.twilio.com/docs/go/install2package main34import (5"fmt"6"github.com/twilio/twilio-go"7api "github.com/twilio/twilio-go/rest/api/v2010"8"os"9)1011func main() {12// Find your Account SID and Auth Token at twilio.com/console13// and set the environment variables. See http://twil.io/secure14// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment15client := twilio.NewRestClient()1617params := &api.CreateCallParams{}18params.SetUrl("http://demo.twilio.com/docs/voice.xml")19params.SetTo("+123456789")20params.SetFrom("+19876543210")2122resp, err := client.Api.CreateCall(params)23if err != nil {24fmt.Println(err.Error())25os.Exit(1)26} else {27if resp.Sid != nil {28fmt.Println(*resp.Sid)29} else {30fmt.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.
- Go to the Twilio Console.
- 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
- 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 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.
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. To learn about other trial account limitations, see how to work with your free Twilio trial account.
To handle incoming phone calls, you'll need a web application that can accept HTTP requests from Twilio.
Install the Gin framework 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:
1package main23import (4"net/http"56"github.com/gin-gonic/gin"7"github.com/twilio/twilio-go/twiml"8)910func main() {11router := gin.Default()1213router.POST("/answer", func(context *gin.Context) {14say := &twiml.VoiceSay{15Message: "Hello from your pals at Twilio! Have fun.",16}1718twimlResult, err := twiml.Voice([]twiml.Element{say})19if err != nil {20context.String(http.StatusInternalServerError, err.Error())21} else {22context.Header("Content-Type", "text/xml")23context.String(http.StatusOK, twimlResult)24}25})2627router.Run(":1337")28}
Save the file. Then, start the server with the following command:
go run server.go
If you don't already use ngrok, download and install it.
Open a new command line window or tab and start ngrok with this command:
ngrok http 1337
You should see output similar to:
1Session Status online2Version 3.23.23Web Interface http://127.0.0.1:40404Forwarding https://ff4660c91380.ngrok.app -> http://localhost:133756Connections ttl opn rt1 rt5 p50 p9070 0 0.00 0.00 0.00 0.00
- Go to the Active Numbers page in the Twilio Console.
- Click your Twilio phone number.
- Visit the Configure tab and find the Voice Configuration section.
- 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
). - 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.