For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
HUMAN DashboardHUMAN WebsiteRequest a Demo
Product GuidesEnforcer GuidesMobile SDKAPI ReferenceCustomer support
Product GuidesEnforcer GuidesMobile SDKAPI ReferenceCustomer support
    • Overview
  • Mobile SDK (for Android, iOS, and visionOS)
    • Android changelog
    • iOS, iPadOs, & visionOS changelog
    • Expo changelog
    • React Native Wrapper changelog
  • iOS SDK
    • What's New
    • How to test your app with the SDK
  • Android SDK
    • What's New
      • How to integrate the SDK in your application
      • How to verify the integration in your app with the SDK
      • Manual integration
      • Handle block responses from the server
      • How to retry requests that were blocked
      • Multiple AppIDs support
      • Hybrid App support
      • Enable support for Account Defender
      • Integration with React Native
      • Integration with Flutter
      • Integration with Ionic
      • Integration with gRPC
      • Migrating SDK from v2 to v3
    • How to test your app with the SDK
LogoLogo
Login
Login
HUMAN DashboardHUMAN WebsiteRequest a Demo
On this page
  • v3.0
  • Start the SDK
  • Create Native Module
  • Pass the SDK’s HTTP headers to the JavaScript code
  • Handle the block response
Android SDKv3

Integration with React Native

Was this page helpful?
Previous

Integration with Flutter

Next
Built with

v3.0

The SDK can be integrated into React Native projects.

Start the SDK

The automatic interceptor is not supported in React Native, so any request from the JavaScript code has to be handled manually. Here is an example:

1PXPolicy policy = new PXPolicy();
2policy.setUrlRequestInterceptionType(PXPolicyUrlRequestInterceptionType.NONE);
3try {
4 PerimeterX.INSTANCE.start(this, "<APP_ID>", this, policy);
5}
6catch (Exception exception) {
7 Log.e("tag","failed to start. error: " + exception.getMessage());
8}

Create Native Module

Create a native module which called PerimeterXModule, as described here.

Pass the SDK’s HTTP headers to the JavaScript code

Create a function that pass the HTTP headers from the SDK to the JavaScript code. Here is an example:

1@ReactMethod
2public void getHTTPHeaders(Promise promise) {
3 JSONObject json = new JSONObject(PerimeterX.INSTANCE.headersForURLRequest(null));
4 promise.resolve(json.toString());
5}

In your JavaScript code, call this function for every URL request to add those HTTP headers. Here is an example:

1const headers = await PerimeterXModule.getHTTPHeaders();
2const obj = JSON.parse(headers);
3const url = 'https://my.request.com'
4const response = await fetch(url, {
5 method: 'GET',
6 headers: obj,
7});

If you wish to reduce the number of times there is a bridge between the JavaScript and native, you can add a listener that will be called when the SDK’s headers were changed. Than, you pass those new headers to the JavaScript side. You may cache those headers for future requests, but you must make sure to update them.

In the Application, implement the PerimeterXDelegate/perimeterxHeadersWereUpdated(headers:forAppId:) function. You should pass those headers to your native module.

1@Override
2public void perimeterxHeadersWereUpdated(@NonNull HashMap<String, String> hashMap, @NonNull String s) {
3 if (perimeterxModule != null) {
4 perimeterxModule.handleUpdatedHeaders(hashMap);
5 }
6}

In the native module, implement the handleUpdatedHeaders function. You should send the event ("pxNewHeaders") to the JavaScript side.

1public void handleUpdatedHeaders(HashMap<String, String> headers) {
2 if (!this.getReactApplicationContext().hasCatalystInstance()) {
3 return;
4 }
5 JSONObject json = new JSONObject(headers);
6 this.getReactApplicationContext()
7 .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
8 .emit("pxNewHeaders", json.toString());
9}

In your JavaScript code, listen to this event.

  1. Import the native module.

    1import {NativeModules, NativeEventEmitter} from 'react-native';
    2import type {Message} from 'react-native/Libraries/LogBox/Data/parseLogBoxLog';
    3const {PerimeterXModule} = NativeModules;
  2. Create new NativeEventEmitter.

    1const pxEventEmitter = new NativeEventEmitter(PerimeterXModule);
  3. Add listener to the event and store the SDK’s headers in variable.

    1var pxHeader = null;
    2
    3const onAddNewHeaders = headers => {
    4 const obj = JSON.parse(headers);
    5 console.log(`[PX] got new px headers from event: ${JSON.stringify(obj)}`);
    6 pxHeader = obj;
    7};
    8
    9const subscriptionPxNewHeaders = pxEventEmitter.addListener(
    10 'PxNewHeaders',
    11 onAddNewHeaders,
    12);
  4. Add those headers to your URL request.

    1const url = 'https://my.request.com'
    2const response = await fetch(url, {
    3 method: 'GET',
    4 headers: pxHeader,
    5});
Headers must be included in all your URL requests

Those SDK’s headers must be included in all your URL requests. Sending requests without them could affect the user experience.

Handle the block response

You have to provide the SDK with the response. After receiving an error in the response, pass the information to SDK:

1const result = await PerimeterXModule.handleResponse(
2 JSON.stringify(json),
3 response.status,
4 url,
5);
6/*
7check the result:
8 'false' - not handled by the SDK
9 'solved' - challenge solved
10 'cancelled' - challenge cancelled
11*/
12if (result === 'solved') {
13 // challenge solved. you may retry the request here
14} else if (result === 'false') {
15 // request finished successfully
16} else if (result === 'cancelled') {
17 // challenge cancelled
18}