Chatbot Widget SDK
Control the fonio Chatbot Widget, listen for events, and pass website or private integration context.
Once the widget script has loaded, you can control the AI Chatbot via the following object:
window.fonio.webchat
Available methods
| Method | Description |
|---|---|
open() | Shows the widget and opens the chat window |
close() | Closes the chat window, the launcher stays visible |
toggle() | Opens or closes the chat window |
show() | Shows the widget without opening the chat window |
hide() | Hides the entire widget without ending the conversation |
startNewConversation() | Opens the widget and starts a new conversation |
setContext(context) | Passes additional website context |
setIntegrationContext(context) | Saves private values for configured integrations and returns a promise |
on(eventName, callback) | Subscribes to a lifecycle event and returns a function to unsubscribe |
Wait until the SDK has loaded
If your code might run before the widget script, wait for the load event:
const webchatScript = document.querySelector(
'[data-fonio-webchat-widget-id]'
);
function configureWebchat() {
window.fonio.webchat.setContext({
user: {
id: 'user-123',
name: 'Philipp',
companyName: 'fonio'
},
plan: 'premium'
});
}
if (window.fonio?.webchat) {
configureWebchat();
} else {
webchatScript?.addEventListener('load', configureWebchat, {
once: true
});
}
Open the widget with your own button
You can connect the widget to your own button on your website:
document
.querySelector('#contact-support')
.addEventListener('click', () => {
window.fonio.webchat.open();
});
Pass website context with setContext()
With setContext() you can pass information your website already knows.
This can be, for example:
- Customer ID
- Name of the logged-in user
- Company
- Plan or product
- Currently opened page
Example:
window.fonio.webchat.setContext({
user: {
id: 'user-123',
name: 'Philipp',
companyName: 'fonio'
},
plan: 'premium'
});
Ideally, set the context before you open the chatbot. Then it is already available with the first message.
You can also update the context during an ongoing conversation. The new data applies from the visitor's next message.
Context is replaced completely
Every call to setContext() replaces the previous context. The data is not merged automatically.
To clear the context, pass an empty object:
window.fonio.webchat.setContext({});
The context must be a JSON-compatible object and may be at most 8 KiB in size.
The data is treated as untrusted website context. It can help the assistant understand the visitor better, but it cannot override the assistant's rules or instructions.
Do not pass passwords, API keys or other secrets. Only send information that is really needed for the conversation.
Availability
Both context methods require website context access for the Chatbot Widget. Check whether it is enabled for your account or ask fonio support.
Pass private values with setIntegrationContext()
Use setContext() for information the assistant can use, such as the current page or product. Treat it as untrusted input. Use setIntegrationContext() for values a configured integration needs, such as a short-lived customer token.
Integration context is encrypted at rest for the Widget session. fonio keeps the values out of model input and redacts them from its logs. An integration receives a value when its configuration references that field. Treat the values as browser-supplied input and validate them on your server.
Pass a flat object whose values are strings, with a maximum serialized size of 8 KiB. Each call replaces the previous object. Pass {} to clear it. The method returns a promise that resolves when the Widget confirms the save. You can call it before the visitor starts a chat. The Widget queues the update until a session exists, so avoid making the chat wait for that promise before it can start. Handle a rejected write in your application.
Authorize requests with a short-lived token
Have your backend issue a short-lived, scoped JWT for the signed-in customer. Once the Widget SDK is available, pass the token obtained from your backend:
// customerToken comes from your authenticated backend.
window.fonio.webchat.setIntegrationContext({ customerToken }).catch(() => {
// Handle the failed write without logging the token.
});
In the fonio HTTP Request integration, reference the field in the outgoing header:
Authorization: Bearer {{webchatIntegrationContext.customerToken}}
When you refresh the token, call the method with the replacement and any other fields you want to keep. Clear the context when the customer signs out.
Your receiving middleware must authorize every request before reading or changing customer data:
- Verify the JWT signature against a trusted signing key with an allowed algorithm. Check the expected issuer and audience, require an expiration, and honor
nbfwhen it is present. Reject missing or invalid tokens before looking up data. - Derive the customer identity from a verified claim and check that the customer can access the specific record or action. If a customer ID is also supplied in the request, require it to match. A missing or empty identity must stop the request.
- Redact the authorization header from middleware, proxy and tracing logs. Keep credentials out of responses and errors, and return only the business data the customer may see.
See JWT Best Current Practices and the JWT claim definitions.
Use lifecycle events
With window.fonio.webchat.on(eventName, callback) you can react to widget events.
The callback receives the following values:
{
type,
widgetId,
sessionId
}
The sessionId is present for events that belong to a conversation.
| Event | Meaning |
|---|---|
opened | The floating chat window was opened |
closed | The floating chat window was closed |
started | The visitor's first message was accepted |
ended | The conversation was explicitly ended |
handover_requested | A handover to a human was requested |
handover_taken | A team member has taken over the chat |
The method returns a function you can use to unsubscribe from the event.
Example with Google Tag Manager
const script = document.querySelector(
'[data-fonio-webchat-widget-id]'
);
function registerEvents() {
const eventNames = [
'opened',
'closed',
'started',
'ended',
'handover_requested',
'handover_taken'
];
for (const name of eventNames) {
window.fonio.webchat.on(name, ({ type, sessionId }) => {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: `fonio_webchat_${type}`,
webchat_session_id: sessionId
});
});
}
}
if (window.fonio?.webchat) {
registerEvents();
} else {
script?.addEventListener('load', registerEvents, { once: true });
}
Tracking and conversions
You set up consent, GA4, Google Ads and conversion definitions on your website yourself. fonio does not automatically send conversions to Google.
started only means that a conversation has begun. The event does not confirm that a request or lead was left.
opened and closed are only available for the floating widget. Events that occur while the website is closed are not sent later.
