Web Widget SDK

A framework-agnostic JavaScript library for embedding Voice AI into any website.

View as Markdown

The Iqra AI Web Widget is a lightweight JavaScript library that handles the complexity of WebRTC audio streaming, microphone permissions, and connection state management.

It supports two modes:

  1. Inline Form: A pre-built, styled UI that renders inside a container.
  2. Headless: Full programmatic control to build your own UI.

Installation

Currently, the SDK is available via CDN. Add this script to your HTML file (usually before </body>).

<script src="https://cdn.iqra.bot/sdk/voice-ai-widget.umd.js"></script>

Mode 1: Turnkey (Inline Form)

Use this if you want to get up and running instantly. The SDK will render a form collecting user details before starting the call.

HTML:

<div id="voice-widget-container"></div>

JavaScript:

const client = VoiceAiWidget.init({
    // 1. URL of your middleware (Required)
    middlewareUrl: 'https://api.your-middleware.com',

    // 2. Where to render the widget (Required for UI mode)
    container: '#voice-widget-container',

    // 3. Define the form fields
    formFields: [
        { 
            name: 'firstName', 
            label: 'First Name', 
            type: 'text', 
            target: 'dynamicVariable', // Sends as {{ firstName }} to Agent
            required: true 
        },
        { 
            name: 'email', 
            label: 'Email Address', 
            type: 'email', 
            target: 'metadata', // Sends as call metadata
            required: true 
        }
    ]
});

Mode 2: Headless (Custom UI)

Use this if you want to build your own "Start Call" button or integrate deeply into a React/Vue app.

JavaScript:

// Initialize without a 'container'
const client = VoiceAiWidget.init({
    middlewareUrl: 'https://api.your-middleware.com'
});

// 1. Listen to state changes to update your UI
client.on('stateChange', ({ state, data }) => {
    console.log('Current State:', state); 
    
    if (state === 'QUEUED') {
        alert(`System busy. You are position #${data.queuePosition} in line.`);
    }
});

// 2. Start the session manually
function onStartClick() {
    client.startSession({
        dynamicVariables: { firstName: 'Ali' },
        metadata: { source: 'custom-ui' }
    });
}

// 3. Hang up
function onHangupClick() {
    client.hangUp();
}

Configuration Reference

The init(options) object accepts:

OptionTypeRequiredDescription
middlewareUrlstringYesThe base URL of your hosted Middleware.
containerstringNoCSS selector (e.g., #app). If provided, UI mode is enabled.
formFieldsArrayNoDefinitions for the input form (UI mode only).

Form Field Object

{
    name: string;       // Unique ID
    label: string;      // Display text
    type?: 'text' | 'email' | 'tel';
    target: 'dynamicVariable' | 'metadata';
    required?: boolean;
}

Events & States

The stateChange event is the heartbeat of the SDK.

client.on('stateChange', (payload) => {
    const { state, data } = payload;
});
StateDescriptionData Payload
IDLEReady to start. No active call.undefined
CONNECTINGContacting middleware/server.undefined
QUEUEDWaiting for a slot (Concurrency limit).{ queuePosition: number }
CONNECTEDCall is live. Audio is streaming.undefined
ERRORAn error occurred (Mic denied, Network fail).{ message: string }

Requirements

HTTPS Required

Modern browsers block Microphone access (getUserMedia) on insecure origins. Both your website and your middleware must be served over HTTPS (localhost is exempt for testing).

On this page