Skip to contentSkip to navigationSkip to topbar
On this page

Verify Go Quickstart


With just a few lines of code, your Go application can verify phone numbers and add another layer of security with Twilio Verify.

This Verify Quickstart will teach you how to do this using our Verify REST API and the Twilio Go helper library.

In this Quickstart, you will learn how to:

  1. Sign up for Twilio
  2. Set up your development environment
  3. Send your first SMS phone verification
  4. Check verification codes
(information)

Info


Sign up for Twilio

sign-up-for-twilio page anchor

If you already have a Twilio account, you're all set here! Feel free to jump to the next step.

Before you can send an SMS with Go, you'll need to sign up for a Twilio account(link takes you to an external page) or sign into your existing account.

You can sign up for a free Twilio trial account here(link takes you to an external page).

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test verification messages to your phone from your Twilio account while in trial mode. This phone verification step is exactly what you'll learn how to build in this tutorial!
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console(link takes you to an external page). This is where you'll be able to access your Account SID, authentication token, create a verification service, and more.

Do I need a phone number?

do-i-need-a-phone-number page anchor

If you've sent SMS with Twilio in the past, you might remember needing to buy a phone number. With Twilio Verify, we take care of that for you! The Verify API selects the best routes for quickly and reliably delivering verification codes globally.

Verify uses Services for configuration. To send a Verify API request you will need both your Twilio Credentials and a Service SID. You can create and update a Service in two ways:

  1. In the Verify Console(link takes you to an external page)
  2. With the Verify API

Services can be used to edit the name (which shows up in the message template), set the code length (4-10 characters), enable settings like the "do not share warning" and more.

Now that you have a Twilio account and a verification service, you can start writing some code!

To make things even easier, we'll next install Twilio's official helper library for Go applications.


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.

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

go version

You should see something like:

1
$ go version
2
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:

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.

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.


Send an SMS verification code

send-an-sms-verification-code page anchor

Now that you have Go and the Twilio Go library installed, you can send an SMS verification code from the Twilio Verify Service that you just created to your phone with a single API request.

Create and open a new file called sendverification.go and type or paste in this code sample.

Send an SMS verification codeLink to code sample: Send an SMS verification code
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
verify "github.com/twilio/twilio-go/rest/verify/v2"
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 := &verify.CreateVerificationParams{}
18
params.SetChannel("sms")
19
params.SetTo("+15017122661")
20
21
resp, err := client.VerifyV2.CreateVerification("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
22
params)
23
if err != nil {
24
fmt.Println(err.Error())
25
os.Exit(1)
26
} else {
27
if resp.Status != nil {
28
fmt.Println(*resp.Status)
29
} else {
30
fmt.Println(resp.Status)
31
}
32
}
33
}

Output

1
{
2
"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"to": "+15017122661",
6
"channel": "sms",
7
"status": "pending",
8
"valid": false,
9
"date_created": "2015-07-30T20:00:00Z",
10
"date_updated": "2015-07-30T20:00:00Z",
11
"lookup": {},
12
"amount": null,
13
"payee": null,
14
"send_code_attempts": [
15
{
16
"time": "2015-07-30T20:00:00Z",
17
"channel": "SMS",
18
"attempt_sid": "VLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19
}
20
],
21
"sna": null,
22
"url": "https://verify.twilio.com/v2/Services/VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Verifications/VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
23
}

Save your changes and run this script from your terminal:

1
go run sendverification.go
2

That's it! In a few moments, your phone will receive a verification code.


Check a verification code

check-a-verification-code page anchor

Sending verification codes is only half of the equation. You also need to be able to check the codes to verify your users!

Create and open a new file called checkcode.go and type or paste in this code sample.

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
verify "github.com/twilio/twilio-go/rest/verify/v2"
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 := &verify.CreateVerificationCheckParams{}
18
params.SetTo("+15017122661")
19
params.SetCode("123456")
20
21
resp, err := client.VerifyV2.CreateVerificationCheck("VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
22
params)
23
if err != nil {
24
fmt.Println(err.Error())
25
os.Exit(1)
26
} else {
27
if resp.Status != nil {
28
fmt.Println(*resp.Status)
29
} else {
30
fmt.Println(resp.Status)
31
}
32
}
33
}

