Expo Integration
You can integrate the SDK into an Expo project. This article explains how to integrate and implement the HUMAN Expo SDK wrapper into an Expo app.
Integrate the HUMAN Expo Module
To integrate the HUMAN SDK into your Expo app, follow these steps:
Add the plugin
Without this step, Android builds will fail to resolve the HUMAN SDK dependency.
The config plugin is available starting from version 1.0.8 and later. If you are using an earlier version (1.0.2 – 1.0.6), skip this step and continue to Step 3.
Add the plugin to your Expo configuration (app.json or app.config.js) so the Android dependency repository is automatically configured:
Run prebuild
Generate the native project files:
For custom or bare workflows (no expo prebuild)
If your project does not use expo prebuild, you must manually add the HUMAN Maven repository so Android can resolve the SDK dependencies.
Add the following line everywhere you define repositories, such as in settings.gradle, build.gradle, or module-level Gradle files:
The config plugin automatically adds this repository during npx expo prebuild. In custom or bare workflows, this must be added manually to ensure proper Android dependency resolution.
Start the SDK
When starting the SDK, start the SDK as soon as possible, and only once. Call startWithAppId in your Expo main entry point (index.tsx) or where you register and initialize one-time services. Start the SDK outside of any UI component to avoid lifecycle issues. If your app sends a URL request to your server before the SDK starts, the attached headers won’t include the latest updated tokens. As a result:
- The Enforcer may block the request, and
- The SDK will not be able to present a challenge to the user
Below is an example implementation:
Don’t forget to replace <APP_ID> with your actual Application ID.
In particular, note the following:
- Initialization: We start the SDK as early as possible and only once. You can move this code to a different function or file and export it as long as it’s only called once.
- Policy configuration: An optional
HSPolicyclass can configure the SDK’s behavior. This object defines settings such ashybridAppPolicy.webRootDomains. This class has two settings:hybridAppPolicy: Use this if your app operates in hybrid mode, meaning it has a WebView that loads a site that’s also protected by HUMAN and running its sensor. Configure all relevant website domains with the application ID. Hybrid is supported for Expo WebView whenwebRootDomainsis set. Set each domain to the bare registrable root (example.com). A leading dot is accepted and equivalent. Do not listwwwor a subdomain. The Expo module turnssupportExternalWebViewson. See WebView container support for more information.detectionPolicy: This setting lets you configure whether the SDK can send touch and motion events, either touch only or both touch and motion, to the server to improve detection. By default, it’s off, and you can choose to enable it.
- Starting the SDK: We call
startWithAppIdwith the following parameters:- Your Application ID. If you have multiple IDs, see our guide on how to handle them.
- The policy object, if needed.
Integrate with Bot Defender
Send your requests through the SDK and check for a blocked request
- Your application adds the SDK’s HTTP headers to the URL request. 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. Each request should call the HumanSecurity module so the headers are not reused from a previous call.
- Your application sends the URL request.
- If the response is blocked, pass the response body, status code, and URL to the SDK. Call
canHandleResponse. If it returns true, callhandleResponseto present the challenge. When usingaxios, you may need to callJSON.stringify(error.response?.data)on the response body before passing it and handle the blocked response in thecatchblock becauseaxiostreats it as a failed request. handleResponseresolves toHSBotDefenderChallengeResult:SOLVED: The user successfully completed the challenge.CANCELLED: The user dismissed or failed to complete the challenge.FAILED: The SDK was unable to handle the response, usually indicating that something wasn’t passed correctly tohandleResponse.
- You can await
handleResponse, which waits until the user solves the challenge. You can also continue your flow and handle the result in the.thenblock.
Below are example implementations using the Fetch API or axios:
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 entire JSON as response in a string format, along with the status code and URL, to HumanSecurity.BD.handleResponse. If not, the SDK won’t present a challenge to the user.
When handling blocked requests:
- Treat blocked requests as a failure. Your app’s UI is shown again after the user solves or cancels the challenge. If the request was triggered by a user action, make it clear that the user may try the same action again.
- Use the promise result to log analytics, create logs, and so on.
- (Optional) Use the returned promise to retry the original request when appropriate. Consider the following:
- Promise scope: The promise returns outside the original request’s scope. Ensure your app handles this case correctly.
- Challenge cancellation: The user might cancel the challenge. Do not retry the request to avoid it being blocked again.
- Retry block possibility: The retry attempt may also be blocked. Do not assume it will succeed.
Set custom parameters (optional)
Custom parameters are additional custom values that you can set yourself to enrich the data you receive from HUMAN. You can have up to 10 custom parameters, and they must follow the pattern custom_paramN, where N is a number between 1 and 10. Call HumanSecurity.BD.setCustomParameters only after startWithAppId has been called.
Below is an example implementation:
Subscribe to events (optional)
Bot Defender provides several delegates, or callbacks, to notify about events related to headers and challenges. These callbacks are mainly intended for analytics and statistics rather than for taking specific actions. In the Expo library, these callbacks work as events.
To listen to an event, add a listener for it. Remove the listener when leaving the context to avoid a memory leak.
Integrate with Account Defender
Enable Account Defender in your app
To enable Account Defender, set the UserID of your current logged-in user in the SDK. When the user logs out, call the function with null to clear the user ID.
Below is an example implementation:
Notify HUMAN's backend on outgoing URL requests from the app
For Account Defender to protect the user’s account, your app has to provide the SDK with outgoing URL requests. Do this after you set the user ID. Call HumanSecurity.AD.registerOutgoingUrlRequest(url: string, appId?: string) before you send the URL request. If you use a custom HTTP client, add this function to the request interceptor.
Below is an example implementation:
Set additional data (optional)
Additional data are additional custom values that you can set yourself to enrich the data you receive from HUMAN. These are separate from custom parameters and are unique to Account Defender. Call HumanSecurity.AD.setAdditionalData(parameters: { [key: string]: string }, appId?: string) only after HumanSecurity.startWithAppId(appId: string, policy?: HSPolicy) has been called.
Below is an example implementation:
HUMAN Expo Mobile API reference
The HUMAN Security Expo Mobile API offers the following functions:
Simulate a challenge
You can simulate a challenge to test the integration. To do so, follow the steps in our help article.