Skip to navigation

Ionic Integration

Learn how to integrate the HUMAN Mobile SDK into an Ionic app.

This guide is relevant for the current Mobile SDK. While we highly recommend using the latest version, if you are using a legacy integration, refer to the iOS or Android documentation.

You can integrate the SDK into an Ionic project. There is no HUMAN Ionic package. Call the native SDK from JavaScript through a Capacitor plugin.

Prerequisites

If the app loads a HUMAN-protected site in a Capacitor WebView, set hybridAppPolicy.webRootDomains and hybridAppPolicy.supportExternalWebViews = true in native start. There is no wrapper-level default. Instead, the flag is set manually. See WebView container support for more information.

Start the SDK

When starting the SDK, keep the following in mind:

  1. Start the SDK as soon as possible on the main thread. We recommend doing so in the Application’s onCreate function on Android or the AppDelegate’s didFinishLaunchingWithOptions function on iOS. If your app sends a URL request to your server before the SDK starts, the request will not include the SDK’s HTTP headers. As a result:
    • The Enforcer may block the request, and
    • The SDK will not be able to present a challenge to the user
  2. Create a HumanManager that handles calls from Ionic.

Below are example implementations for Android and iOS:

Don’t forget to replace <APP_ID> with your actual Application ID.

import android.app.Application;
public class MainApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
HumanManager.startHumanSDK(this);
}
}

In particular, note the following:

  1. Initialization: We start the SDK as early as possible on the main thread.
  2. Policy configuration: We create an HSPolicy instance and set the HSAutomaticInterceptorPolicy/interceptorType property to HSAutomaticInterceptorType/none. That disables Automatic Interception, which lets the SDK manipulate URL requests and handle their responses, but isn’t supported in Ionic.
  3. Starting the SDK: We call HumanSecurity/start(appId:policy:) with the following parameters:
    • The Application instance (Android only)
    • Your Application ID. If you have multiple IDs, see our guide on how to handle them.
    • The HSPolicy object that we configured
  4. Send the response: The response is sent from the Ionic to the native side.
  5. Handle the response: The native side calls the HSBotDefender/handleResponse(response:data:forAppId:callback:) function. This function presents a challenge to the user when the provided response is a blocked response.

Send your requests through the SDK and check for a blocked request

  1. Export the HumanManager plugin.
  2. Add the SDK’s HTTP headers to your URL requests.
  3. When a request is blocked, send the block response to the SDK.
  4. Handle the challenge’s result. See handle blocked requests for more information.

Below is an example implementation:

JavaScript
import { registerPlugin } from '@capacitor/core';
export interface HumanPlugin {
getHttpHeaders():Promise<{ value: string }>;
handleResponse(value : any):Promise<{ value: string }>;
}
const Human = registerPlugin<HumanPlugin>('HUMAN');
const sendUrlRequest = async () => {
const result = await Human.getHttpHeaders()
const json = JSON.parse(JSON.stringify(result))
const map = new Map(Object.entries(json))
var xhr = new XMLHttpRequest()
xhr.addEventListener('load', async () => {
const challengeResult = await Human.handleResponse({ "value": xhr.responseText })
const challengeResultJson = JSON.parse(JSON.stringify(challengeResult))
const challengeResultMap = new Map(Object.entries(challengeResultJson))
console.log("challenge result = ", challengeResultMap.get("value"))
})
xhr.open('GET', 'https://example.com')
for (const [key, value] of map) {
xhr.setRequestHeader(key, value as string)
}
xhr.send()
};

Handle blocked requests

When the Enforcer blocks a request, it returns a JSON string in the response body with an HTTP status code of 403, which includes metadata for the SDK. For example:

{
"vid": "928d7ab3-9cf1-11ee-a624-b802520f369f",
"uuid": "fd01e6d6-9cf2-11ee-808c-acde48001122",
"page": "...",
"appId": "PXj9y4Q8Em",
"action": "captcha",
"collectorUrl": "https://collector-pxj9y4q8em.perimeterx.net"
}

Your app should pass the whole JSON to the SDK via the HSBotDefender/handleResponse(response:data:callback:) function. If not, the SDK won’t present a challenge to the user.