Output

1
{
2
"sid": "VEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"service_sid": "VAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"to": "+15017122661",
6
"channel": "sms",
7
"status": "approved",
8
"valid": true,
9
"amount": null,
10
"payee": null,
11
"sna_attempts_error_codes": [],
12
"date_created": "2015-07-30T20:00:00Z",
13
"date_updated": "2015-07-30T20:00:00Z"
14
}

Replace to with the phone number that you sent the code to, and code with the verification code that you received.

Save your changes and run this script from your terminal:

1
go run checkcode.go
2

After the briefest delay, you will see "approved" appear in your terminal, signaling that this combination of phone number and code are considered verified!


Test the verification lifecycle with one program

test-the-verification-lifecycle-with-one-program page anchor

Let's combine these two processes into a single program that accepts terminal input.

(information)

Info

In production, you would build an OTP input modal or page in your site interface to accept the input.

There will be a main.go file in your directory from when you first bootstrapped this Go project. Go ahead and open that file, replace the boilerplate contents with the following code sample, replace the placeholder strings for Account SID, Auth Token, Verify Service SID, and your phone number in E.164 format, and save your changes.

Phone verification with Twilio Verify and Go.Link to code sample: Phone verification with Twilio Verify and Go.
1
package main
2
3
import (
4
"fmt"
5
6
"github.com/twilio/twilio-go"
7
openapi "github.com/twilio/twilio-go/rest/verify/v2"
8
)
9
10
accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
11
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
12
verifyServiceSid := "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
13
14
client := twilio.NewRestClientWithParams(twilio.ClientParams{
15
Username: accountSid,
16
Password: authToken,
17
})
18
19
// This function sends an OTP to your phone number
20
func sendOtp(to string) {
21
params := &openapi.CreateVerificationParams{}
22
params.SetTo(to)
23
params.SetChannel("sms")
24
25
resp, err := client.VerifyV2.CreateVerification(verifyServiceSid, params)
26
if err != nil {
27
fmt.Println(err.Error())
28
} else {
29
fmt.Printf("Sent verification '%s'\n", *resp.Sid)
30
}
31
}
32
33
// This function waits for you to input the OTP sent to your phone,
34
// and validates that the code is approved
35
func checkOtp(to string) {
36
var code string
37
fmt.Println("Please check your phone and enter the code:")
38
fmt.Scanln(&code)
39
40
params := &openapi.CreateVerificationCheckParams{}
41
params.SetTo(to)
42
params.SetCode(code)
43
44
resp, err := client.VerifyV2.CreateVerificationCheck(verifyServiceSid, params)
45
if err != nil {
46
fmt.Println(err.Error())
47
} else if *resp.Status == "approved" {
48
fmt.Println("Correct!")
49
} else {
50
fmt.Println("Incorrect!")
51
}
52
}
53
54
func main() {
55
to := "<your phone number here>"
56
57
sendOtp(to)
58
checkOtp(to)
59
}

Output

1
$ go run .
2
Sent verification 'VEd123455403fa12345c4812345c812345'
3
Please check your phone and enter the code:
4
077100
5
Correct!

This code sends an SMS OTP to your phone, and uses Go's fmt.Scanln to accept your input through the terminal.

The code then checks to make sure that the status is approved. If you provide an incorrect code, the status will remain "pending" and you'll see "Incorrect!" print to the terminal instead of "Correct!".

For this example, the verification channel is hard coded as "sms", but you could make this dynamic to accept other channel options such as "call" or "whatsapp".


Now that you've seen how to leverage Verify for SMS verification, check out adding additional verification channels supported by the Verify API like:

(information)

Info

Lastly, to protect your service against fraud, view our guidance on Preventing Toll Fraud when using Verify.