# React Native SDK quick start

This guide describes how to integrate Fraud Prevention into your React Native application. It helps you set up the Mosaic SDK wrapper for React Native, enabling you to enhance your mobile applications' security with Fraud Prevention services.

Note
Client-side integrations are recommended for POCs and testing. For production environments, consider implementing [Backend integration](/guides/risk/quick_start_backendapi). Learn more about integration options: [Client-side integration vs Backend integration](/guides/risk/integration_clientside_vs_backend).

## How it works

The flow starts with the user navigating to the app (1). The SDK gets initialized and starts sending telemetry to Mosaic (2). When a user performs an action, for example, clicks a login button (3), the SDK triggers an action event (4) and obtains an action token (5) which then forwards to the backend (6). Having received an action token, the application backend uses it to fetch recommendation from Mosaic (7 & 8) and instructs the client to act accordingly (9) in order to complete the login procedure (10). Upon successful login, the client sets the user (11).

![](/assets/drs-integrations.88751400966c093ef32f43c3a699d31f766f7ff46b9053f2fbaa898cf39860b5.e95a590b.png)

## Step 1: Get client credentials

div
div
Admin Portal
Client credentials are used to identify your app and generate access tokens for authorizing Mosaic requests. To obtain them, you'll need to create an application in the [Admin Portal](https://portal.transmitsecurity.io/) (if you don't have one yet).

1. From **Applications**, click **Add application**.
2. Add the friendly application name to display in the Admin Portal.
3. Add an OIDC client, specify the client secret as an authentication method, and your website URL as a redirect URI (e.g., `https://your-domain.com`).


Note
These fields are required for all Mosaic apps, but won't be used for Fraud Prevention.

1. Click **Add** to create your application. This will automatically generate your client credentials.


## Step 2: Install the wrapper

div
div
Client
Install the Mosaic SDK wrapper for React Native:


```bash
npm install react-native-ts-accountprotection
```

## Step 3: Configure the wrapper

div
div
Client
Android
### Update build.gradle

In your `app/build.gradle` file, add the following under `repositories`:


```gradle
repositories {
  google()
  maven {
    url 'https://transmit.jfrog.io/artifactory/transmit-security-gradle-release-local/'
  }
}
```

Then, add the following under `dependencies`:


```gradle
dependencies {
  implementation "com.ts.sdk:accountprotection:2.1.+"
}
```

### Update strings.xml

Update your `strings.xml` file by adding the following:


```xml
<resources>
    <!-- Mosaic Credentials -->
    <string name="transmit_security_client_id">"CLIENT_ID"</string>
    <string name="transmit_security_base_url">https://api.transmitsecurity.io/</string>
</resources>
```

Note
Configure the SDK to work with a different cluster by setting the base URL to https://api.eu.transmitsecurity.io (for EU), https://api.ca.transmitsecurity.io (for CA), https://api.au.transmitsecurity.io (for AU), or a [custom domain](/guides/deployment/custom_domains).

iOS
### Install CocoaPods dependencies

Navigate to your iOS project directory and run `pod install`.

### Create TransmitSecurity.plist

Create a file named `TransmitSecurity.plist` in your native iOS Xcode project and add the following content:


```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>credentials</key>
    <dict>
        <key>baseUrl</key>
        <string>https://api.transmitsecurity.io/</string>
        <key>clientId</key>
        <string>[CLIENT_ID]</string>
    </dict>
</dict>
</plist>
```

Note
Configure the SDK to work with a different cluster by setting the base URL to https://api.eu.transmitsecurity.io (for EU), https://api.ca.transmitsecurity.io (for CA), https://api.au.transmitsecurity.io (for AU), or a [custom domain](/guides/deployment/custom_domains).

## Step 4: Import and initialize the SDK

div
div
Client
Android
Open your `MainApplication.kt` file in your React Native Android project, and add:


```kotlin
import com.transmit.accountprotection.TSAccountProtection // add this import statement at the top of the file

class Application : Application() {
  override fun onCreate() {
    super.onCreate()
    TSAccountProtection.initializeSDK(this.applicationContext) // initialize the SDK
    // ...
  }
}
```

iOS
In your main application file, initialize the SDK when your app component is ready:


```javascript
import { initializeSDKIOS, triggerAction, setUserId, clearUser, TSAction } from 'react-native-ts-accountprotection';

componentDidMount(): void {
    await initializeSDKIOS();
}
```

## Step 5: Use the React Native wrapper

div
div
Client
The example below demonstrates triggering a login event from a login button, setting and clearing a user.

Note
For an alternative approach that directly utilizes our backend API instead, refer to our [Backend API implementation](/guides/risk/quick_start_backendapi#step-4-trigger-actions) guide.

- `triggerAction()` receives an action type and returns a response that includes the `actionToken` that you should pass to the backend. To obtain risk recommendations for sensitive actions, your application should report these actions. To do this, add the code below to relevant user interactions (such as the Login button `click` event handler). The wrapper allows reporting on events with specific action types. Replace `[ACTION_TYPE]` with the appropriate action type from our [list of actions](/guides/risk/recommendations). To improve Fraud Prevention, optionally pass the correlation ID, and claimed user ID and its type (for users that haven't authenticated yet).



```javascript
import { triggerAction, setUserId, clearUser, TSAction } from 'react-native-ts-accountprotection';

private handleTriggerActionLoginExample = async () => {
  const triggerActionResponse = await triggerAction(
    TSAction.login,
    {
      correlationId: "CORRELATION_ID",
      claimUserId: "CLAIMED_USER_ID",
      referenceUserId: "REFERENCE_USER_ID",
      transactionData: undefined
    }
  )
  const actionToken = triggerActionResponse.actionToken;
  console.log("Action Token: ", actionToken); // Use the action token to invoke the recommendation API.
}
```

- `setUserId()` sets the user context for all subsequent events for the session (or until the user is explicitly cleared). It should be set only after you've fully authenticated the user (including, for example, any 2FA that was required). For the complete list of action types, see our [recommendations](/guides/risk/recommendations#action-types) page.

```javascript
await setUserId(username);
```
- `clearUser()` clears the user context for all subsequent events in the mobile session. The user gets automatically cleared once the session expires or in case of a login action.

```javascript
await clearUser();
```


## Step 6: Fetch recommendations

div
div
Backend
You can fetch recommendations for the reported action using the [Recommendation API](/openapi/risk/recommendations.openapi/other/getriskrecommendation). This is the same API that's also used for mobile integrations.

Mosaic APIs are authorized using an OAuth access token so you'll need to fetch a token using your client credentials (from Step 1). To do this, send the following request:


```javascript
  const { access_token } = await fetch(
    `https://api.transmitsecurity.io/oidc/token`,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams({
        grant_type: client_credentials,
        client_id: [CLIENT_ID],
        client_secret: [CLIENT_SECRET]
      })
    }
  );
```

From your backend, invoke the Recommendation API by sending a request like the one below. The `[ACCESS_TOKEN]` is the authorization token you obtained using your client credentials and `[ACTION_TOKEN]` is the `actionToken` received in Step 5.


```javascript
const query = new URLSearchParams({
  action_token: '[ACTION_TOKEN]',
}).toString();

const resp = await fetch(
  `https://api.transmitsecurity.io/risk/v1/recommendation?${query}`,
  {
    method: 'GET',
    headers: {
      Authorization: 'Bearer [ACCESS_TOKEN]',
    },
  }
);
```