1. Home
  2. Docs
  3. NPM package
  4. Basic Usage
  5. Basic code example

Basic code example

Overview

In this section, we present a straightforward code example to illustrate the fundamental operations of the @face-auth/face-id package. This example is designed to help users quickly grasp how to utilize the core features of the package without delving into detailed setup or configuration. It provides a practical starting point for integrating the package into projects.

Example Code

Here’s a basic example demonstrating how to use the main functionalities of the @face-auth/face-id package:

// Import the package
import { FaceId } from "@face-auth/face-id";

// Initialize the package with domain and client token
const faceId = new FaceId('<domain>', '<client_token>');

// Register a new user with a face image
async function registerUser(userName: string, photo) {
    const imageType = 'image/png';
    photo.toBlob(async (imageBlob) => {    
        try {
            let result = await faceId.register(userName, imageBlob, imageType);
            console.log('User registered:', result);
        } catch (error) {
            console.error('Error registering user:', error);
        }
    }, imageType);
}

// Identify a face from an image
async function identifyFace(imagePath) {
    const imageType = 'image/png';
    photo.toBlob(async (imageBlob) => {    
        try {
            let result = await faceId.identifyFace(imageBlob, imageType);
            console.log('Face identified successfully:', result);
        } catch (error) {
            console.error('Error identifying user:', error);
        }
    }, imageType);
}

TypeScript

In this example:

  • We import the @face-auth/face-id package.
  • Initialize the package with the domain and client token.
  • Provide functions to register a new user and identify a face, showcasing how to call the core methods of the package.

How can we help?