SavyScan SDK 0.5.0-beta.7
API reference
The approved commercial beta contract, documented with explicit evidence and support boundaries.
01 / Module surface
Exports
0.5.0-beta.7 approved beta. The live demo and release build include SavyScanError and early validation.
// These imports compile against the approved 0.5.0 beta release.
import SavyScan, { create, version } from "@savyscan/web-sdk";
import "@savyscan/web-sdk/styles.css";
// 0.5.0-beta.7 source also provides a named SavyScanError export.
const ErrorType = SavyScan.SavyScanError;| Export | Type | Contract |
|---|---|---|
default | SavyScanSDK | Object containing version, create, and SavyScanError. |
version | string | "0.5.0-beta.7" in the approved beta release. |
create | (options) => instance | Creates and mounts one hidden scanner interface. |
SavyScanError | Error subclass | 0.5.0-beta.7 structured configuration and runtime error. |
window.SavyScan | SavyScanSDK | Preferred global exposed by the direct browser bundle. |
window.DocumentScannerSDK | SavyScanSDK | Deprecated migration alias retained through 0.5.x. |
The runtime contains internal test helpers, but they are not part of the supported customer API and may change without a semver guarantee.
02 / Configuration
create(options)
Creates an SDK-owned root under document.body by default. Call it after the body exists, or provide a valid mount target. The option set below is shared by 0.4.0 and 0.5.0-beta.7; validation descriptions refer to the beta.
| Option | Default | Supported behavior |
|---|---|---|
openCvUrl | Required | Non-empty HTTP(S) URL, including a relative URL, to pinned OpenCV.js. Prefer same-origin OpenCV 4.13.0. |
mountTarget | document.body | Element, non-empty selector resolving to an element, or null. A supplied legacy root retains precedence. |
maxPages | 40 | Positive safe integer. |
maxPdfBytes | 12 MiB | Positive safe integer; maximum completed PDF size. |
maxPageBytes | 8 MiB | Positive safe integer; maximum corrected JPEG page size. |
maxSourceDimension | 3000 | Positive safe integer; maximum decoded or captured source edge. |
maxOutputDimension | 2600 | Positive safe integer; maximum corrected page edge. |
maxOutputPixels | 5,400,000 | Positive safe integer; maximum corrected page area. |
jpegQuality | 0.9 | Finite number from 0 through 1. |
defaultEnhancement | "grayscale" | Exactly grayscale, enhanced_color, black_white, or original. |
automaticCapture | true | Boolean; false starts in manual mode. |
labels | {} | Known keys with non-empty string values. |
theme | {} | Known keys with non-empty CSS-value strings. |
onComplete | No-op | Function receiving the completed result. A returned promise is awaited. |
onClose | No-op | Function receiving { completed: boolean }. |
onCancel | No-op | Function receiving { completed: false }. |
onError | No-op | Function receiving SavyScanError in 0.5.0-beta.7 source. |
onStateChange | No-op | Function receiving announced state messages and tones. |
onDiagnostic | No-op | Opt-in callback receiving frozen privacy-safe lifecycle events. SavyScan never transmits them. |
Privacy-safe diagnostics
onDiagnostic(event) {
// Closed schema: no pixels, text, references, filenames, URLs,
// device identifiers, wall-clock timestamps, or host metadata.
telemetry.record(event);
}Every event includes schemaVersion, sdkVersion, name, session-relative elapsedMs rounded to 100 ms, and pageCount. Applicable events may add only captureMode, automaticCapture, cameraAvailable, completed, errorCode, or recoverable. Callback failures are ignored and cannot interrupt scanning.
Advanced automatic-capture controls
| Option | Default | Meaning and beta range |
|---|---|---|
autoCaptureAnalysisDimension | 720 | Positive safe integer; analysis-frame maximum edge. |
autoCaptureIntervalMs | 350 | Positive finite target delay; scheduling is clamped internally to 300–1200 ms. |
autoCaptureWarmupMs | 1500 | Non-negative finite delay before analysis. |
autoCaptureStableMs | 900 | Non-negative finite required stable duration. |
autoCaptureStableFrames | 4 | Positive safe integer; required stable-frame count. |
autoCaptureMaxDriftRatio | 0.045 | Finite ratio from 0 through 1 between adjacent frames. |
autoCaptureMaxAnchorDriftRatio | 0.08 | Finite ratio from 0 through 1 from the stability anchor. |
autoCaptureMaxContentDifference | 14 | Finite adjacent-frame difference from 0 through 255. |
autoCaptureMaxAnchorContentDifference | 22 | Finite anchor difference from 0 through 255. |
autoCaptureMinimumSharpness | 110 | Non-negative finite automatic-detection threshold. |
captureMinimumSharpness | 125 | Non-negative finite final quality gate. |
autoCaptureSharpnessDropRatio | 0.88 | Finite ratio greater than 0 and no greater than 1. |
autoCaptureSharpnessGainRatio | 1.15 | Finite ratio of at least 1. |
autoCaptureMissToleranceMs | 700 | Non-negative finite ready-state tolerance. |
autoCaptureMissToleranceFrames | 2 | Non-negative safe integer; brief missed detections allowed without restarting the hold. |
autoCaptureAnnouncementIntervalMs | 1600 | Non-negative finite guidance interval. |
03 / Instance
Methods
open(options?)
Shows the scanner, locks body scrolling, makes background siblings inert, and begins asynchronous OpenCV and camera setup. It returns void, throws after destruction, and is a no-op for valid options if already open.
scanner.open({
referenceLabel: "Purchase Order",
referenceNumber: "5C.143430",
returnFocusElement: launchButton,
});The 0.5.0-beta.7 source validates open(options) before it mutates session state. References may be strings or finite numbers, returnFocusElement is an element or null, and a session-only maxPages must be a positive safe integer. The deprecated purchaseOrderNumber input remains accepted for backward compatibility.
close({ completed = false }?)
Stops the camera, clears pages and canvases, revokes SDK-owned object URLs, hides the interface, and restores page state. Consumers normally call close(); completed close is primarily the SDK’s internal success path.
destroy()
Idempotently closes if necessary, removes listeners, clears resources, and removes an SDK-owned root. The instance cannot reopen.
04 / Completion value
Result
type SavyScanResult = {
pdfBlob: Blob; // application/pdf
previewBlob: Blob; // image/jpeg — first corrected page
pageCount: number;
};previewBlob is not a PDF preview. It is the first corrected page as JPEG, useful when a mobile browser cannot render the PDF Blob inline.
05 / Event order
Callbacks
| Callback | Payload | Behavior |
|---|---|---|
onComplete | SavyScanResult | Runs first on success. Its returned promise is awaited before the completed close. |
onClose | { completed } | Runs after every completed or cancelled close. |
onCancel | { completed: false } | Runs before onClose when the scan is not completed. |
onStateChange | { message, tone } | Tones: info, success, warning, error, or working. |
onError | SavyScanError | In 0.5.0-beta.7, receives reported operational failures, including recoverable camera warnings and host callback failures. Setup errors may arrive before open() returns. |
Success order: onComplete → await returned value → onClose({ completed: true }). A rejected completion promise leaves the scanner open and reports HOST_CALLBACK_FAILED.
06 / Appearance
Labels and theme
| Theme key | SDK default |
|---|---|
primaryColor | #3b82f6 |
primaryTextColor | #ffffff |
surfaceColor | #090d14 |
reviewSurfaceColor | #151a22 |
textColor | #f8fafc |
borderRadius | 0.875rem |
fontFamily | Inter/system sans stack |
Supported labels are title (default “SavyScan”) and cameraPlaceholder (default “Camera preview will appear here.”).
07 / 0.5.0-beta.7 contract
Structured validation and errors
The 0.5.0 beta source validates create(options) and open(options) synchronously, before UI, camera, or session side effects. It rejects unknown top-level and nested keys instead of coercing or silently ignoring them. Reported operational failures use the same error type through onError; camera capability errors may be delivered before open() returns.
{
name: "SavyScanError",
code: "CONFIG_INVALID_VALUE",
message: "maxPages must be at least 1.",
field: "maxPages",
recoverable: false
}Use code and field for program flow; messages may improve between releases. recoverable means the same instance can continue or retry. The optional underlying cause is deliberately omitted by toJSON(), and configured values or URLs are not copied into error messages.
| Code | When it is used |
|---|---|
CONFIG_REQUIRED | A required options object or openCvUrl is missing. |
CONFIG_UNKNOWN_KEY | An unsupported create, open, label, or theme key was supplied. |
CONFIG_INVALID_TYPE | A value has the wrong JavaScript type or a numeric value is not finite. |
CONFIG_INVALID_VALUE | A numeric range, enum, URL protocol, or selector is invalid. |
ENVIRONMENT_UNSUPPORTED | A required DOM, Canvas, Blob, URL, text-encoding, or WebAssembly capability is missing. |
MOUNT_TARGET_NOT_FOUND | The configured selector does not resolve to an element. |
SCANNER_ROOT_INVALID | A legacy supplied root does not contain the complete scanner structure. |
OPENCV_LOAD_FAILED | The pinned processor asset fails to load or times out. |
OPENCV_INITIALIZATION_FAILED | The processor loads but cannot become ready. |
CAMERA_PERMISSION_DENIED | The user or browser denies live-camera access; Photos remains available. |
CAMERA_UNAVAILABLE | No compatible live camera API or device is available; Photos remains available. |
CAMERA_START_FAILED | The live camera cannot start; Photos remains available. |
INSTANCE_DESTROYED | open() is called after permanent destruction. |
PROCESSING_FAILED | Capture, correction, output, or another scanning operation fails. |
HOST_CALLBACK_FAILED | A notification callback other than onError throws synchronously, or onComplete throws or rejects; rejected completion remains retryable. |
Release boundary. Structured validation is part of the exact 0.5.0-beta.7 contract. Do not assume it exists in earlier package versions.