How to Do Phone Verification in Go with Twilio Verify
Phone verification is a common requirement in many applications to ensure the authenticity of users. Twilio Verify is a robust service that helps you implement phone verification easily and securely. In this blog post, we will walk through the steps to set up phone verification in a Go application using Twilio Verify.
Prerequisites
Before you start, make sure you have the following:
- A Twilio account. If you don’t have one, you can sign up here.
- Go installed on your machine. You can download it from the official Go website.
- A Twilio Verify service SID, which you can create from the Twilio Console under the Verify services section.
Step 1: Install the Twilio Go Helper Library
First, you need to install the Twilio Go helper library. This library simplifies interacting with the Twilio API.
go get github.com/twilio/twilio-go
Step 2: Set Up Environment Variables
For security reasons, it’s best to use environment variables to store sensitive information such as your Twilio Account SID, Auth Token, and Verify Service SID. Create a .env
file in your project directory:
TWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_VERIFY_SERVICE_SID=your_verify_service_sid
Then, use the godotenv
package to load these environment variables. Install it by running:
go get github.com/joho/godotenv
Step 3: Initialize Your Go Application
Create a new Go file, for example, main.go
, and initialize your Go application. Load the environment variables using godotenv
.
package
mainimport
( "log"
"os"
"github.com/joho/godotenv"
"github.com/twilio/twilio-go"
"github.com/twilio/twilio-go/client"
"github.com/twilio/twilio-go/rest/verify/v2"
)func init()
{
err := godotenv.Load() if err != nil
{
log.Fatal("Error loading .env file"
)
}
}
Step 4: Send a Verification Code
Create a function to send a verification code to the user's phone number.
func sendVerificationCode(phoneNumber string) (*v2.VerifyV2ServiceVerification, error
) {
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: os.Getenv("TWILIO_ACCOUNT_SID"
),
Password: os.Getenv("TWILIO_AUTH_TOKEN"
),
})
params := &v2.CreateVerificationParams{}
params.SetTo(phoneNumber)
params.SetChannel("sms"
)
resp, err := client.VerifyV2.CreateVerification(os.Getenv("TWILIO_VERIFY_SERVICE_SID"
), params) if err != nil
{
return nil
, err
} return resp, nil
}
Step 5: Verify the Code
Create a function to verify the code entered by the user.
func checkVerificationCode(phoneNumber string, code string) (*v2.VerifyV2ServiceVerificationCheck, error
) {
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: os.Getenv("TWILIO_ACCOUNT_SID"
),
Password: os.Getenv("TWILIO_AUTH_TOKEN"
),
})
params := &v2.CreateVerificationCheckParams{}
params.SetTo(phoneNumber)
params.SetCode(code)
resp, err := client.VerifyV2.CreateVerificationCheck(os.Getenv("TWILIO_VERIFY_SERVICE_SID"
), params) if err != nil
{
return nil
, err
} return resp, nil
}
Step 6: Implementing the API Endpoints
Now, let's create API endpoints to handle the verification process. We will use the Gin framework for this. Install Gin using:
go get github.com/gin-gonic/gin
Then, create the following endpoints in your main.go
:
func main()
{
router := gin.Default()
router.POST("/send-verification", func(c *gin.Context)
{
phoneNumber := c.PostForm("phone_number"
)
resp, err := sendVerificationCode(phoneNumber)
if err != nil
{
c.JSON(500, gin.H{"error"
: err.Error()})
return
}
c.JSON(200, gin.H{"message": "Verification code sent", "sid"
: resp.Sid})
})
router.POST("/check-verification", func(c *gin.Context)
{
phoneNumber := c.PostForm("phone_number"
)
code := c.PostForm("code"
)
resp, err := checkVerificationCode(phoneNumber, code)
if err != nil
{
c.JSON(500, gin.H{"error"
: err.Error()})
return
}
c.JSON(200, gin.H{"status"
: resp.Status})
})
router.Run(":8080"
)
}
Step 7: Running the Application
Run your Go application using:
go run main.go
Now, you can use tools like Postman or curl to test the endpoints:
- Send Verification Code:
curl -X POST http://localhost:8080/send-verification -d "phone_number=+1234567890"
- Check Verification Code:
curl -X POST http://localhost:8080/check-verification -d "phone_number=+1234567890" -d "code=123456"
Conclusion
Implementing phone verification in a Go application using Twilio Verify is straightforward. By following the steps outlined in this guide, you can enhance the security of your application and ensure that your users are authentic. Twilio’s comprehensive API and Go SDK make it easy to integrate phone verification into your applications, providing a robust solution for user authentication.