Everything your JavaScript contains ships inside the app bundle, and anyone who inspects that bundle can read it as plain text. That fact decides where a third-party API key, a signed-in user's access token and an offline cache should live, and each of the three gets a different answer.

Key terms

  • App bundle: the packaged app code you ship to devices, which anyone inspecting it can read as plain text.
  • Orchestration layer: a server-side step between your app and a protected resource that holds the API key or secret and forwards the app's requests.
  • Async Storage: a community-maintained module, separate from React Native itself, that provides an asynchronous, unencrypted key-value store.
  • Keychain Services: the iOS facility for securely storing small pieces of sensitive data such as tokens, passwords and certificates.
  • Encrypted Shared Preferences: an Android wrapper around Shared Preferences that encrypts keys and values automatically.

API keys in app code

Never put a sensitive API key in app code. Whatever form the key takes in your source, a constant or a field in a config object, it ships in the bundle where anyone inspecting it can read it.

Environment-variable tools are the usual trap. react-native-dotenv and react-native-config are a good fit for values that change per environment, such as the API endpoint your staging and production builds call. They are not server-side environment variables, which is where secrets and API keys usually sit, so treat every value you put in them as public.

A server function holds the key

When the app needs a key or secret to reach a resource, the most secure setup is an orchestration layer between the app and that resource. A serverless function, on AWS Lambda or Google Cloud Functions for example, receives the app's request, adds the key and forwards it. Secrets kept in server-side code can't be read by the API's consumers the way secrets shipped in app code can.

Take a translation feature backed by a paid API. The app never talks to the translation provider. The translateText example calls your own function with a POST request, built with the second argument of fetch, and its body is the only data that leaves the device:

body: JSON.stringify({ text, targetLanguage })

The request has no Authorization header and no key in the URL. Everything the provider requires beyond the text itself is added on the server.

Choosing storage by sensitivity

Apps persist data on the device to work offline, to cut down on network requests, or to keep the user's access token between sessions so they don't sign in on every launch. Persisted data is written to disk, which lets it survive app launches and also makes it more exposed to attackers. Unpersisted data never touches the disk, so there is nothing there to read. Pick the storage for each value by how sensitive it is.

Async Storage is the React Native counterpart of the web's Local Storage. Each app has its own sandbox, so other apps can't read your store, but the store itself is unencrypted. It is the right place for non-sensitive data persisted across app runs, such as persisted Redux or GraphQL state. Tokens and secrets don't belong in it.

React Native ships no built-in way to store sensitive data. Secure storage comes from each platform:

  • On iOS, Keychain Services holds tokens, passwords, certificates and any other sensitive value that doesn't belong in Async Storage.
  • On Android, plain Shared Preferences data is not encrypted by default, and Encrypted Shared Preferences adds automatic encryption of keys and values.
  • The Android Keystore system keeps cryptographic keys in a container that makes them harder to extract from the device.

To reach Keychain Services or Android's secure shared preferences from JavaScript, you either write a native bridge yourself or adopt a library that wraps them behind one API, at your own risk. expo-secure-store and react-native-keychain are two such libraries.

Leaks through state and monitoring

Sensitive data often lands somewhere unsafe without anyone writing it there on purpose. Keep a sensitive form field, such as a passport number, in Redux state, persist the whole state tree to Async Storage, and that field now sits on disk in an unencrypted store. Application monitoring is the other common path: user tokens and personal info attached to what the app sends to a service such as Sentry or Crashlytics are exposed there too.

The cheapest protection is to request less, since information the app never asked for is much harder to steal than information it stored carefully.

Code Examples

typescript
// No API key in this file: the server function adds it before calling the provider.
const TRANSLATE_FUNCTION_URL = 'https://api.example.com/translate';

export async function translateText(
  text: string,
  targetLanguage: string,
): Promise<string> {
  const response = await fetch(TRANSLATE_FUNCTION_URL, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ text, targetLanguage }),
  });
  if (!response.ok) {
    throw new Error(`Translation failed with status ${response.status}`);
  }
  const result = await response.json();
  return result.translation;
}
✓ Completed