Browser SDK Initial Integration Guide
Use this guide to integrate the FaceTec Browser SDK into Your App.
For integrating the FaceTec Android SDK into Your App, see the Android SDK Initial Integration Guide.
For integrating the FaceTec iOS SDK into Your App, see the iOS SDK Initial Integration Guide.
Video Guide
Prerequisite
Install Node.js (with npm).
Create a New Browser Web App:
- In Your App's Root Directory, create a basic index.html with the Source Directory specified for the JavaScript Portion (i.e. /dist/main.js).
<!DOCTYPE html> <head> <title>FaceTec Browser SDK App</title> <meta charset="utf-8"/> <!-- These settings are highly recommended for good app behavior. --> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <script type="text/javascript" src="./dist/main.js"></script> </head> <body> </body> </html> - Create a src directory. In src, create the file index.ts.
- Open another terminal in Your App's folder.
- Run “npm init -y”.
- Run “npm i -D webpack-cli webpack typescript ts-loader @types/node @types/webpack”.
- Run “npx create-new-webpack-app”.
- Answer the questions as follows:
? Which of the following JS solutions do you want to use? Typescript ? Do you want to use webpack-dev-server? No ? Do you want to simplify the creation of HTML files for your bundle? No ? Do you want to add PWA support? No ? Which of the following CSS solutions do you want to use? none ? Which package manager do you want to use? npm [webpack-cli] ℹ Initializing a new Webpack project - File conflict at index.html? do not overwrite skip index.html create webpack.config.js - File conflict at packge.json? overwrite overwrite package.json create README.md create tsconfig.json - File conflict at index.ts? do not overwrite -skip src/index.ts - Run "npm install live-server".
- In package.json, specify the Application Name, the Main Source File, and the script for running "live-server" from localhost.
"name": "MyFaceTecApp", "main": "src/index.ts", "scripts": { "local-server": "live-server --host=localhost --port=8000" ... } - Run "npm run local-server".
- In a second terminal, run "npm run watch".
- On success, ./src/index.ts will build successfully, webpack will compile, and a blank webpage titled "FaceTec Browser SDK App" will be shown.
Add the FaceTec Browser SDK to Your New App:
- Terminate the process that is running "npm run watch".
- Download and extract the FaceTec Browser SDK. Copy the directory core-sdk and place it into Your App's folder.
- In src/index.ts, import the FaceTec SDK.
import { FaceTecSDK } from "../core-sdk/FaceTecSDK.js/FaceTecSDK"; - Rerun "npm run watch".
- On success, ./src/index.ts will build successfully, webpack will compile, and a blank webpage titled "FaceTec Browser SDK App" will be shown.
Import the Default FaceTec Wrapper Classes & Methods:
- Terminate the process that is running "npm run watch".
- Copy Config.js into Your App's src directory.
- From the Sample App, copy the following files into Your App's src directory:
- SessionRequestProcessor.ts
- SampleAppNetworkingRequest.ts
- In src/index.ts, import FaceTecInitializationError, FaceTecSDKInstance, and FaceTecSessionStatus.
import { FaceTecInitializationError, type FaceTecSDKInstance, type FaceTecSessionStatus } from "../core-sdk/FaceTecSDK.js/FaceTecPublicApi"; - In src/SessionRequestProcessor.ts, update the paths for importing classes from FaceTecPublicApi and SampleAppNetworkingRequest. Additionally, remove the line for importing
SampleAppController.import { FaceTecSessionRequestProcessor, FaceTecSessionResult, type FaceTecSessionRequestProcessorCallback } from "../core-sdk/FaceTecSDK.js/FaceTecPublicApi"; import { SampleAppNetworkingRequest } from "./SampleAppNetworkingRequest"; - In src/SessionRequestProcessor.ts, update
onFaceTecExitto be a Basic Return.onFaceTecExit = (faceTecSessionResult: FaceTecSessionResult): void { // Handle the FaceTecSessionResult with Your Custom Logic. return; } - In src/SampleAppNetworkingRequest.ts, update the paths for importing classes from Config, FaceTecSessionRequestProcessorCallback, FaceTecSDK, and SessionRequestProcessor. Additionally, remove the line for importing
SampleAppControllerandDeveloperStatusMessages.import { Config } from "./Config"; import { FaceTecSessionRequestProcessorCallback } from "../core-sdk-FaceTecSDK.js/FaceTecPublicApi"; import { FaceTecSDK } from "../core-sdk/FaceTecSDK.js/FaceTecSDK"; import { SessionRequestProcessor } from "./SessionRequestProcessor"; - In src/SampleAppNetworkingRequest.ts, remove all references to
SampleAppController,DeveloperStatusMessages, andstoreOfficialIDPhotoIfApplicable. - In src/index.ts, import Config and SessionRequestProcessor.
import { Config } from "./Config"; import { SessionRequestProcessor } from "./SessionRequestProcessor"; - Rerun "npm run watch".
- On success, ./src/index.ts will build successfully, webpack will compile, and a blank webpage titled "FaceTec Browser SDK App" will be shown.
Create and Implement the LivenessCheck Button:
- In index.html, create a button with id=”liveness-button” and onclick=”MyFaceTecApp.onLivenessCheckPressed()".
<style> button { height: 20px; width: auto; } .wrapper { width: 100%; height: 100%; text-align: center; } </style> <div class="wrapper"> <button id="liveness-button" onClick="MyFaceTecApp.onLivenessCheckPressed()">3D Liveness Check</button> </div> - In MyApp/src/index.ts, create and export the class MyApp with a private FaceTecSDKInstance property. Afterwards, create an initialization function that does the following:
- Sets the directory path for FaceTec Browser SDK Resources
- Sets the directory path for FaceTec Browser SDK Images
- Initializes the FaceTec SDK calling FaceTecSDK.initializeWithSessionRequest(...)
export class MyApp { private faceTecSDKInstance!: FaceTecSDKInstance; public init = ():void => { // Set the directory path for FaceTec Browser SDK Resources. FaceTecSDK.setResourceDirectory("../core-sdk/FaceTecSDK.js/resources"); // Set the directory path for FaceTec Browser SDK Images. FaceTecSDK.setImagesDirectory("../core-sdk/FaceTec_images"); // Initialize FaceTec Browser SDK. FaceTecSDK.initializeWithSessionRequest(Config.DeviceKeyIdentifier, new SessionRequestProcessor(), { onSuccess: (newFaceTecSDKInstance: FaceTecSDKInstance) => { this.faceTecSDKInstance = newFaceTecSDKInstance; this.onFaceTecSDKInitializationSuccess(); }, onError: this.onFaceTecSDKInitializationFailure }); } ... } - In src/index.ts, create empty methods for
onFaceTecSDKInitializationSuccessandonFaceTecSDKInitializationFailure.// Finish setup after initialization is successful. private onFaceTecSDKInitializationSuccess = (): void => { }; // Handle the case where initialization is not successful. private onFaceTecSDKInitializationFailure = (initializationError: FaceTecInitializationError): void => { }; - In src/index.ts, create the
onLivenessCheckPressedMethod to configure the 3D Liveness Check Button to perform 3D Liveness Checks.// Initiate a 3D Liveness Check. public onLivenessCheckPressed = (): void => { const sessionRequestProcessor: SessionRequestProcessor = new SessionRequestProcessor(); this.faceTecSDKInstance.start3DLiveness(sessionRequestProcessor); }; - In src/index.ts, but outside of the
MyAppClass, add the method for initializing the SDK when the browser is loaded.window.onload = ():void => { const MyFaceTecApp = new MyApp(); MyFaceTecApp.init(); // @ts-ignore // Make the class available to the html file. window.MyFaceTecApp = MyFaceTecApp } - Rerun "npm run watch".
- On success, Your App will be able to build and run!
Additional Note: Incorrect Response MIME Type
Browser SDK uses a .wasm file that may require extra configuration on your web server. You may need to add "application/wasm" to the MIME Type configuration of your web server. Each web server may have a different process and we recommend referring to your server manual.
Important Note: Your Application needs to wait for the FaceTec SDK to initialize before performing a 3D Liveness Check.