> ## Documentation Index
> Fetch the complete documentation index at: https://docs.junction.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Junction Core SDK

> Use the Junction Core SDK to access the Junction API from mobile apps through typed request methods across iOS, Android, Flutter, and React Native.

## Overview

Check out the [SDK Authentication guide](/wearables/sdks/authentication) on the available SDK authentication
schemes, and how to authenticate with the Junction Mobile SDK.

<Card title="Mobile SDK Authentication" icon="id-card" href="/wearables/sdks/authentication" horizontal>
  Learn how to authenticate your mobile app user with Junction Mobile SDK.
</Card>

## Accessing Junction API

You can access Junction API through `VitalClient` with typed request methods and response models.

<CodeGroup>
  ```swift Native iOS theme={null}
  // e.g. Link Service
  let linkService = VitalClient.shared.link
  ```

  ```kotlin Native Android theme={null}
  // e.g. Link Service
  val linkService = VitalClient.getOrCreate(context).linkService
  ```

  ```dart Flutter theme={null}
  import 'package:vital_core/vital_core.dart' as vital_core;

  vital_core.VitalClient client;

  // For customers using Junction Team API Keys for early evaluation.
  client = vital_core.VitalClient()
    ..init(region: Region.eu, environment: Environment.sandbox, apiKey: 'sk_eu_...');

  // For customers using the Junction Sign-In Tokens scheme
  client = vital_core.VitalClient.forSignedInUser(environment: Environment.sandbox, region: Region.eu);

  // e.g. Link Service
  final linkService = client.linkService;
  ```
</CodeGroup>

<br />

<Info>Typed API access is not yet available in React Native.</Info>

## Generating Junction Link URL

You can obtain a URL to the Junction Link Widget through `VitalClient.linkWidgetUrl`. Note that the URL contains
a short-lived token with 10-minute expiry.

For the `redirectUrl`, you should set it to a URL with a custom URL scheme which your app would register to handle.

<CodeGroup>
  ```swift Native iOS theme={null}
  let widgetUrl = await VitalClient.shared.link.createProviderLink(
    redirectURL: "x-vital-app://"
  )
  ```

  ```dart Flutter theme={null}
  Uri widgetUrl = await client.linkWidgetUrl(redirectUrl: "x-vital-app://");
  ```
</CodeGroup>

## Core SDK Status

You can inspect the status of the Core SDK for troubleshooting, as well as for your own business logic.
For example, you may reconcile your user session and the Junction SDK sign-in based on the Core SDK status.

The SDK normally reports one of the following status combinations:

| Combinations                                | Auth Scheme            | Remarks                                                                          |
| ------------------------------------------- | ---------------------- | -------------------------------------------------------------------------------- |
| *Empty*                                     | N/A                    | The SDK has neither been configured nor has an active sign-in.                   |
| `Configured`                                | API Key                | The SDK has been configured, but does not have an active sign-in.                |
| `Configured`, `SignedIn` & `UseApiKey`      | API Key                | The active sign-in was done by setting an API Key and a target Junction User ID. |
| `Configured`, `SignedIn` & `UseSignInToken` | Junction Sign-In Token | The active sign-in was done by consuming a Junction Sign-In Token.               |

### Get the current status

<CodeGroup>
  ```swift Native iOS theme={null}
  import VitalCore

  let status: VitalClient.Status = VitalClient.status
  ```

  ```kotlin Native Android theme={null}
  import io.tryvital.client.VitalClient
  import android.content.Context

  val applicationContext: Context

  VitalClient.getOrCreate(applicationContext)
  val status = VitalClient.status
  ```

  ```typescript React Native theme={null}
  import { VitalCore } from "@tryvital/vital-core-react-native";

  await VitalCore.status();
  ```

  ```dart Flutter theme={null}
  import 'package:vital_core/vital_core.dart' as vital_core;

  await vital_core.clientStatus();
  ```
</CodeGroup>

### Observe status changes over time

<CodeGroup>
  ```swift Native iOS theme={null}
  import VitalCore

  // Combine Publisher
  let cancellable = VitalClient.statusDidChange.sink { _ in }

  // AsyncStream
  Task {
    for status in VitalClient.statuses {
      // ...
    }
  }
  ```

  ```kotlin Native Android theme={null}
  import io.tryvital.client.VitalClient
  import android.content.Context

  val applicationContext: Context

  val statuses: Flow<Set<VitalClient.Status>>
  statuses = VitalClient.statuses(applicationContext)

  val job = statuses
    .onEach { /** ... */ }
    .launchIn(...)
  ```

  ```typescript React Native theme={null}
  import { VitalCore } from "@tryvital/vital-core-react-native";

  VitalCore.observeStatusChange((statuses) => {
    // ...
  });
  ```

  ```dart Flutter theme={null}
  import 'package:vital_core/vital_core.dart' as vital_core;

  // NOTE: This stream does not yield the current statuses on listen.
  Stream<Set<ClientStatus>> statuses;
  statuses = vital_core.clientStatusStream;
  ```
</CodeGroup>

## Get Current Junction User ID

Aside from the Core SDK Status, you can also query the Junction User ID of the signed-in user:

<CodeGroup>
  ```swift Native iOS theme={null}
  import VitalCore

  let userId: String? = VitalClient.currentUserId
  ```

  ```kotlin Native Android theme={null}
  import io.tryvital.client.VitalClient
  import android.content.Context

  val applicationContext: Context

  VitalClient.getOrCreate(applicationContext)
  val userId = VitalClient.currentUserId
  ```

  ```typescript React Native theme={null}
  import { VitalCore } from "@tryvital/vital-core-react-native";

  const userId = await VitalCore.currentUserId();
  ```

  ```dart Flutter theme={null}
  import 'package:vital_core/vital_core.dart' as vital_core;

  final userId = await vital_core.currentUserId();
  ```
</CodeGroup>

## Reset the SDK (Sign Out)

Refer to [the Sign Out section in the SDK Authentication guide](/wearables/sdks/authentication#sign-out).

## Verbose Logging to Console/Logcat

You can enable verbose logging to have a deeper look into how Junction Mobile SDK is reacting to your calls, as well as
all the automatic behaviors that the SDK is performing in background.

To avoid any missing log entry, you should enable verbose logging as early as possible — preferably before any usage of
any Junction Mobile SDKs.

<Note>
  On Android, verbose logging is enabled by default when running as a debuggable build
  (i.e., with `ApplicationInfo.FLAG_DEBUGGABLE`).
</Note>

<CodeGroup>
  ```swift Native iOS (Swift) theme={null}
  import VitalCore

  class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
      _ application: UIApplication,
      didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
    ) -> Bool {

      VitalLogger.stdOutEnabled = true

      // ...

      return true
    }
  }
  ```

  ```objc Native iOS (Objective-C) theme={null}
  #import "AppDelegate.h"

  @import VitalCore;

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    VitalLoggerObjC.stdOutEnabled = YES;

    // ...

    return YES;
  }
  ```

  ```kotlin Native Android theme={null}
  import io.tryvital.client.utils.VitalLogger

  VitalLogger.getOrCreate().enabled = true
  ```
</CodeGroup>
