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

# gRPC integration

> **Note**
>
> 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](/applications/ios-how-to-integrate-the-sdk-in-your-application-v3) or [Android](/applications/android-how-to-integrate-the-sdk-in-your-application-v3) documentation.

You can integrate the SDK into a [gRPC](https://grpc.io/) 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:

> **Note**
>
> Don't forget to replace `<APP_ID>` with your actual **Application ID**.

#### Android

**`Kotlin (MainApplication.kt)`**

```kotlin title="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}")
        }
    }
}
```

#### iOS

**`Swift (AppDelegate.swift)`**

```swift title="Swift (AppDelegate.swift)"
import HUMAN

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        startHumanSDK()
        return true
    }

    func startHumanSDK() {
        do {
            let policy = HSPolicy()
            policy.automaticInterceptorPolicy.interceptorType = .none

            try HumanSecurity.start(appId: "<APP_ID>", policy: policy)
        }
        catch {
            print("Error: \(error)")
        }
    }
}
```

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](/applications/sdk/v5/getting-started#handle-multiple-application-ids) 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](#handle-blocked-requests) for more information.

Below are example implementations for Android and iOS:

#### Android

**`Kotlin`**

```kotlin title="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`...
}
```

#### iOS

**`Swift`**

```swift title="Swift"
import HUMAN

func sendUrlRequest() {
    let headers = HumanSecurity.BD.headersForURLRequest(forAppId: "<APP_ID>")
    var callOption = CallOptions()
    for (key, value) in headers {
        callOption.customMetadata.replaceOrAdd(name: key, value: value)
    }
    // send you gRPC request with `callOption`...
}
```

## 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:

```json
{
  "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/handleBlockResponse` function. If not, the SDK won't present a challenge to the user.