Tutorial on SMS verification service in Twilio

Author: Manish Prabhu and Ganesh Nyati

Twilio is a cloud communication platform which is used to send & receive SMS, make & receive voice calls, etc. This cloud platform provides SMS verification service which we can use in our web application. Twilio provides a set of three verify API which can be used to create verification service, send verification code SMS to a phone number & verify that code respectively. We will demonstrate this by showing the APIs along with their specification & then by designing a mule application.

In order to use these APIs for SMS verification, we need to follow three steps. 

Step 1: Create a verification service using twilio API

POST https://verify.twilio.com/v2/Services

This API is used to create a verification service in twilio. We need to provide the name of the verification service in the body of this POST request inside the FriendlyName parameter in x-www-form-urlencoded format. This API also requires ACCOUNT_SID as username & AUTHTOKEN as password in the basic authentication. The output of this API will be a json object consisting of sid as an attribute starting with VA letters which should be used in the next step.

Step 2: Send the verification code using twilio API

POST https://verify.twilio.com/v2/Services/{verificationServiceId}/Verifications

This API is used to send the verification code. We need to provide the language in which verification code will be sent inside the locale parameter. The locale parameter accepts two letter language code. For e.g. for hindi the code is ‘hi’. It supports a total 35 languages. We also need to provide the number on which the verification code is sent inside To parameter & channel as sms. All these parameters are accepted in x-www-form-urlencoded format. This API also requires ACCOUNT_SID as username & AUTHTOKEN as password in the basic authentication.

For more information about languages, you can check:

https://www.twilio.com/docs/verify/supported-languages

Step 3: Verification of code using twilio API

POST https://verify.twilio.com/v2/Services/{verificationServiceId}/VerificationCheck

This API is used to verify the verification code. We need to provide the phone number inside To parameter & verification code inside the code parameter. All these parameters are accepted in x-www-form-urlencoded format. This API also requires ACCOUNT_SID as username & AUTHTOKEN as password in the basic authentication.

Implementation in Mule 4:

Step 1 flow:

The first transform message is configured as:

So I am passing the name of the verification service inside the nameOfService parameter. Second transform message is configured as:

We can verify the output using the postman request.

We can also verify the sms received.

Step 2 flow:

The first transform message is configured as:

So I am passing the name of the verification service inside the Locale(language code), To(verified phone number on twilio), Channel as sms parameter.

Second transform message is configured as:

We can verify the output in postman request:

You can also verify this by checking sms on given phone number:

Step 3 flow:

The first transform message is configured as:

I am passing a registered mobile number and the verification code is received on the same number. The second transform message is configured as:

We can verify the output using a postman request. The status attribute in the output of the previous request was pending. It will be changed to approved.

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.