Skip to main content

Widgets API

Nuclia's widgets are custom elements, and they offers an API that can be interacted with from the outside. It allows you to achieve a better integration between the widget and your own application.

Search widget API

Methods

This triggers a search.

Example:

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.search('What is the answer to the Ultimate Question of Life, the Universe, and Everything?');

reloadSearch

This triggers a reloading of the search results.

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.reloadSearch();

onInit

Return a promise that resolves when the widget is initialized.

const widget = document.getElementsByTagName('nuclia-search-bar')[0];
widget.onInit().then(() => {
console.log('Widget is initialized');
});

initHook and modifyHeaders

initHook is a hook that will be called when the widget is initialized before any call to the API is made.

It is useful to modify the Nuclia options, typically setting extra HTTP headers using modifyHeaders:

widget.initHook = (nuclia) => {
nuclia.options.modifyHeaders = (headers) => {
headers['x-whatever'] = 'some-value';
return headers;
};
};

A typical use case is to set a CSRF token in the headers.

onError

This is a hook that will be called when a network error occurs.

widget.onError.subscribe((error) => {
console.error('An error occurred', error.status, error.detail);
};

Chat widget API

(Note: the search widget API is also available on the chat widget, except for search and reloadSearch methods.)

Methods

ask

This triggers a question answering.

Parameters:

  • question (string): The question to ask.
  • reset (boolean): If true, the current chat will be reset before asking the question.

Example:

const widget = document.getElementsByTagName('nuclia-chat')[0];
widget.ask('What is the answer to the Ultimate Question of Life, the Universe, and Everything?');

openChat and closeChat

These methods open and close the chat, respectively. They do not apply when using the inline mode.

Example:

const widget = document.getElementsByTagName('nuclia-chat')[0];
widget.openChat();