Live Chat JavaScript API

Use the onWebChat JavaScript API to customize your chat widget and automate chat workflows. You can pass visitor data, set widget language, maximize or hide the widget, and more.

For example, you can send visitor details to chat, set language by page, or open the widget programmatically when a user clicks a button.

Add your API code directly below the chat widget script, as shown on the right.





    <script type='text/javascript'>
        ... onWebChat widget code ...
    </script>
    <script type='text/javascript'>
        ... API functions goes here ...
    </script>



Set Visitor Information

You can set visitor details before or during a chat session. For example, setting the visitor name helps your team identify contacts in the Visitor List.

Use the following API calls to set a visitor's name, email, phone, and notes:



    onWebChat.set("name","John");
    onWebChat.set("email","john@mail.com");
    onWebChat.set("phone","1234567890");
    onWebChat.set("notes","notes for visitor ..");
                        
                        

Get Visitor Information

You can also retrieve visitor details set by the API, pre-chat form, or agent updates.

Call these functions after the widget has fully loaded. Example:
onWebChat.set("onWebChatLoaded", function() { console.log("visitor name: " + onWebChat.get("name")); });



    onWebChat.get("name");
    onWebChat.get("email");
    onWebChat.get("phone");
    onWebChat.get("notes");

Set Widget Language

Set widget language using the language code for any language enabled in your dashboard.

For example, use "en" for English, "es" for Spanish, and "fr" for French. If a language code is not enabled, the widget falls back to your default language. You can update the default language in Settings > Languages.

Available language codes:

am ar az be bg bn bs ca cs da de dv dz el en es et fa fi fil fo fr he hi hr ht hu hy id is it ja ka kk kl km ko ky la lb lo lt lv mg mk mn ms mt my ne nl no pl ps pt ro ru rw si sk sl sm so sq sr sv sw tg th ti tk to tr uk ur uz vi zh zh-TW





    onWebChat.set("language","es");




Set Widget Appearance

You can control key widget display settings:

Use the max parameter with 0 to minimize the widget or 1 to maximize it.

To hide the widget, use the set function with the show parameter and value 0. Use 1 to show it again.





    onWebChat.set("max",1);

    onWebChat.set("show",0);




Set Page Information

You can override the current page URL and title sent to onWebChat:

Use the pageUrl parameter to set a custom URL shown to agents in the chat console. This is useful for single-page apps or custom routing.

Use the pageTitle parameter to set a custom title that agents will see instead of the browser page title.





    onWebChat.set("pageUrl","https://onwebchat.com/newPage");

    onWebChat.set("pageTitle","New title");




Get Widget Status

To retrieve the onWebChat widget status, call:

The function returns "online" or "offline" based on your current chat availability.

Call this after the widget has loaded. Example:
onWebChat.set("onWebChatLoaded", function() { console.log("chat status: " + onWebChat.get("status")); });






    onWebChat.get("status");





Callback Functions

You can run callback functions when specific chat events occur:

onClick runs when a visitor clicks the chat widget

onMaximize runs when the widget is maximized

onMinimize runs when the widget is minimized

onTrigger runs when a trigger is activated

onStartWriting runs when a visitor starts typing

onWebChatLoaded runs when the chat widget finishes loading




    onWebChat.set("onClick",function(){ alert('chat widget is clicked'); });

    onWebChat.set("onMaximize",function(){ alert('chat maximized'); });

    onWebChat.set("onMinimize",function(){ alert('chat maximized'); });

    onWebChat.set("onTrigger",function(){ alert('trigger activated'); });

    onWebChat.set("onStartWriting",function(){ alert('visitor is writing'); });

    onWebChat.set("onWebChatLoaded",function(){ alert('chat widget is loaded'); });


We use cookies to ensure that we give you the best experience on our website.