Skip to navigation

gRPC integration

Learn how to integrate the HUMAN Mobile SDK into a gRPC 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 a gRPC project.

Start the SDK

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

Below are example implementations for Android and iOS:

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

Kotlin (MainApplication.kt)
import android.app.Application
import com.humansecurity.mobile_sdk.HumanSecurity
import com.humansecurity.mobile_sdk.main.policy.HSPolicy
import com.humansecurity.mobile_sdk.main.policy.HSAutomaticInterceptorType
class MainApplication: Application() {
override fun onCreate() {
super.onCreate()
startHumanSDK()
}
private fun startHumanSDK() {
try {
val policy = HSPolicy()
policy.automaticInterceptorPolicy.interceptorType = HSAutomaticInterceptorType.NONE
HumanSecurity.start(this, "<APP_ID>", policy)
}
catch (exception: Exception) {
println("Exception: ${exception.message}")
}
}
}

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

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

  1. Add the SDK’s HTTP headers to your URL requests. Include them on every request, and do not cache them. They contain a token with an expiration date, and the SDK keeps that token up to date.
  2. When a request is blocked, the SDK handles the response and presents a challenge. See handle blocked requests for more information.

Below are example implementations for Android and iOS:

Kotlin
import com.humansecurity.mobile_sdk.HumanSecurity
fun sendRequest() {
val headers = HumanSecurity.BD.headersForURLRequest("<APP_ID>")
val metadata = Metadata()
for ((key, value) in headers) {
metadata.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value)
}
// send you gRPC request with `metadata`...
}

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.