React Native + Axios

New Architecture

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

Find documentation for previous major version of the SDK on the v2 documentation page

Since version 4.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 v3 page, 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-axios @datadome/react-native-core-datadome

Or, with Yarn:

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

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

cd ios && pod install

Configuration

Initialize the DataDome instance

The DataDome instance stores the DataDome cookie and shows the challenges. You do not need to add a component to your views.

To identify the traffic, DataDome needs your client-side key. Get the key from the Dashboard. Then give it to the DataDome instance:

import {
  DataDome,
  DataDomeInterceptor,
} from '@datadome/react-native-datadome-axios';

const datadome = new DataDome({ 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.

Attach the DataDomeInterceptor

  1. Create a DataDomeInterceptor instance with your DataDome instance:

    const dataDomeInterceptor = new DataDomeInterceptor(datadome);
  2. Attach the DataDomeInterceptor to your Axios instance. Attach it before you add other interceptors:

    dataDomeInterceptor.attach(axios);

    The interceptor then protects every request that the Axios instance sends. It adds the DataDome cookie to each request, and it checks each response.

Advanced: use a custom adapter

Make your adapter compatible with DataDome

⚠️

Skip this section if you use the default Axios adapter, or the built-in fetch or xhr adapter. These adapters already meet the two requirements.

In Axios, the adapter is the function that sends the HTTP request and builds the response. DataDome wraps your adapter:

  • Before each attempt, DataDome adds its cookie to the request headers.
  • After each attempt, DataDome checks the response for a challenge.

Your adapter must meet two requirements for this to work.

Requirement 1: Send the request headers that Axios gives you

DataDome adds its cookie to config.headers before it calls your adapter. Your adapter must send these headers. If your adapter builds its own headers without the DataDome cookie, the retry after a solved challenge does not have the cookie. DataDome then challenges the request again, and the user sees a new challenge every time.

If your adapter sets its own Cookie header, add your cookies to the existing value. Do not replace the existing value, because it contains the DataDome cookie.

Wrong implementation:

// Don't: this builds new headers and leaves out the DataDome cookie
headers: { Authorization: `Bearer ${token}` },

Correct implementation:

import { AxiosHeaders } from 'axios';

// send config.headers, and add your own headers on top if you need any
headers: {
  ...AxiosHeaders.from(config.headers).toJSON(true),
  'X-Custom-Header': 'value',
},

Requirement 2: Return the responses as Axios does

DataDome reads the challenge from the response that your adapter returns. Your adapter must:

  • Resolve with an Axios response for a successful status.
  • Reject with an AxiosError that contains the response for an error status. This includes the 403 response of a DataDome challenge.

If your adapter rejects a 403 response with a different error type, or with an AxiosError without a response, DataDome cannot see the challenge. The request then fails, and the SDK does not show a challenge.


Did this page help you?