Initialization
Initialization validates the license and prepares the session for measurement. Initialize once per user session. Use an SDK key during development or a short-lived token from your backend in production. Wait for initialization to succeed before calling measurement APIs.
The examples use credential for that value and the shared default user profile. To identify individual users, pass an opaque user ID.
When using the device camera, set up camera access first. For the embedded UI, put the scan canvas on the page before loading the SDK.
import CreateShenaiSDK from "@shenai/sdk"
const shenaiSDK = await CreateShenaiSDK({})
shenaiSDK.initialize(credential, "", {}, result => {
if (result !== shenaiSDK.InitializationResult.OK) {
console.error("SDK initialization failed", result)
return
}
// The session is initialized.
})CreateShenaiSDK returns a promise that resolves when the Web runtime has loaded. initialize reports the session result through its callback. Put code that needs the session in the success branch.
Error handling
| Initialization result | What to check |
|---|---|
INVALID_API_KEY | Check your license and credential. If the token expired, request a new one. |
CONNECTION_ERROR | Check that the device can reach the licensing server. |
INTERNAL_ERROR | Record the result and the SDK logs. |
If CreateShenaiSDK rejects, initialization has not started. Check the browser error in Web SDK troubleshooting.
Scan input and display
The SDK attaches to a canvas with the ID mxcanvas automatically. Size it with CSS and give its container a height. The SDK sets the drawing buffer resolution.
<canvas id="mxcanvas" style="width: 100%; height: 100%"></canvas>On mobile, include <meta name="viewport" content="width=device-width, initial-scale=1"> in the page head so the canvas uses the device viewport and the page remains zoomable.
If you use a different canvas ID, pass its ID to the loader for the preload display and attach the SDK to the same element before initialization.
const shenaiSDK = await CreateShenaiSDK({ preloadDisplayCanvasId: "scan" })
shenaiSDK.attachToCanvas("#scan")To render to an additional canvas, use attachToCanvas("#second-scan", false). The default replaces the previous canvas.
For a fully custom UI, your app supplies the scan interface.
Monitoring required model downloads
Initialization can succeed while models are still downloading. Before starting a scan from your app, check that the SDK is ready to start a measurement. Use the download percentage to display progress.
const ready = shenaiSDK.isReadyToStartMeasurement()
const percent = shenaiSDK.getRequiredModelsDownloadProgressPercentage()Download progress ranges from 0 to 100. A zero can mean progress is still unknown. For a custom Start button, use the readiness check. Even after downloads finish, the SDK may still be waiting for the face to be in position.
Initialization settings
The defaults are enough for a first scan. To customize a session, use the settings below.
Pass settings as the third argument to initialize in place of the empty object.
const settings = { showFaceMask: false }Options that apply while the Web runtime loads go in CreateShenaiSDK.
| Loader option | Use it to |
|---|---|
onWasmLoadingProgress | Display the WebAssembly download percentage (0–100). For model files, use the model download percentage. |
enablePreloadDisplay, hidePreloadDisplayLogo | Set the first to false to hide the loading screen, or the second to false to show the Shen AI logo on it. |
enableErrorReporting | Set to false to disable built-in error reporting. |
To set the order of the embedded screens, supply a nonempty uiFlowScreens list. It takes precedence over the individual screen flags.
For blood pressure calibration, set initializationMode to CALIBRATION to collect calibration data or CALIBRATED_MEASUREMENT to use a saved calibration.
If your license or token supplies a remote configuration, see which settings take precedence.
Tracking the measurement
Pass eventCallback in the settings for initialize.
- On
MEASUREMENT_FINISHED, read the measurement results. - On
USER_FLOW_FINISHED, return to your app when the user finishes the embedded flow. - To follow user interactions, handle
START_BUTTON_CLICKED,STOP_BUTTON_CLICKED, orSCREEN_CHANGED. Changing the screen from your own code does not emitSCREEN_CHANGED.
Deinitialization
When a session ends, stop any polling or subscriptions your app started and release the session before initializing again.
Use deinitialize() when the user may scan again on the same page. You can initialize another session on the same SDK instance.
shenaiSDK.deinitialize()When your app is finished with the runtime, call destroyRuntime() and drop references to the instance. This also ends an active session. A later scan needs a new instance from CreateShenaiSDK.
shenaiSDK.destroyRuntime()