Skip to content

Widget reference

<script src="https://developers.airfone.app/widget.js" data-key="af_pk_..." async></script>

The widget renders in a shadow root, so your page’s CSS does not affect it. It is about 8 KB gzipped. Voice code loads only when a visitor presses Talk.

AttributeValuesDefault
data-keyYour publishable key. Required.
data-languageen or neThe language set in the console
data-positionright or leftright
data-colorAny CSS color, for the launcher and your visitor’s messagesTheme color from the console
data-opentrue opens the panel on loadfalse
data-launcherfalse hides the bubble, so you can open it from your own buttontrue
data-apiAPI base, only for testing against another environmenthttps://app.airfone.app

You can also set window.AirFoneSettings = { key, language, api } before the script loads.

After the script loads, window.AirFone has:

MethodWhat it does
AirFone.open()Opens the panel and starts a session.
AirFone.close()Closes the panel.
AirFone.toggle()Opens or closes.
AirFone.identify({ userId, userHash, name, phone, email })Links the visitor to a known customer.
AirFone.setLanguage('ne')Switches the widget’s language.
AirFone.on(event, handler)Listens for an event. Returns a function that stops listening.
AirFone.shutdown()Removes the widget and closes its connections.

Before the script loads, push calls onto an array and they run in order once it does:

<script>
window.AirFone = window.AirFone || [];
window.AirFone.push(['setLanguage', ['ne']]);
</script>

Without verification, anyone could claim to be any customer. So identify needs userHash: the hex HMAC-SHA256 of userId, keyed with your identity secret from the console. Compute it on your server, never in the browser.

import hmac, hashlib
user_hash = hmac.new(IDENTITY_SECRET.encode(), user_id.encode(), hashlib.sha256).hexdigest()
$userHash = hash_hmac('sha256', $userId, getenv('AIRFONE_IDENTITY_SECRET'));

A visitor with a valid hash is linked to the AirFone contact with that external id, and the assistant can use their records. A wrong hash is ignored: the visitor continues as a guest.

Set color, position, corner radius, greeting and the assistant’s display name in the console under Widget. The preview there uses the real widget. data-color and data-position on the tag win over the console. The panel follows the visitor’s light or dark setting.

EventDetail
readyThe widget is on the page.
open, closeThe panel opened or closed.
messageA message was sent or received. Detail is the message.
call.start, call.endA voice call started or ended.
errorSomething failed. Detail is the error.
AirFone.on('call.end', () => analytics.track('assistant_call'));

The panel is a labelled dialog. Focus moves into it when it opens, Tab stays inside it, and Escape closes it and returns focus. New messages are announced to screen readers. On screens narrower than 520 px the panel fills the screen.

To build your own interface, use @airfone/web, the client the widget is built on.

import { AirFoneClient } from '@airfone/web';
const client = new AirFoneClient({ publishableKey: 'af_pk_...' });
client.on((event) => console.log(event));
await client.send('Do you have rooms on Friday?');
const call = await client.startVoice((state) => console.log(state));