# Connecting SDK to API

To connect SDK to Oz API, specify the API URL and [access token](https://doc.ozforensics.com/oz-knowledge/guides/developer-guide/api/oz-api/use-cases/authentication) as shown below.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
OzLivenessSDK.setApiConnection(OzConnection.fromServiceToken(host, token))
```

{% endtab %}

{% tab title="Java" %}

```java
OzLivenessSDK.INSTANCE.setApiConnection(
        OzConnection.Companion.fromServiceToken(host, token), 
        null
);
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Please note:&#x20;

* In your host application, it is recommended that you set the API address on the screen that precedes the liveness check. Setting the API URL initiates a service call to the API, which may cause excessive server load when being done at the application initialization or startup. We recommend calling the `setApiConnection` method once, for example, in the `Application` class.
* The order of SDK initialization and API connection does not matter, but both methods must be finished successfully before invoking the `createStartIntent` method.
  {% endhint %}

Alternatively, you can use the login and password provided by your Oz Forensics account manager:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
OzLivenessSDK.setApiConnection(
    OzConnection.fromCredentials(host, username, password),
    statusListener(
        { token -> /* token */ },
        { ex -> /* error */ }
    )
)
```

{% endtab %}

{% tab title="Java" %}

```java
OzLivenessSDK.INSTANCE.setApiConnection(
        OzConnection.Companion.fromCredentials(host, username, password),
        new StatusListener<String>() {
            @Override
            public void onStatusChanged(@Nullable String s) {}
            @Override
            public void onSuccess(String token) { /* token */ }
            @Override
            public void onError(@NonNull OzException e) { /* error */ }
        }
);
```

{% endtab %}
{% endtabs %}

Although, the preferred option is authentication via access token – for security reasons.

By default, logs are saved along with the analyses' data. If you need to keep the logs distinct from the analysis data, set up the separate connection for [telemetry](https://doc.ozforensics.com/oz-knowledge/other/faq#what-is-telemetry-and-why-should-i-use-it) as shown below:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
OzLivenessSDK.setEventsConnection(
    OzConnection.fromCredentials(
        "https://echo.cdn.ozforensics.com/",
        "<your_telemetry_user_eg_tm@company.com>",
        "your_telemetry_password"
    )
)
```

{% endtab %}

{% tab title="Java" %}

```java
OzLivenessSDK.setEventsConnection(
        OzConnection.fromCredentials(
                "https://tm.ozforensics.com/",
                "<your_telemetry_user_eg_tm@company.com>",
                "your_telemetry_password"
        )
);
```

{% endtab %}
{% endtabs %}

Clearing authorization:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
OzLivenessSDK.setApiConnection(null)
```

{% endtab %}

{% tab title="Java" %}

```java
OzLivenessSDK.INSTANCE.setApiConnection(null, null);
```

{% endtab %}
{% endtabs %}

### Other Methods

Check for the presence of the saved Oz API access token:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
val isLoggedIn = OzLivenessSDK.isLoggedIn
```

{% endtab %}

{% tab title="Java" %}

```java
boolean isLoggedIn = OzLivenessSDK.INSTANCE.isLoggedIn();
```

{% endtab %}
{% endtabs %}

LogOut:

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
OzLivenessSDK.logout()
```

{% endtab %}

{% tab title="Java" %}

```java
OzLivenessSDK.INSTANCE.logout();
```

{% endtab %}
{% endtabs %}
