Rate this page:

Integrating Twilio Verification SDK for Android using the sample backend

This guide is part of the Twilio Verification SDK integration guides

For the full flow to be implemented, you must deploy your own JWT provider. The service will receive phone numbers and return signed JWTs, using your AUTHY_API_KEY as encoding key.

First, we’ll integrate the SDK against a sample backend that we’ve already set up for you.

Step 1: deploy a sample backend to begin the integration

We’ll need to add a call to the sample token server, which you can deploy with just one click from the github repository

Then you will need to set your AUTHY_API_KEY and your APP_ID in the heroku app as an environment variable.


- If you need help finding your AUTHY_API_KEY, please follow these steps

- If you need help finding your APP_ID, please follow these steps

Setup your AUTHY_API_KEY

This is how your environment should look like after the configuration.

Setup your AUTHY_API_KEY and APP_ID in your sample deployment

Step 2: query your sample token server to transform phone numbers into JWTs

Create a new blank-activity project on Android Studio. In this example we’ll use retrofit.


    compile 'com.squareup.retrofit2:retrofit:2.2.0' 
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'

Retrofit interface:

public interface {
    Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);

Response holder:

public class TokenServerResponse {
    private String jwtToken;

    public String getJwtToken() {
        return jwtToken;

    public void setJwtToken(String jwtToken) {
        this.jwtToken = jwtToken;

In the main activity initialize the service. Replace the TOKEN_SERVER_URL string with your deployed sample backend.

    private TokenServerApi tokenServerApi;

    private void initTokenServerApi() {
        String TOKEN_SERVER_URL = "";

        Retrofit retrofit = new Retrofit.Builder()

        tokenServerApi = retrofit.create(TokenServerApi.class);

Make the call to the sample token server. This server will receive a phone number and return a JWT which will be used to verify the authenticity of the requester.

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String numberToVerify = "1555555555"; //Should come from user input

                .enqueue(new Callback<TokenServerResponse>() {

                public void onResponse(Call<TokenServerResponse> call,
                                       Response<TokenServerResponse> response) {
                    String jwtToken = response.body().getJwtToken();

                public void onFailure(Call<TokenServerResponse> call, Throwable t) {
                    throw new RuntimeExecutionException(t); //Woops!

Step 3: add Twilio Verification SDK for Android

Add the SDK into your module's build.gradle

    compile 'com.twilio:verification:+'

Note: The SDK is exposed via jCenter repository, so make sure you add that repository in your repository list. In your app’s build.gradle

allprojects {
    repositories {

Create a TwilioVerification instance. Keep a reference in your activity or presenter

    private TwilioVerification twilioVerification;

Instantiate it in onCreate(). The constructor will require a context.

    twilioVerification = new TwilioVerification(this);

Add the start verification call when JWT is received

public void onResponse(Call<TokenServerResponse> call, 
                       Response<TokenServerResponse> response) {
    String jwtToken = response.body().getJwtToken();
    twilioVerification.startVerification(jwtToken, Via.SMS);

Step 4: receive TwilioVerification callback

When the user's device receives a phone verification SMS from Twilio, Google Play services will automatically pass it to Twilio Verification SDK for validation. This will let your app know when the SDK has a response.

Androidmanifest.xml (Inside <application> tag)

    <receiver android:name=".MyVerificationReceiver" >
            <action android:name="com.twilio.verification.current_status" />

    public class MyVerificationReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            VerificationStatus verificationStatus = TwilioVerification.getVerificationStatus(intent);

            state = VerificationStatus.State

Next step: Integrate Twilio Verification with your own backend

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

Loading Code Sample...

        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!