# Get started with React Native

> Build a small React Native (Expo) app that enrols your face and verifies it offline, on your own phone.

Source: https://docs.faceping.ai/start/react-native/

What you'll build

### A face check that works in airplane mode

An app that enrols your face, verifies it live with a head-turn liveness check, and still matches with no network.

About 10 minutes
Android phone or iPhone
Free sandbox key

## Get a sandbox key

Sign up at [app.faceping.ai/signup](https://app.faceping.ai/signup?platform=react-native) with your email, then open [**Developers → Sandbox keys → Create sandbox key**](https://app.faceping.ai/developers). It starts with `fp_test_`. It also works on the API for your practice list, so keep test apps within your team, and revoke the key under Developers if it leaks.

The sandbox is free and instant: up to 25 faces per device, each deleted 24 hours after it was enrolled. No card, no approval.

## Create the project

You need Node 20.19.4 or later and a phone with a cable: an Android phone (Android 7 or later, 64-bit) with USB debugging on, the Android SDK and JDK 17 or later (both come with [Android Studio](https://developer.android.com/studio)), or an iPhone (iOS 16.4 or later, the minimum for Expo SDK 57; FacePing itself needs 15.1) and a Mac with Xcode 26 or later.

```bash
npx create-expo-app@latest facecheck --template blank-typescript
cd facecheck
```

Give the app its ids now, in `app.json` (otherwise Expo makes up `com.anonymous.facecheck`, and iOS signing needs an id that's yours). The template already has an `ios` and an `android` block under `expo`: add one line to each, and keep what's there:

```json title="app.json"
"ios": {
  "supportsTablet": true,
  "bundleIdentifier": "com.yourcompany.facecheck"
},
"android": {
  …,
  "package": "com.yourcompany.facecheck"
}
```

FacePing has native code, so the app runs as a development build (`npx expo run:…`), not in Expo Go. Using a bare React Native app? See [bare React Native](https://docs.faceping.ai/sdk/react-native/#bare-react-native).

First React Native app on this machine?

The 10 minutes assume your machine already builds native React Native apps. If it doesn't, follow Expo's [set-up guide](https://docs.expo.dev/get-started/set-up-your-environment/) for a development build on a physical device (Android Studio, or Xcode on a Mac; both take a while to download).

## Add FacePing

One package from npm. It brings the face models, liveness checks and a camera view with it. FacePing for React Native is in preview, under the `preview` tag:

```bash
npx expo install @faceping/react-native@preview
```

It also adds FacePing's config plugin to `app.json`. Give the plugin the camera text iOS shows the person (see [Allow the camera](#allow-the-camera)) by changing its entry under `"plugins"`:

```json title="app.json"
"plugins": [
  ["@faceping/react-native", { "cameraPermission": "FaceCheck uses the camera to verify your face." }]
]
```

See [npm](https://www.npmjs.com/package/@faceping/react-native) for the latest version. The first build downloads Microsoft's ONNX Runtime and the face models (a few tens of MB), and they add about the same to your app.

## Allow the camera

Nothing more to add: the config plugin did it.

- **iOS:** it puts your `cameraPermission` text in the app's Info.plist as `NSCameraUsageDescription`. iOS shows it to the person before the camera turns on, and stops the app without it.
- **Android:** the FacePing library declares the camera permission (and `INTERNET`, to activate your key). The plugin also builds Android for the two ABIs FacePing runs on, `arm64-v8a` and `x86_64`.

The camera view asks the person for the camera the first time it appears.

Face data stays on the phone. FacePing encrypts it with a key kept in the Android Keystore, which backups don't include, so a backed-up copy could never be read on another phone. The config plugin adds Android backup rules that leave FacePing's data out of cloud backups and phone-to-phone transfers (unless your app already has its own rules: then add FacePing's two excludes to yours, see [the reference](https://docs.faceping.ai/sdk/react-native/#android-backup)). Also turn off Android backup in `app.json`: add `allowBackup` to the same `android` block as the package:

```json title="app.json"
"android": {
  …,
  "package": "com.yourcompany.facecheck",
  "allowBackup": false
}
```

Run `npx expo prebuild --clean` if you've already generated the native projects.

## Turn FacePing on

Set FacePing up once, when the app starts, with your sandbox key. Add this to `App.tsx`, under the imports:

```tsx title="App.tsx"
import { FacePing } from '@faceping/react-native';

// Starts loading the face models in the background, so the first check is quick.
FacePing.init({ sandboxKey: 'fp_test_…' }).catch((e) => console.warn(e.message));
```

To keep the key out of your code, put `EXPO_PUBLIC_FACEPING_SANDBOX_KEY=fp_test_...` in `.env.local` (the Expo template keeps it out of git) and use `process.env.EXPO_PUBLIC_FACEPING_SANDBOX_KEY`, as the starter app does.

There's no need to sync first: the key activates itself on the first enrol or verify (which needs a connection, once). `await FacePing.sync()` at start-up is optional; it just tells you sooner about a revoked key or a missing connection.

## Add the screen

A camera view and two buttons, with just enough style to look like an app: one enrols your face on the phone, the other verifies it. Replace `App.tsx` (the `init` call from [Turn FacePing on](#turn-faceping-on) is at the top).

```tsx title="App.tsx"
import { FaceCamera, FacePing, type FaceCameraRef } from '@faceping/react-native';
import { useRef, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';

// Starts loading the face models in the background, so the first check is quick.
FacePing.init({ sandboxKey: 'fp_test_…' }).catch((e) => console.warn(e.message));

export default function App() {
  const camera = useRef<FaceCameraRef>(null);
  const [status, setStatus] = useState('');

  // For example no connection on the very first run (the sandbox key is activated once), or no camera.
  const act = (action: () => Promise<string>) => action().then(setStatus, (e: Error) => setStatus(e.message));

  const enroll = () =>
    act(async () => {
      const result = await FacePing.enroll('me', camera); // a still from the camera
      return result.isEnrolled ? 'Enrolled. Now tap Verify.' : `Try again: ${result.outcome}`;
    });

  const verify = () =>
    act(async () => {
      // A live check: matches your face, then asks you to turn your head (liveness).
      const result = await FacePing.verifyLive('me', camera);
      const time = (result.elapsedMs / 1000).toFixed(2);
      return result.isMatch ? `Match · ${time} s` : `Try again: ${result.outcome}`;
    });

  return (
    <View style={styles.screen}>
      <FaceCamera ref={camera} style={styles.camera} onCameraFailed={({ message }) => setStatus(message)} />
      <Text style={styles.status}>{status}</Text>
      <Pressable style={[styles.button, styles.outline]} onPress={enroll}>
        <Text style={[styles.label, styles.outlineLabel]}>Enrol my face</Text>
      </Pressable>
      <Pressable style={styles.button} onPress={verify}>
        <Text style={styles.label}>Verify</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1, padding: 24, paddingTop: 64, gap: 12 },
  camera: { height: 420 },
  status: { fontSize: 18, textAlign: 'center', minHeight: 24 },
  button: { backgroundColor: '#2563eb', borderRadius: 12, padding: 16, alignItems: 'center' },
  label: { color: '#fff', fontSize: 17, fontWeight: '600' },
  outline: { backgroundColor: 'transparent', borderWidth: 1.5, borderColor: '#2563eb' },
  outlineLabel: { color: '#2563eb' },
});
```

## Run it on your phone

Plug your phone in and run one of these. The first run creates the native `android` and `ios` folders from `app.json`:

```bash
npx expo run:android --device
npx expo run:ios --device
```

A debug build is fine for trying it: the FacePing core inside the SDK is already compiled for speed, whichever build you run. For a release build (the app as you'd ship it):

```bash
npx expo run:android --device --variant release
npx expo run:ios --device --configuration Release
```

The Expo template signs Android release builds with the debug keystore, so they install straight away; set up your own upload key before you publish.

To build without installing (for example to hand the app to someone else), build in the native projects after the first run has created them:

```bash
# Android: the APK lands in android/app/build/outputs/apk/release/
cd android && ./gradlew assembleRelease
# iOS: open ios/FaceCheck.xcworkspace in Xcode and use Product → Archive
```

That APK holds the face engine for phones (`arm64-v8a`) and for the emulator (`x86_64`), about 100 MB. For phones only, about 60 MB, add `-PreactNativeArchitectures=arm64-v8a` to the Gradle line; for the Play Store, `./gradlew bundleRelease` makes an App Bundle, from which each phone downloads only its own. On iOS, `pod install` downloads the FacePing framework from GitHub once and checks its SHA-256; pick your team when Expo asks, or set it in Xcode under **Signing & Capabilities**. The first time, turn on **Developer Mode** on the iPhone (**Settings → Privacy & Security → Developer Mode**).

The first run needs a connection, to activate your sandbox key. Tap **Enrol my face**, then **Verify**: you should see `Match`. Use a phone: the iOS simulator has no front camera, and an Android emulator only has one if you map your computer's webcam to it in the emulator's camera settings, which is slower and less reliable for the liveness check.

**Now switch on airplane mode and tap Verify again.** Still a match: the check ran entirely on your phone.

## You've built a face check that works offline

Next, make it real: verify the people in your app (members, visitors or people with tickets), and apply to go live when you're ready.

- [Clone the finished app](https://github.com/faceping/react-native-starter): github.com/faceping/react-native-starter
- [How the SDK works](https://docs.faceping.ai/sdk/): Liveness, storage, sandbox limits
- [React Native reference](https://docs.faceping.ai/sdk/react-native/): Every type and method
- [@faceping/react-native on npm](https://www.npmjs.com/package/@faceping/react-native): Versions and changelog

## Try a check-in device on your practice list

This is the ticketing flow, end to end, in the sandbox: a person enrols through your server or an enrolment link, and a check-in device verifies them offline.

1. Create your practice list, if your account doesn't have one: **New list** in the [dashboard](https://app.faceping.ai/lists) asks for everything it needs, or use [quickstart steps 1 and 2](https://docs.faceping.ai/quickstart/#1-create-a-check-in-list) (create the list, then set its widget details: enrolment links need them). Then enrol someone on it with your sandbox key: open an enrolment link ([quickstart step 3](https://docs.faceping.ai/quickstart/#3-create-an-enrolment-link-for-a-person); links last 30 minutes unless you pass `validForHours`), or enrol a selfie from your server with the copy-paste request in [Enrol someone from your server](https://docs.faceping.ai/quickstart/#enrol-someone-from-your-server). Note their `personId`, for example `ACME-000123` (if your team shares the practice list, pick your own).
2. Add a check-in device: the list's **Devices** tab in the [dashboard](https://app.faceping.ai/lists), or `POST /v1/lists/{listId}/devices`. Copy the `fpd_` device token: it's shown once.
3. Check the token works before you build: `curl https://api-eu.faceping.ai/v1/device -H "X-Device-Token: fpd_…"` returns the list's name and `peopleEnrolled`. It doesn't download any faces or count as a sync.
4. Make a device build that sits next to your sandbox app: copy the project folder (without `android`, `ios` and `node_modules`), and in the copy's `app.json` give it its own `name` (for example `FaceCheck Device`), `ios.bundleIdentifier` and `android.package` (for example `com.yourcompany.facecheck.device`). Put the token in the copy's `.env.local` as `EXPO_PUBLIC_FACEPING_DEVICE_TOKEN=fpd_…`.
5. Replace the copy's `App.tsx`. It sets FacePing up with the device token, verifies the person you enrolled, syncs when the screen opens and shows the result of every sync after that. People enrol on the server, not in the app (`enroll` throws a `FacePingError` with the code `invalidOperation` on a device), so there's no enrol button:

```tsx title="App.tsx (device build)"
import { FaceCamera, FacePing, type FaceCameraRef } from '@faceping/react-native';
import { useEffect, useRef, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';

const personId = 'ACME-000123'; // the person you enrolled

// A device token instead of the sandbox key. A setup problem shows on screen when the app syncs.
const ready = FacePing.init({ deviceToken: process.env.EXPO_PUBLIC_FACEPING_DEVICE_TOKEN });
ready.catch(() => {});

export default function App() {
  const camera = useRef<FaceCameraRef>(null);
  const [synced, setSynced] = useState('');
  const [status, setStatus] = useState('');

  useEffect(() => {
    // Every sync, now and the automatic ones every few minutes, updates the line
    // ('revoked': the token was revoked, mistyped or its list deleted).
    const subscription = FacePing.addListener('synced', (r) => setSynced(`${r.enrolled} people synced (${r.status})`));
    ready.then(() => FacePing.sync()).catch((e: Error) => setSynced(e.message));
    return () => subscription.remove();
  }, []);

  const verify = () =>
    FacePing.verifyLive(personId, camera).then(
      (result) =>
        setStatus(
          result.isMatch
            ? `Match · ${(result.elapsedMs / 1000).toFixed(2)} s`
            : result.outcome === 'notEnrolled'
              ? `${personId} isn't on this device yet: enrol them, then wait for the next sync.`
              : `Try again: ${result.outcome}`,
        ),
      (e: Error) => setStatus(e.message),
    );

  return (
    <View style={styles.screen}>
      <FaceCamera ref={camera} style={styles.camera} onCameraFailed={({ message }) => setStatus(message)} />
      <Text style={styles.synced}>{synced}</Text>
      <Text style={styles.status}>{status}</Text>
      <Pressable style={styles.button} onPress={verify}>
        <Text style={styles.label}>Verify</Text>
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1, padding: 24, paddingTop: 64, gap: 12 },
  camera: { height: 420 },
  synced: { fontSize: 15, textAlign: 'center', color: '#555' },
  status: { fontSize: 18, textAlign: 'center', minHeight: 24 },
  button: { backgroundColor: '#2563eb', borderRadius: 12, padding: 16, alignItems: 'center' },
  label: { color: '#fff', fontSize: 17, fontWeight: '600' },
});
```

6. Run it with `npm install` and `npx expo run:android --device` (or `run:ios`) in the copy. It downloads the list's faces at start-up and every few minutes after; the top line shows how many people synced and the sync status, and updates with each sync. Then tap **Verify**, with no network if you like.

Each face stays on the device no longer than its 24 hours on the practice list. See [Devices and offline sync](https://docs.faceping.ai/devices/).

The [finished app](https://github.com/faceping/react-native-starter) runs as a check-in device too: put `EXPO_PUBLIC_FACEPING_DEVICE_TOKEN` and `EXPO_PUBLIC_FACEPING_PERSON_ID` in its `.env.local` (see its README), and if you change its ids, run `npx expo prebuild --clean` before you build, so the native projects pick up the new ids.

If you switch one install from the sandbox key to a device token instead, the two sets of faces stay apart: the app sees only the people synced from the device's list, and your sandbox faces come back (if they're under 24 hours old) when you switch back. Uninstalling removes both.
