# Token-based Setup

Token-based setup is a security measure that avoids exposing your API key in the mobile app. Instead of embedding the API key or sending it to the app, your backend uses the API key to create a session token, then sends that token to the app to start the onboarding session. This prevents attackers from extracting the API key and calling Incode APIs on your behalf.

## Prerequisites

- A session `token` issued by your backend.
- The `apiUrl` provided by Incode.

## Set up the SDK with a token

Token-based setup has three steps: initialize the SDK with an empty apiKey, isExternalTokenEnabled set to "true", and your apiUrl; create a sessionConfig with your backend token; then pass that config to your chosen flow API.

### 1. Initialize the SDK without an API key

Pass an empty string for `apiKey`, set `isExternalTokenEnabled` to "true", and provide your `apiUrl` to `initializeSDK()`.

```javascript
cordova.exec(
  function () {
    console.log("SDK initialized — safe to start onboarding");
    setupSessionWithToken();
  },
  function (err) {
    console.log("Init error:", err);
  },
  "Cplugin",
  "initializeSDK",
  [
    "",                              // apiKey — empty for token-based setup
    "https://your.api.url",          // apiUrl
    "true",                          // loggingEnabled
    "false",                         // testMode
    "true",                          // isExternalTokenEnabled — required
    null,                            // clientExperimentId
    null,                            // e2eeUrl (optional; set if using E2EE)
    { enabled: false, forceSSLPinning: false }
  ]
);
```

### 2. Configure the session with a token

After initialization succeeds, create a `sessionConfig` and pass your session `token`.

```javascript
var sessionConfig = {
  token: "YOUR_TOKEN",
};
```

### 3. Start onboarding or set up a section-based flow

Pass the configured `sessionConfig` to `startOnboarding()`, `setupOnboardingSession()`, or any of the online-configured flow methods. Token-based setup works with all three [Common Implementation Patterns](#).

```javascript
// Section-based — create/resume session first
cordova.exec(
  function (data) {
    console.log("Session ready:", data.interviewId, data.token);
  },
  function (err) { console.log("Error:", err); },
  "Cplugin",
  "setupOnboardingSession",
  [sessionConfig]
);

// End-to-end — pass token in sessionConfig to startOnboarding
cordova.exec(
  function (result) { console.log("Done:", result); },
  function (err) { console.log("Error:", err); },
  "Cplugin",
  "startOnboarding",
  [sessionConfig, flowConfig, recordSessionConfig]
);

// Dashboard flows — same sessionConfig
cordova.exec(..., "startFlow", [sessionConfig, "EMAIL"]);
cordova.exec(..., "startWorkflow", [sessionConfig]);
```

## Token expiration

Tokens are long-lived, so expiration during a session is unlikely. If a token does expire mid-session, the backend returns a 401 error, which is propagated to the SDK. Tokens cannot be refreshed; the user must start a new onboarding session, which generates a new token.
