# Token-based Setup

Initialize the SDK without an API key by providing only an `apiUrl` to the `init` method, then using a session `token` to configure your onboarding session.

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 only an `apiUrl`, configure your session with a `token`, then start onboarding or set up a section-based flow.

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

Provide only the `apiUrl` to the `init` method. Omit `apiKey`.

```dart
IncodeOnboardingSdk.init(
  apiUrl: 'https://demo-api.incodesmile.com/0/',
  onError: (String error) {
    print('Incode SDK init failed: $error');
  },
  onSuccess: () {
    // Update UI, safe to start Onboarding
    print('Incode initialize successfully!');
  },
);
```

### 2. Configure the session with a token

In your `onSuccess` callback, create an `OnboardingSessionConfiguration` and pass your session `token`.

```dart
OnboardingSessionConfiguration sessionConfiguration = OnboardingSessionConfiguration(
  token: 'YOUR_TOKEN',
);
```

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

Pass the configured `OnboardingSessionConfiguration` to `startOnboarding`, `setupOnboardingSession`, or any of the online-configured flow methods. Token-based setup works with all three [Common Implementation Patterns](./flutter-common-implementation-patterns).

## 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.
