Skip to main content

init()

init() is a one-time function that creates a MonitorDog SDK instance in the browser and registers the default settings used by the SDK.

This function does not immediately run login or detection. It prepares the SDK instance so your website can call login() after the user signs in.

static init(options: MonitorDogDetectorOptions): Promise<MonitorDogDetector>

Function Arguments

The main parameters for init() are:

  • apiBaseUrl (required): MonitorDog API server URL.
  • sessionTokenProvider (required): Callback called at login() time to fetch the SDK access token. This callback must call your server access token endpoint. Do not use the MonitorDog partner API key directly from the browser.
  • onDetect (required): Callback that receives detection results for each frame. Use it to run your screen blocking or unblocking logic depending on whether a phone is detected.
  • onError: Callback for errors that occur while the SDK is running.
  • video: Video element used by the SDK. If omitted, the SDK creates an internal video element.
  • constraints: Webcam constraints. Use this to set camera selection, resolution, front or rear camera direction, and similar options.
  • runInBackground: Whether detection should continue when the tab is hidden.
  • overlay: Whether to use the SDK default overlay.

init() internally creates the detector instance and registers a singleton so it can later be accessed with getDetector().

Implementation Example

import { MonitorDogDetector } from "@monitordog/detector";

async function getMonitorDogAccessToken({ email }: { email: string }) {
const response = await fetch("/api/monitordog/access-token", {
method: "POST",
headers: {
"content-type": "application/json",
},
credentials: "include",
body: JSON.stringify({ email }),
});

if (!response.ok) {
throw new Error("Failed to create MonitorDog access token");
}

return response.json();
}

const detector = await MonitorDogDetector.init({
apiBaseUrl: "https://api.monitor.dog/v1",

sessionTokenProvider: getMonitorDogAccessToken,

onDetect: (result) => {
if (result.phoneDetected) {
blockScreen();
return;
}

unblockScreen();
},

onError: (error) => {
console.error("MonitorDog SDK error", error);
},
});

Table of Contents

Share This Contents