React Native + Fetch

New Architecture

The React Native Fetch SDK protects the requests that your React Native application sends with the Fetch API.

Since version 3.0.0, the SDK uses the CoreDataDome SDK for React Native. The CoreDataDome SDK manages the native code, the cookie storage, and the challenge display.

If you use an older version, read the migration guide to upgrade.

The documentation for previous major versions is on the v2 page and the v1 page.

Requirements

  • React Native: 0.85+, with the New Architecture enabled (the default)
  • iOS: deployment target 15.1+
  • Android: minSdkVersion 24+

Installation

Add the SDK and its dependency, the CoreDataDome SDK, to your project:

npm install @datadome/react-native-datadome-fetch @datadome/react-native-core-datadome

Or, with Yarn:

yarn add @datadome/react-native-datadome-fetch @datadome/react-native-core-datadome

On iOS, install the pods to link the native SDK:

cd ios && pod install

Configuration

Initialize the DataDome instance

  1. Create a configuration object:

    import { DataDome, type DataDomeConfiguration } from '@datadome/react-native-datadome-fetch';
    
    const config: DataDomeConfiguration = {
      clientKey: 'YOUR_CLIENT_KEY',
    };
    PropertyTypeDescription
    clientKeystringRequired: Your DataDome client-side key.
    domainstring | nullOptional: The origin URL that the instance applies to, for example https://example.com.
  2. Create the DataDome instance:

    const dataDome = new DataDome(config);

If you create a second DataDome instance with the same configuration, the SDK uses the same native instance again.

Create the protectedFetch function

createDataDomeFetch returns a drop-in replacement for the Fetch API:

import { createDataDomeFetch } from '@datadome/react-native-datadome-fetch';

const protectedFetch = createDataDomeFetch(dataDome);

Usage

Use this function as you use the Fetch API:

const response = await protectedFetch('https://api.example.com/items', {
  method: 'GET',
  headers: { Accept: 'application/json' },
});

For each request, the SDK does these steps:

  1. It adds headers to the request. These headers include the datadome cookie.
  2. It sends the request with the built-in Fetch API.
  3. It validates the response and saves the datadome cookie. If DataDome challenges the request, the SDK shows the challenge to the end user (for example Slider, Device Check, or Block page).
  4. After the end user solves the challenge, the SDK retries the request. Then it returns the response to the application.

Did this page help you?