# Configure Flows Locally and Run Step by Step

This pattern breaks a locally-defined flow into sections your app runs one at a time. Control returns to your app between sections, so you can display your own intermediate UI, run business logic, or branch based on intermediate results before starting the next section.

Use this pattern when you need to interleave your own UI or logic with the SDK modules, or when you want to split a long flow into discrete steps.

## Prerequisite

The SDK must be initialized before you start a session. See [Installation](installation) for the `initializeSDK()` call and its arguments.

## Lifecycle

The step-by-step pattern uses five methods, called in this order:

1. `setupOnboardingSession()`**:** create the session. Returns an `interviewId` and `token`.
2. `startOnboardingSection()`**:** run one section. Can be called multiple times, but only one section at a time. Wait for each section's callback before starting the next.
3. `finishOnboarding()`**:** finalize the session. Call exactly once, after all sections succeed.
4. `getUserScore()`**:** fetch verification scores and results.
5. `deleteUserLocalData()`**:** clear local cache.

Steps 4 and 5 are optional. Skip `getUserScore()` if you don't need scores (or use the inline `userScore` module in a section). Skip `deleteUserLocalData()` if you don't need local cleanup.

## Walkthrough

### Step 1: Set up the session

```js
let sessionConfig = {
  configurationId: "your-flow-id",   // optional
  externalId: "your-external-id",    // optional
  e2eEncryptionEnabled: false,       // optional
  region: "ALL"                      // optional
};

cordova.exec(
  function (data) {
    console.log("Session ready. interviewId:", data.interviewId, "token:", data.token);
    runFirstSection();
  },
  function (err) { console.log("setupOnboardingSession error:", err); },
  "Cplugin",
  "setupOnboardingSession",
  [sessionConfig]
);
```

### Step 2: Run a section

```js
function runFirstSection() {
  let flowConfig = [
    { module: "addId", showIdTypeChooser: "true" },
    { module: "addSelfieScan" },
    { module: "addFaceMatch", matchType: "idSelfie" }
  ];
  let recordSessionConfig = { recordSession: "false", forcePermissions: "false" };
  let sectionTag = "identity-section-001";

  cordova.exec(
    function (result) {
      console.log("Section status:", result.status);         
      console.log("Section tag:", result.sectionTag);
      console.log("Front ID:", result.frontIdData);
      console.log("Face match:", result.faceMatchData);
      // Call startOnboardingSection again for the next section,
      // or proceed to finishing the session.
      finishSession();
    },
    function (error) {
      // Typed error string, e.g. "permissionsDenied", "rootDetected"
      console.log("Section error:", error);
    },
    "Cplugin",
    "startOnboardingSection",
    [flowConfig, recordSessionConfig, sectionTag]
  );
}
```

### Step 3: Finish the session

```js
function finishSession() {
  cordova.exec(
    function () {
      console.log("Onboarding finished");
      fetchScore(); // optional, only if you need scores
    },
    function (err) { console.log("finishOnboarding error:", err); },
    "Cplugin",
    "finishOnboarding",
    []
  );
}
```

### Step 4: Fetch the user score (Optional)

```js
function fetchScore() {
  cordova.exec(
    function (winParam) {
      console.log("Score:", JSON.stringify(winParam));
      cleanup();
    },
    function (err) { console.log("getUserScore error:", err); },
    "Cplugin",
    "getUserScore",
    ["fast"] // "fast" | "accurate"
  );
}
```

### Step 5: Delete local data (Optional)

```js
function cleanup() {
  cordova.exec(
    function () { console.log("Local data deleted"); },
    function (err) { console.log("deleteUserLocalData error:", err); },
    "Cplugin",
    "deleteUserLocalData",
    []
  );
}
```

## Notes

- You can start multiple sections, but only one at a time. Wait for each section's callback before starting the next.
- Section result payloads vary depending on which modules ran in the section. See [Results](results) for the full structure.

## Related pages

- [Modules](modules): every module available in `flowConfig` and its parameters.
- [Results](results): the structure of section results and the list of error strings.
- [API Reference](api-reference): the full signatures of `setupOnboardingSession()`, `startOnboardingSection()`, `finishOnboarding()`, `getUserScore()`, and `deleteUserLocalData()`.

<br />
