Docs

Initialize

Start the SDK once, in your entry file, with a publishable key.

Call initialize in your entry file

Call Lander.initialize once, as early as possible: at module scope in index.js (bare React Native) or in app/_layout.tsx (Expo Router). Do not call it inside a component render or an effect that can run twice.

index.js
import { AppRegistry } from 'react-native'
import { Lander } from 'react-native-lander'
import App from './App'
import { name as appName } from './app.json'

Lander.initialize({
  apiKey: 'pk_live_...', // publishable key from Settings > API keys
  enableLogging: __DEV__,
})

AppRegistry.registerComponent(appName, () => App)

You do not need to await it before rendering. The returned Promise resolves quickly, after local work only: loading (or creating) the install ID from on device storage and asking the native side for the launch link. The launch report to Lander (POST /v1/sdk/init) runs in the background, so a slow or missing network never delays your first screen.

Parameters

Lander.initialize(options: LanderOptions): Promise<void>

type LanderOptions = {
  apiKey: string
  baseUrl?: string
  enableLogging?: boolean
}

Prop

Type

Use a publishable key

Create keys in the dashboard under Settings > API keys (Dashboard: API keys). Only publishable keys are accepted:

KeyIn an app?
pk_test_...Yes. Test environment.
pk_live_...Yes. Live environment.
sk_test_..., sk_live_...Never. initialize rejects with a LanderError (invalid_argument).

Anything you ship in an app bundle is public, which is why secret keys are refused outright. Any other value that is not a publishable key (pk_test_ or pk_live_ followed by at least 24 letters or digits) also rejects with invalid_argument. Surrounding whitespace is ignored, and the error never contains the key, so it is safe to log.

In development an unhandled rejection shows up as a warning, so a wrong key is hard to miss. To handle it yourself:

Lander.initialize({ apiKey: landerKey }).catch((e) => console.error('Lander:', e.message))

Test and live builds

The key you pass decides which environment the app talks to. Links created in the test environment only resolve for apps using a pk_test_ key, and live links only for pk_live_ keys. See Environments.

A simple way to switch is an environment variable read at build time. With Expo, variables prefixed EXPO_PUBLIC_ are inlined into the bundle:

.env.development
EXPO_PUBLIC_LANDER_KEY=pk_test_...
app/_layout.tsx
Lander.initialize({ apiKey: process.env.EXPO_PUBLIC_LANDER_KEY ?? '', enableLogging: __DEV__ })

In a bare React Native app, __DEV__ works as a switch, or use a package such as react-native-config:

index.js
Lander.initialize({ apiKey: __DEV__ ? 'pk_test_...' : 'pk_live_...' })

If the variable is missing, the key is an empty string and initialize rejects with invalid_argument, so the mistake shows up on the first run instead of in production.

If you switch an existing install from a test key to a live key (or to another project), Lander reports that the install ID belongs elsewhere (install_id_conflict). The SDK then creates a fresh install ID and reports a first launch, so nothing breaks.

What initialize does

  1. Validates the key (rejects with invalid_argument for secret or malformed keys).
  2. Loads the install ID from storage, or creates a random UUID v4 on first launch.
  3. Starts listening for links and reads the launch link.
  4. Starts listening to AppState: events are flushed when the app goes to the background, and a failed launch report is retried when it comes back.
  5. In the background, reports the launch with POST /v1/sdk/init. On an Android first launch it reads the Play Install Referrer (waiting at most 5 seconds) and sends it along. On a first launch the answer may carry a deferred link.
  6. Loads any events left in the on device queue from earlier sessions and sends them.

initialize never rejects because of the network. A failed launch report is logged (when logging is on). A failed first launch report is retried in the same session (when the app returns to the foreground, before claimHandoff and before events are sent) and on the next launch. The install is counted once, because the API keeps one record per install ID.

Rules worth knowing

  • Call it once. A second call is ignored (it logs a message when logging is on) and returns the same Promise as the first. Fast Refresh does not re-run your entry file, so this is rarely an issue.
  • Other methods need it. getInitialLink, createLink, claimHandoff and trackEvent reject with a LanderError with code not_initialized if you call them before initialize.
  • Errors arrive through the Promise. Every method that returns a Promise, initialize included, reports its errors as a rejected LanderError rather than throwing synchronously, so await inside try catches them.
  • onDeepLink does not need it. You can subscribe with Lander.onDeepLink or useLanderDeepLink before initialize; links are buffered until you do.
  • installId is null until initialize resolves.
  • Expo Go. Without the native module (Expo Go, or an app not rebuilt after installing), the SDK warns once, keeps the install ID in memory and skips the launch report. Use a development build.

The install ID

Lander.installId is the random UUID v4 the SDK created for this install. It is not tied to the device or the user:

  • It is stored in a file excluded from iCloud and Android backups (Application Support on iOS, noBackupFilesDir on Android), so it is never restored onto another device.
  • It is gone when the app is uninstalled. A reinstall gets a new one.

It is handy for support requests: show it in a debug screen so testers can quote it.

<Text selectable>Install ID: {Lander.installId ?? '(loading)'}</Text>

Next steps

On this page