Menu

Expand
Rate 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

Short on time? Spin up a low-code, fully editable verification demo in less than 2 minutes using Twilio's Code Exchange and Quick Deploy here.

Sign up for Twilio

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 or sign into your existing account.

You can sign up for a free Twilio trial account here.

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

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.

Create a Verify Service

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

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.

Install Go

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:

$ go version
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. 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

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

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.

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

        Send an SMS verification code

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

        Save your changes and run this script from your terminal:

        go run sendverification.go
        

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

        Check a verification code

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

              Check a verification code

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

              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:

              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

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

              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.

                    
                    
                    

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

                    What's Next?

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

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

                    Shawn Stern Kelley Robinson Estefany Añasco Alan Klein Kat King Jose Oliveros Maria Bermudez Kristian Tran Anna Mason
                    Rate this page:

                    Need some help?

                    We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

                          
                          
                          

                          Thank you for your feedback!

                          Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                          Sending your feedback...
                          🎉 Thank you for your feedback!
                          Something went wrong. Please try again.

                          Thanks for your feedback!

                          Refer us and get $10 in 3 simple steps!

                          Step 1

                          Get link

                          Get a free personal referral link here

                          Step 2

                          Give $10

                          Your user signs up and upgrade using link

                          Step 3

                          Get $10

                          1,250 free SMSes
                          OR 1,000 free voice mins
                          OR 12,000 chats
                          OR more