Back to Dashboard

Documentation

Learn how to use Asyntai

hapi Plugin

Add the Asyntai AI chat assistant to every HTML page of your hapi server

Create Account

Overview

hapi is a web framework for Node.js. The Asyntai plugin is an npm package. Register it on your server, and the chat bubble is on every HTML page your server sends: pages from route handlers, from templates, and static files.

The assistant answers your visitors from your own content, in more than 80 languages, day and night.

Requirements

  • hapi 20 or 21
  • Node 18 or later
  • An Asyntai account. The Free plan is enough to try the widget

Installation

Follow these steps to add Asyntai to your hapi server:

1

Get Your Widget ID

Sign in to Asyntai and open the Install page. The widget ID is the value of data-asyntai-id in the snippet shown there. It starts with asyntai_.

2

Add the Package

Run this command in your project folder:

npm install hapi-ai-chatbot
3

Register the Plugin

Register it with your widget ID:

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = Hapi.server({ port: 3000 });

  await server.register({
    plugin: require('hapi-ai-chatbot'),
    options: { widgetId: 'asyntai_1a2b3c4d5e6f' },
  });

  server.route({
    method: 'GET',
    path: '/',
    handler: () => '<html><body><h1>Hello</h1></body></html>',
  });

  await server.start();
};

init();

With ES modules or TypeScript:

import Hapi from '@hapi/hapi';
import aiChatbot from 'hapi-ai-chatbot';

const server = Hapi.server({ port: 3000 });
await server.register({ plugin: aiChatbot, options: { widgetId: 'asyntai_1a2b3c4d5e6f' } });
4

Start the Server

Start your server and open any page in the browser. The chat bubble is in the corner.

Options

OptionTypeDefaultWhat it does
widgetIdstringASYNTAI_WIDGET_ID environment variableYour Asyntai widget ID
shouldInjectfunctionevery page(request) => boolean. Return false to keep the chat off a page, for example an admin area
scriptUrlstringthe Asyntai CDNAdvanced. Load the widget from another URL

A missing or malformed widget ID stops the server at startup with a message that says how to fix it.

To keep the chat off your admin pages:

await server.register({
  plugin: require('hapi-ai-chatbot'),
  options: {
    widgetId: 'asyntai_1a2b3c4d5e6f',
    shouldInject: (request) => !request.path.startsWith('/admin'),
  },
});

How It Works

The plugin adds the Asyntai loader to every HTML response, just before </body>. It works for HTML from route handlers, from @hapi/vision templates and from @hapi/inert files, in every route and every plugin of your server. JSON, files that are not HTML, and redirects are not touched.

The loader waits for the page's load event, so your own page always renders first. hapi compresses a response after this step, so gzip pages get the chat too.

Answering From Your Content

Asyntai reads your published pages. Add your site URL in the dashboard, and the assistant answers from the same pages the widget sits on. You can also add documents, and question and answer pairs.

Tip: If you have your own onPreResponse extension that makes HTML error pages, add it before this plugin. Then your error pages get the chat too.

Removing It

Delete the server.register call and restart the server. The chat bubble is gone.

Plans & Pricing

The package is free. View pricing.

Need Help?

If you have any questions or run into issues, contact us at [email protected].