Web SDK

Add Sanad to your website or web app.

The web SDK opens a Sanad session from your site, reads the browser's signals in the background and sends them to Lean, so your backend can read the session's assessment. Use the same SDK key as your Android and iOS apps, so Lean assesses all your sessions together.

📘

Prerequisites

Before you add the SDK, complete the setup in Getting started:

  • Ask your Lean representative to enable Sanad for your application.
  • Get your SDK key from the Application Dashboard.
  • Authentication: the access token your backend uses to read assessments.

This page covers only the SDK integration in your site.

1. Add the loader

The web SDK is served by Lean. Add the loader to your page with a script tag:

<script src="https://sanad.leantech.me/loader/1.0.0/lean-device-loader.js" crossorigin="anonymous"></script>
import { LeanDevice } from "https://sanad.leantech.me/loader/1.0.0/lean-device-loader.mjs";

The loader runs the SDK in a small hidden frame on Lean's domain, so the SDK, its storage and its calls to Lean stay separate from your page.

2. Start the SDK

Call LeanDevice.init once, as early as you can: when your site or web app loads, and at the latest before your login page. The first signals then reach Lean straight away, so an assessment is ready by the time your user logs in.

const { LeanDevice } = LeanDeviceWebLoader; // with the script tag; the ES module exports LeanDevice directly

const sessionId = await LeanDevice.init({ sdkKey: "lean_device_…" });
// Send sessionId to your backend with a request it already makes (a login, a payment).
// Null means there is no session: carry on without Sanad, and see LeanDevice.diagnostics().

init opens a session with Lean and resolves its id. Then, by default, it reads the browser's signals once in the background and sends them to Lean.

init rejects only when the key is blank (LeanDeviceError with the code missing_sdk_key). Every other failure resolves null, and diagnostics().lastError says why, for example frame_unavailable when Lean's frame couldn't load. Nothing the SDK does reaches your page as an error. Loading the loader touches no browser API, so it's safe during server-side rendering.

Configuration

ParameterDefaultDescription
sdkKeyRequiredYour SDK key. It also selects the Lean environment
allowPromptsfalseLets the SDK show the browser's location prompt, once per browser profile
collectOnInittrueRead and send signals as soon as the session opens
onSessionChange-Reserved for a future release

3. Collect again before sensitive actions

Call collect() right before each later sensitive action, such as a payment or a change of payout details, so Lean assesses the browser as it is at that moment:

async function onConfirmPayment() {
  LeanDevice.collect(); // returns at once; the work runs in the background
  // ... then send your payment request with the session id
}

A session sends at most 10 sets of signals, counting the one from init. After that, lastError is envelope_cap.

4. Send the session id to your backend

Send the session_id to your backend with the login request, so your backend can read the assessment before it decides on the login, and add friction such as an OTP if there are red flags. Your backend keeps it for the rest of the session, so later requests don't need to carry it again.

📘

Tip: link the session to your customer

Once your user logs in, consider linking the session to their Lean customer. Sanad can then also compare the device with that customer's other devices and locations.

What your page needs

If your site sets a Content Security Policy, allow Lean's domain for the loader script and for its frame:

Content-Security-Policy: script-src 'self' https://sanad.leantech.me; frame-src https://sanad.leantech.me

The SDK's calls to Lean are made from inside Lean's frame, so your policy doesn't need to allow them. If the frame is blocked, init resolves null with frame_unavailable.

What the SDK stores in the browser

The SDK stores nothing in your site's storage and sets no cookies. Its key and the device token Lean issues are kept by Lean's frame on sanad.leantech.me, which browsers keep separate for each site that embeds it.

📘

Safari

Safari can delete the frame's storage after 7 days without a visit to your site, so the browser gets a new device token. Lean still recognises the browser from its other signals where it can.

Check your integration

Open your page with a sandbox key and log the diagnostics after init:

console.log(LeanDevice.diagnostics()); // { state, sessionId, lastError }

You should see the state active, a session id and lastError: null. If init resolves null with frame_unavailable, check that your Content Security Policy allows sanad.leantech.me. The codes are explained in Troubleshooting.


Did this page help you?