This README explains how to integrate **Device Intelligence SDK** into your app. It covers setup, login/signup flows, where to place your custom logic, and a placeholder for transactions.
npm install device-intelligence-sdkThis README explains how to integrate Device Intelligence SDK into your app. It covers setup, login/signup flows, where to place your custom logic, and a placeholder for transactions.
---
sdk-utils.js) app.js) ---
- Install SDK:
``bash
npm install device-intelligence-sdk --registry http://10.10.10.97:4873
---
* Project must support ES modules.
* Ensure your serverUrl is correct and reachable.
* Example environment:
* React (hooks used in snippets)
* A reachable Device Intelligence server (http://localhost:8010?EIO=4&transport=websocket in dev)
---
)Create a shared SDK instance for your app:
`js
// sdk-utils.js
import { DeviceIntelligenceSDK } from "device-intelligence-sdk";
export const devIntelligenceSdk = DeviceIntelligenceSDK({
productId: "proj_456",
serverUrl: "http://localhost:8010?EIO=4&transport=websocket",
});
`
---
)Start SDK once and subscribe to alerts:
`js
// app.js
import React, { useEffect } from "react";
import { devIntelligenceSdk } from "./sdk-utils";
function App() {
useEffect(() => {
let subscription;
devIntelligenceSdk.start()
.then(() => {
subscription = devIntelligenceSdk.getGlobalAlertListener().subscribe((data) => {
console.log("Alert from Global Listener", data);
// === CUSTOM LOGIC PLACE ===
// Example: show toast, open alert modal, block UI, or log analytics
});
})
.catch((error) => {
console.error("SDK failed to start:", error);
});
return () => {
if (subscription?.unsubscribe) {
subscription.unsubscribe();
}
};
}, []);
return
export default App;
`
---
`js
const signin = async () => {
try {
const resp = await devIntelligenceSdk.trackLoginAsync({ userId: email });
console.log("login response", resp);
if (resp.action === "Alert") {
alert(
Alert: Detected ${resp.casesPassed[0].condition.left.name} : ${resp.casesPassed[0].actual}. But login to Continue
);
// === CUSTOM LOGIC PLACE ===
// Example: require 2FA, log event, or show warning modal
devIntelligenceSdk.trackLoginSuccess({});
navigate("/dashboard");
} else if (resp.action === "Block") {
alert(
Block: Detected ${resp.casesPassed[0].condition.left.name} : ${resp.casesPassed[0].actual}. Login Blocked
);
// === CUSTOM LOGIC PLACE ===
// Example: show error view, log to monitoring system
devIntelligenceSdk.trackLoginFail({ reason: "Block Detected" });
} else {
// Allow
devIntelligenceSdk.trackLoginSuccess({});
navigate("/dashboard");
}
} catch (error) {
console.error("signin error:", error);
// === CUSTOM LOGIC PLACE ===
// Example: show error to user or retry
}
};
`
---
`js
const signup = () => {
devIntelligenceSdk.updateUserId(email);
devIntelligenceSdk.trackSignUp({
promoCodeUsed: promoCode,
});
// === CUSTOM LOGIC PLACE ===
// Example: validate promo, run extra verification
devIntelligenceSdk.trackSignUpSuccess({
promoCodeUsed: promoCode,
});
navigate("/dashboard");
};
`
---
The SDK supports async transaction tracking with alert/block decisions, similar to login.
`js
const submitTransaction = async (txPayload) => {
try {
const resp = await devIntelligenceSdk.trackTransactionAsync({
txnId: txPayload.txnId,
transactionType: txPayload.type,
amount: txPayload.amount,
currency: txPayload.currency,
userId: email,
});
if (resp.action === "Alert") {
alert(Transaction Alert: ${resp.casesPassed[0].condition.left.name} → ${resp.casesPassed[0].actual});Transaction Blocked: ${resp.casesPassed[0].condition.left.name}
// === CUSTOM LOGIC PLACE ===
// Example: require OTP, pause transaction for manual review
devIntelligenceSdk.trackTransactionSuccess({
txnId: txPayload.txnId,
amount: txPayload.amount,
});
} else if (resp.action === "Block") {
alert();`
// === CUSTOM LOGIC PLACE ===
devIntelligenceSdk.trackTransactionFail({
txnId: txPayload.txnId,
reason: "Blocked by rules",
});
} else {
// Allow
devIntelligenceSdk.trackTransactionSuccess({
txnId: txPayload.txnId,
amount: txPayload.amount,
});
}
} catch (error) {
console.error("Transaction error:", error);
devIntelligenceSdk.trackTransactionFail({
txnId: txPayload.txnId,
reason: "SDK error",
});
}
};
---
| Function | Description |
| ---------------------------------------- | ---------------------------------------------------- |
| DeviceIntelligenceSDK(config) | Create SDK instance with { projectId, serverUrl }. |start()
| | Start SDK and connect. |getGlobalAlertListener().subscribe(cb)
| | Subscribe to global alerts. |trackLoginAsync({ userId })
| | Send login attempt and return decision. |trackLoginSuccess(meta)
| | Notify successful login. |trackLoginFail({ reason })
| | Notify failed login. |updateUserId(userId)
| | Update user identifier. |trackSignUp(meta)
| | Track signup attempt. |trackSignUpSuccess(meta)
| | Notify signup success. |trackTransactionAsync(params)
| | Send transaction attempt and return decision. |trackTransactionSuccess(params)
| | Notify successful transaction. |trackTransactionFail(params)
| | Notify failed transaction. |
---
* SDK fails to start → check serverUrl and use wss:// in production if needed.start()
No alerts → ensure subscription happens after* .Alert
* Duplicate alerts → export a singleton SDK instance and clean up listeners.
* Testing → use staging projectId to simulate and Block.projectId
* Security → never expose secrets; is safe to embed.resp
* Logging → inspect from trackLoginAsync` to understand backend decisions.
---
* Global alerts: show toast, block actions, log.
* Sign-in Alert: require 2FA, log warning, allow with caution.
* Sign-in Block: stop login, show error, track failure.
* Sign-up: validate promo, verify user, decide success/fail.
* Transactions: Transactions: handle allow/alert/block before committing.