Initialization
Initialize the widget in your app code after the SDK is available and your target container exists.
Configuration object
init() accepts:
- Required
apiKey: stringcontainer: string | HTMLElement- Optional
userId?: stringuserName?: stringmetadata?: Record<string, unknown>width?: stringheight?: stringonReady?: () => voidonError?: (error: Error) => voidenvironment?: "test" | "production"
Example
const widget = new window.SophiWebSDK.SophiWidget();
await widget.init({
apiKey: "YOUR_SOPHI_API_KEY",
container: "#sophi-widget-container",
userId: "user-123",
userName: "Jane",
environment: "production",
width: "100%",
height: "600px",
metadata: {
channel: "web",
locale: "en-US",
},
onReady: () => {
console.log("Sophi widget ready");
},
onError: (error) => {
console.error("Sophi widget init error", error);
},
});
Recommended flow
- Ensure cookie consent is granted (if required by your policy)
- Ensure SDK script is loaded (or npm bundle is ready)
- Ensure container is mounted in DOM
- Call
await widget.init(...) - Register event listeners (
on(...))
Note
Keep apiKey in your app configuration layer. Never hardcode production secrets in publicly shared examples.