# Integrating Twilio Verification SDK for Android using the sample backend

This guide is part of the [Twilio Verification SDK](/docs/verify/tutorials/android-sdk-integration) 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](https://github.com/authy/authy-sdk-backend)

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

Note:

* If you need help finding your AUTHY\_API\_KEY, please [follow these steps](/docs/authy/twilioauth-sdk/quickstart/obtaining-authy-api-key)
* If you need help finding your APP\_ID, please [follow these steps](/docs/authy/obtain-your-app-id)

![Deploy Authy SDK Backend with options for app name and runtime selection.](https://docs-resources.prod.twilio.com/7e480f1b781473e835c713bae8453cad30a02a16819ccf7098c95c7d7cf3011b.gif)

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

![Config variables setup with APP\_ID, AUTHY\_API\_KEY, LANG, and RACK\_ENV fields.](https://docs-resources.prod.twilio.com/081e8aa9dd1c5f6da6e0bf72eb123fdf60dfa27467b41ecbe6d7bd57c18e4ba8.png)

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

`Build.gradle`

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

Retrofit interface: `TokenServerApi.java`

```java
public interface {
    @POST("/verify/token")
    @FormUrlEncoded
    Call<TokenServerResponse> getToken(@Field("phone_number") String phoneNumber);
}
```

Response holder: `TokenServerResponse.java`

```java
public class TokenServerResponse {
    @SerializedName("jwt_token")
    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.

```java
    private TokenServerApi tokenServerApi;

    private void initTokenServerApi() {
        String TOKEN_SERVER_URL = "https://verification-token.herokuapp.com";

        Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(TOKEN_SERVER_URL)
            .build();

        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.

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

            tokenServerApi
                .getToken(numberToVerify)
                .enqueue(new Callback<TokenServerResponse>() {

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

                @Override
                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`

```bash
    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`

```bash
allprojects {
    repositories {
        jcenter()
        (...)
    }
}
```

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

```java
    private TwilioVerification twilioVerification;
```

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

```java
    twilioVerification = new TwilioVerification(this);
```

Add the start verification call when JWT is received

```java
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)

```xml
    <receiver android:name=".MyVerificationReceiver" >
        <intent-filter>
            <action android:name="com.twilio.verification.current_status" />
        </intent-filter>
    </receiver>
```

`MyVerificationReceiver.java`

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

            // NOT_STARTED, STARTED, AWAITING_VERIFICATION, SUCCESS, ERROR
            state = VerificationStatus.State
    }
}
```

## [Next step: Integrate Twilio Verification with your own backend](/docs/verify/tutorials/android-sdk-integration-custom-backend)
