> For the complete documentation index, see [llms.txt](https://docs.caf.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.caf.io/sdk-lite/setup/android/configuration/starting-liveness-flow.md).

# Starting Liveness Flow

The Liveness flow has three steps:

1. **`configure()`** : give the SDK your credentials, once.
2. **`prewarm()`** : fetch the liveness session while the user is still on the previous screen.
3. **`startLiveness()`** : open the camera and receive the result.

{% hint style="info" %}
Splitting the work this way lets the network request happen before the user asks for the camera, so the camera opens noticeably faster.
{% endhint %}

## Configure

Create a `CafLivenessConfig` and pass it to `configure()`. This only stores the context and configuration. It makes no network request, so it is safe to call early.

{% code title="MainActivity.kt" %}

```kotlin
val livenessConfig = CafLivenessConfig(
    mobileToken = "<YOUR_MOBILE_TOKEN>",
    stage = CafStage.PROD,
    personId = "<PERSON_ID>",
    showLoading = true,
    enableSecurity = true
)
CafFaceLivenessLite.instance.configure(context, livenessConfig)
```

{% endcode %}

### `CafLivenessConfig` Parameters

| Parameter         | Default | Description                                                                                                                                                         |
| ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `mobileToken`     | —       | **Required.** The `sessionToken` your backend requested for this attempt. See [Backend Setup](https://docs.caf.io/sdk-lite/setup/backend/setup.md).                 |
| `personId`        | —       | **Required.** Identifies the user for the liveness process.                                                                                                         |
| `stage`           | `PROD`  | Target environment: `PROD`, `BETA`, or `DEV`.                                                                                                                       |
| `showLoading`     | `true`  | Shows loading indicators during processing when **true**.                                                                                                           |
| `enableSecurity`  | `true`  | When **enabled**, runs device security validation before and during liveness. See [Security Checks](https://docs.caf.io/sdk-lite/setup/android/security-checks.md). |
| `uiCustomization` | `null`  | Visual branding for the liveness screen. See [UI Customization](https://docs.caf.io/sdk-lite/setup/android/configuration/ui-customization.md).                      |

## Pre-warm

Call `prewarm()` as soon as you know Liveness is coming — when the user reaches the screen before it, or when the "Start" button becomes visible. The SDK creates the Liveness session in the background so `startLiveness()` does not have to wait for it.

```kotlin
CafFaceLivenessLite.instance.prewarm()
```

`prewarm()` takes no parameters, returns immediately and reports nothing — the work continues in the background. It requires `configure()` to have been called first.

{% hint style="info" %}
`prewarm()` is optional. If you skip it, `startLiveness()` creates the session itself and everything works exactly the same — just without the speed gain.
{% endhint %}

{% hint style="warning" %}
The pre-warmed session is valid for 90 seconds and is used at most once. If the window passes, or the session was already used by a previous `startLiveness()`, a fresh one is fetched automatically.
{% endhint %}

## Start Liveness

Call `startLiveness()` with a callback to handle the results and events triggered during the Liveness flow.

```kotlin
CafFaceLivenessLite.instance.startLiveness { event ->
    when (event) {
        is LivenessLiteEvent.Success -> {
            // Handle Success
        }
        is LivenessLiteEvent.Failure -> {
            // Handle Failure
        }
        is LivenessLiteEvent.Error -> {
            // Handle Error
        }
        LivenessLiteEvent.Cancelled -> {
            // The user abandoned the flow
        }
        LivenessLiteEvent.Loading -> {
            // Show a loading indicator
        }
        LivenessLiteEvent.Loaded -> {
            // Hide the loading indicator
        }
    }
}
```

{% hint style="info" %}
If a pre-warm is still running when `startLiveness()` is called, the SDK waits for it rather than starting a second request — so calling `prewarm()` shortly before is never wasted.
{% endhint %}

## Complete example

```kotlin
class SelfieIntroFragment : Fragment() {

    private val livenessConfig = CafLivenessConfig(
        mobileToken = "<YOUR_MOBILE_TOKEN>",
        stage = CafStage.PROD,
        personId = "<PERSON_ID>",
        showLoading = true,
        enableSecurity = true
    )

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // The user is on the screen before Liveness: prepare the session now.
        CafFaceLivenessLite.instance.configure(requireContext(), livenessConfig)
        CafFaceLivenessLite.instance.prewarm()

        startButton.setOnClickListener {
            CafFaceLivenessLite.instance.startLiveness { event ->
                // Handle events
            }
        }
    }
}
```

## Events

Every run delivers its events to the listener passed to `startLiveness()`, on the main thread. A run always ends with exactly one of `Success`, `Failure`, `Error` or `Cancelled`.

| Event                                                                        | When it is delivered                                                                                                                                                                               |
| ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Success(signedResponse: String)`                                            | The capture completed. Forward the `signedResponse` to your backend.                                                                                                                               |
| `Failure(signedResponse: String, type: CafFailureType, description: String)` | The capture ran but did not pass. Guide the user with the `type` and forward the `signedResponse` to your backend. See [Mobile Failures](https://docs.caf.io/sdk-lite/failure-handling/mobile.md). |
| `Error(type: CafLivenessErrorType, description: String, cause: Throwable)`   | The run could not start or finish. No `signedResponse`. See [Android Errors](https://docs.caf.io/sdk-lite/error-handling/android.md).                                                              |
| `Cancelled`                                                                  | The user closed the capture screen. Go back to the previous screen without an error message.                                                                                                       |
| `Loading` / `Loaded`                                                         | The SDK started / stopped waiting. Use them to show or hide your own loading indicator.                                                                                                            |

{% hint style="warning" %}
Never decide the liveness outcome on the device. Your backend validates the `signedResponse` and retrieves the final result.
{% endhint %}

## Changing the user or credentials

If you call `configure()` again with a different `mobileToken` or `personId`, any pre-warmed session is discarded, since it belongs to the previous user. Call `prewarm()` again after reconfiguring.

## Releasing the SDK

`release()` cancels the Liveness in progress and discards any pre-warmed session. The configuration from `configure()` is kept, so you can call `prewarm()` or `startLiveness()` again without reconfiguring.

```kotlin
CafFaceLivenessLite.instance.release()
```

## Debugging

To see the SDK logs in Logcat, enable logging. Logs only appear when your app is debuggable; in release builds the flag is ignored.

```kotlin
CafFaceLivenessLite.isLogEnabled = true
```

## Next step

Forward the `signedResponse` to your backend, which validates it and retrieves the result: [Backend Setup](https://docs.caf.io/sdk-lite/setup/backend/setup.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.caf.io/sdk-lite/setup/android/configuration/starting-liveness-flow.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
