Skip to contentSkip to navigationSkip to topbar
Rate this page:
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:


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


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 code

send-an-sms-verification-code-1 page anchor

Sends a one-time passcode to a user's phone number

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.verifications
_11
.create({to: '+15017122661', channel: 'sms'})
_11
.then(verification => console.log(verification.status));

Output

_23
{
_23
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_23
"to": "+15017122661",
_23
"channel": "sms",
_23
"status": "pending",
_23
"valid": false,
_23
"date_created": "2015-07-30T20:00:00Z",
_23
"date_updated": "2015-07-30T20:00:00Z",
_23
"lookup": {},
_23
"amount": null,
_23
"payee": null,
_23
"send_code_attempts": [
_23
{
_23
"time": "2015-07-30T20:00:00Z",
_23
"channel": "SMS",
_23
"attempt_sid": "VLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}
_23
],
_23
"sna": null,
_23
"url": "https://verify.twilio.com/v2/Services/VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Verifications/VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_23
}

Save your changes and run this script from your terminal:


_10
go run sendverification.go

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.

Checks the user-provided passcode. The provided code is correct if the response 'status' parameter is 'approved'.

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_11
// Download the helper library from https://www.twilio.com/docs/node/install
_11
// Find your Account SID and Auth Token at twilio.com/console
_11
// and set the environment variables. See http://twil.io/secure
_11
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_11
const authToken = process.env.TWILIO_AUTH_TOKEN;
_11
const client = require('twilio')(accountSid, authToken);
_11
_11
client.verify.v2.services('VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_11
.verificationChecks
_11
.create({to: '+15017122661', code: '123456'})
_11
.then(verification_check => console.log(verification_check.status));

Output

_14
{
_14
"sid": "VEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"service_sid": "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_14
"to": "+15017122661",
_14
"channel": "sms",
_14
"status": "approved",
_14
"valid": true,
_14
"amount": null,
_14
"payee": null,
_14
"sna_attempts_error_codes": [],
_14
"date_created": "2015-07-30T20:00:00Z",
_14
"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:


_10
go run checkcode.go

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.

phone-verification-with-twilio-verify-and-go page anchor
Go

_59
package main
_59
_59
import (
_59
"fmt"
_59
_59
"github.com/twilio/twilio-go"
_59
openapi "github.com/twilio/twilio-go/rest/verify/v2"
_59
)
_59
_59
accountSid := "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_59
authToken := "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
_59
verifyServiceSid := "VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_59
_59
client := twilio.NewRestClientWithParams(twilio.ClientParams{
_59
Username: accountSid,
_59
Password: authToken,
_59
})
_59
_59
// This function sends an OTP to your phone number
_59
func sendOtp(to string) {
_59
params := &openapi.CreateVerificationParams{}
_59
params.SetTo(to)
_59
params.SetChannel("sms")
_59
_59
resp, err := client.VerifyV2.CreateVerification(verifyServiceSid, params)
_59
if err != nil {
_59
fmt.Println(err.Error())
_59
} else {
_59
fmt.Printf("Sent verification '%s'\n", *resp.Sid)
_59
}
_59
}
_59
_59
// This function waits for you to input the OTP sent to your phone,
_59
// and validates that the code is approved
_59
func checkOtp(to string) {
_59
var code string
_59
fmt.Println("Please check your phone and enter the code:")
_59
fmt.Scanln(&code)
_59
_59
params := &openapi.CreateVerificationCheckParams{}
_59
params.SetTo(to)
_59
params.SetCode(code)
_59
_59
resp, err := client.VerifyV2.CreateVerificationCheck(verifyServiceSid, params)
_59
if err != nil {
_59
fmt.Println(err.Error())
_59
} else if *resp.Status == "approved" {
_59
fmt.Println("Correct!")
_59
} else {
_59
fmt.Println("Incorrect!")
_59
}
_59
}
_59
_59
func main() {
_59
to := "<your phone number here>"
_59
_59
sendOtp(to)
_59
checkOtp(to)
_59
}

Output

_10
$ go run .
_10
Sent verification 'VEd123455403fa12345c4812345c812345'
_10
Please check your phone and enter the code:
_10
077100
_10
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.


Rate this page: