gRPC integration
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.
Android
iOS
In particular, note the following:
- Initialization: We start the SDK as early as possible on the main thread.
- Policy configuration: We create an
HSPolicyinstance and set theHSAutomaticInterceptorPolicy/interceptorTypeproperty toHSAutomaticInterceptorType/none. That disables Automatic Interception, which lets the SDK manipulate URL requests and handle their responses, but isn’t supported in gRPC. - Starting the SDK: We call
HumanSecurity/start(appId:policy:)with the following parameters:- The
Applicationinstance (Android only) - Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The
HSPolicyobject that we configured
- The
Send your requests through the SDK and check for a blocked request
- 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.
- 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:
Android
iOS
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:
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.