> This page is for Applications Protection.

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.humansecurity.com/llms.txt.

# Callbacks from the SDK

#### v2.x

The SDK provides callbacks that will help you to handle events from it. There are 4 types of events:

1. The SDK is ready.
2. A request was blocked.
3. A challenge was solved by the user.
4. A challenge was cancelled by the user.

## The SDK is ready

This event means that the SDK was finished the start process successfully. When calling the `PerimeterX/start(appId:delegate:enableDoctorCheck:completion:)` function, you should provide a completion handler. Once the handler is called with `success == true` you know that the SDK is ready. In addition, you can register to this event in other places within your code, using the `PerimeterX/addInitializationFinishedCallback(forAppId:callback:)` function. Here is an example:

```kotlin
PerimeterX.INSTANCE.addInitializationFinishedCallback(null) {
    println("PerimeterX is ready")
}
```

```java
PerimeterX.INSTANCE.addInitializationFinishedCallback(null, () -> {
    Log.i("tag", "PerimeterX is ready");
    return null;
});
```

You can call the `PerimeterX/addInitializationFinishedCallback(forAppId:callback:)` when the SDK is already ready. In this case, the callback will be called immediately.

## A request was blocked

This event occurred when a request was blocked by HUMAN. Notice that this event is only for native URL requests. URL requests which are origin from web views won't trigger this event.

There are two ways to receive this event:

1. HUMAN's delegate - The `delegate` object that conform to the `PerimeterXDelegate` protocol should implement the `PerimeterXDelegate/perimeterxDidRequestBlocked(forAppId:)` function.
2. Register to the event - you can register to this event in other places within your code, using the `PerimeterX/registerCallbackForRequestBlockedEvent(forAppId:callback:)` function. This function returns a Registration ID which can be used to unregister from this event with the `PerimeterX/unregisterCallbackForRequestBlockedEvent(forAppId:registrationId:)`. Here is an example:

```kotlin
val requestBlockedEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForRequestBlockedEvent(null) {
	println("Request Blocked Event")
}
```

```java
String requestBlockedEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForRequestBlockedEvent(null, () -> {
	Log.i("tag", "Request Blocked Event");
	return null;
});
```

## A challenge was solved by the user

This event occurred when a challenge was solved by the user. Notice that this event is only for native URL requests. URL requests which are origin from web views won't trigger this event.

There are two ways to receive this event:

1. HUMAN's delegate - The `delegate` object that conform to the `PerimeterXDelegate` protocol should implement the `PerimeterXDelegate/perimeterxDidChallengeSolved(forAppId:)` function.
2. Register to the event - you can register to this event in other places within your code, using the `PerimeterX/registerCallbackForChallengeSolvedEvent(forAppId:callback:)` function. This function returns a Registration ID which can be used to unregister from this event with the `PerimeterX/unregisterCallbackForChallengeSolvedEvent(forAppId:registrationId:)`. Here is an example:

```kotlin
val challengeSolvedEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForChallengeSolvedEvent(null) {
	println("Challenge Solved Event")
}
```

```java
String challengeSolvedEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForChallengeSolvedEvent(null, () ->
	Log.i("tag", "Challenge Solved Event");
	return null;
});
```

## A challenge was cancelled by the user

This event occurred when a challenge was cancelled by the user. Notice that this event is only for native URL requests. URL requests which are origin from web views won't trigger this event.

There are two ways to receive this event:

1. HUMAN's delegate - The `delegate` object that conform to the `PerimeterXDelegate` protocol should implement the `PerimeterXDelegate/perimeterxDidChallengeCancelled(forAppId:)` function.
2. Register to the event - you can register to this event in other places within your code, using the `PerimeterX/registerCallbackForChallengeCancelledEvent(forAppId:callback:)` function. This function returns a Registration ID which can be used to unregister from this event with the `PerimeterX/unregisterCallbackForChallengeCancelledEvent(forAppId:registrationId:)`. Here is an example:

```kotlin
val challengeCancelledEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForChallengeCancelledEvent(null) {
	println("Challenge Cancelled Event")
}
```

```java
String challengeCancelledEventRegistrationId = PerimeterX.INSTANCE.registerCallbackForChallengeCancelledEvent(null, () -> {
	Log.i("tag", "Challenge Cancelled Event");
	return null;
}];
```