> For the complete documentation index, see [llms.txt](https://docs.facephi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.facephi.com/docs.facephi-en/sdks/sdk-web/ejemplos-de-integracion/uso-basico.md).

# Basic usage

Basic SDK Web integration example.

The following guide shows an integration example in a TypeScript + VITE environment. This same process can be carried out in current frameworks, with the differences required by the environment (syntax, component definitions, etc.).

This example is based on a basic integration. You can change the flow's behavior using the required configurations or changes.

This integration uses the SDK Provider, SelphID Widget, Selphi Widget, and Tracking components (included in SDK Provider).

***

### Installing the component

Install the SDK Web library:

```bash
npm install @facephi/sdk-web-wc@latest
```

{% hint style="info" %}
Remember to prepare the file `.npmrc` with the credentials required for the download.
{% endhint %}

Define the custom elements:

```javascript
// main.ts
import '@facephi/sdk-web-wc';
import { defineCustomElements as defineFacephiSDKCustomElements } from '@facephi/sdk-web-wc/loader';

defineFacephiSDKCustomElements(window);
```

***

### SDK Provider integration and configuration

Integrate and configure the SDK Provider:

{% code title="App.tsx" %}

```tsx
import { Language, TrackingSteps, TypeFamily } from '@facephi/sdk-web-wc';

<section className="facephi-sdk-provider">
  <facephi-sdk-provider
    apikey={import.meta.env.VITE_LICENSE_KEY}
    steps={`${TrackingSteps.start},${TrackingSteps.selphidWidget},${TrackingSteps.selphiWidget},${TrackingSteps.finish}`}
    type={TypeFamily.onboarding}
    customerId={customerId}
    language={Language.en}
  />
</section>
```

{% endcode %}

SDK Provider logic:

{% code title="facephi-sdk-provider.ts" %}

```typescript
import { initSelphidWidget } from './facephi-selphid-widget';
import { ErrorData } from '@facephi/sdk-web-wc';

(async () => {
	await customElements.whenDefined('facephi-sdk-provider');

	// SDK Provider event handlers
	function handleEmitData(event: CustomEvent<{ operationId: string; sessionId: string; extraData: string }>) {
		const result = event.detail;
		console.log('[FACEPHI SDK PROVIDER] onEmitData', result);
	}

	function handleEmitError(event: CustomEvent<ErrorData>) {
		const result = event.detail;
		console.error('[FACEPHI SDK PROVIDER] onEmitError', result);
	}

	const sdkProvider = document.querySelector('facephi-sdk-provider');
	if (sdkProvider) {
		sdkProvider.addEventListener('emitData', handleEmitData);
		sdkProvider.addEventListener('emitError', handleEmitError);
		
		// Initializes the SelphID widget
		initSelphidWidget(sdkProvider);
	}
})();
```

{% endcode %}

SelphID Widget logic:

{% code title="facephi-selphid-widget.ts" %}

```typescript
import {
	Language,
	type ExtractionFinishEvent,
} from '@facephi/selphid-web-component';
import { initSelphiWidget } from './facephi-selphi-widget';

export function initSelphidWidget(sdkProvider: HTMLElement) {
	sdkProvider.innerHTML = '';
	const selphidWidget = document.createElement('facephi-selphid-widget');
	selphidWidget.country = 'GB';
	sdkProvider.appendChild(selphidWidget);

	// SELPHID EVENTS
	function handleExtractionFinish(event: CustomEvent<ExtractionFinishEvent>) {
		const result = event.detail.detail;
		Logger.printLog(LoggerType.SELPHID, 'extractionFinish', result);

		// Initialize Selphi
		initSelphiWidget(sdkProvider);
	}
	
	// other events

	if (selphidWidget) {
		selphidWidget.addEventListener('extractionFinish', handleExtractionFinish);
	}
}
```

{% endcode %}

Selphi Widget logic

{% code title="facephi-selphi-widget.ts" %}

```typescript
import {
	Language,
	type ExtractionFinishEvent,
} from '@facephi/selphi-web-component';

export function initSelphiWidget(sdkProvider: HTMLElement) {
	sdkProvider.innerHTML = '';
	const selphiWidget = document.createElement('facephi-selphi-widget');
	selphiWidget.stabilization = true;
	selphiWidget.interactible = true;
	selphiWidget.previewImage = true;
	selphiWidget.timeout = 30000;
	selphiWidget.showLog = false;
	sdkProvider.appendChild(selphiWidget);

	//SELPHI EVENTS
	function handleExtractionFinish(event: CustomEvent<ExtractionFinishEvent>) {
		const result = event.detail.detail;
		console.log('[FACEPHI SELPHI] extractionFinish', result);

		// Finish onboarding process with any logic
		sdkProvider.innerHTML = '<div class="onboarding-finished">ONBOARDING FINISHED</div>';
	}
	
	// Other events

	if (selphiWidget) {
		selphiWidget.addEventListener('extractionFinish', handleExtractionFinish);
	}
}
```

{% endcode %}

{% hint style="info" %}
This is only an approximation. Different types of integration or syntax can be used.

This example only takes into account the *happy path*, so the remaining events also need to be integrated in real-world implementations.
{% endhint %}

***

### Component Flow Integration and Configuration

This example is an integration of the *Happy Path* for an Onboarding carried out using SelphID and Selphi.

First, when the SDK Provider is instantiated, the internal logic to initialize all services (Loading, Licensing, Tracking Initialization, etc.) will run.

When the Provider emits the `emitData` event, the first Widget (SelphID) will be loaded using the `initSelphidWidget(sdkProvider)`.

Once the user has completed the capture process, the SelphID Widget will close and emit the `extractionFinish` event. The data will be processed as needed in this event, and the next component (Selphi) will be loaded using the method `initSelphiWidget(sdkProvider);`.

Finally, when the user has finished capturing the facial pattern, the data obtained will be processed as needed and the flow will end, continuing with whatever logic is desired.

#### Flow Between Components

In more comprehensive environments, such as modern frameworks, Components, Routers, Stores, and all kinds of tools can be used to implement this flow.

The Facephi team recommends using components and routers for a more modular, straightforward, and faster integration into the framework of your choice.

***

### Behavioral testing

Once the Integration is complete, it is highly recommended to test in the application’s development environment to ensure that it works as expected, taking into account the possible configurations and scenarios that may arise in the end users’ Flow.

***
