Skip to main content

Functions

The Website Widget exposes a set of functions on the global window.Kapa object, allowing you to manipulate it programmatically. The following functions are available:

To call these functions, the window.Kapa object must be initialized first. Use preinitialization to prevent errors when functions are called before the widget object has completed initialization.

open

Open the widget modal.

Optional parameters:

ParameterTypeDefaultDescription
mode'search' | 'ai''ai'The mode the widget is set to when the modal is opened.
querystring''Opens the widget with this query prefilled.
submitbooleanfalseAutomatically submit the query when the model is opened.

Example usage:

window.Kapa.open({
mode: "ai",
query: "How to get started?",
submit: true,
});

close

Close the widget modal.

Example usage:

window.Kapa.close();

render

Mounts the global Kapa object. This operation is asynchronous, so any subsequent function calls that depend on the widget being fully initialized should be executed only after render() has completed, typically by using the provided onRender callback.

Optional parameters:

ParameterTypeDefaultDescription
onRenderfunctionCallback function to invoke when the widget has rendered.

Example usage:

window.Kapa.render({
onRender: () => window.Kapa.open(),
});

unmount

Unmounts the global Kapa object, removing its rendered DOM elements and clearing all references to it.

window.Kapa.unmount()

setSourceGroupIDs

Sets the source group IDs to include in subsequent search and AI queries. This allows dynamically changing the filtering at runtime without reinitializing the widget.

Parameters:

ParameterTypeDefaultDescription
idsstring[]Array of source group IDs to include. Pass an empty array [] to clear and remove filtering.

The value is initialized from the data-source-group-ids-include script attribute on widget load. Calling setSourceGroupIDs() updates the value for both search mode and AI chat mode, and changes take effect immediately for subsequent queries.

Example usage:

// Filter to specific source groups
window.Kapa.setSourceGroupIDs(["group-id-1", "group-id-2"]);

// Clear filtering (equivalent to no data-source-group-ids-include attribute)
window.Kapa.setSourceGroupIDs([]);

getSourceGroupIDs

Returns the current source group IDs being used for filtering.

Returns: string[] | undefined - Array of source group IDs, or undefined if no filtering is active.

Example usage:

const currentGroups = window.Kapa.getSourceGroupIDs();
console.log(currentGroups); // ["group-id-1", "group-id-2"] or undefined