Guide Website widget

Website widget

On this page

A chat box for your own website. Your visitors chat with the assistant in a box at the bottom of the page; the conversation runs through Determesh like any other channel, with the same access rules, resources and checks.

How it works

  1. You add a widget on the panel's Channels page: the sites it may run on, its look, and an optional message limit. You get its signing secret once: keep it on your server.
  2. Your page loads the widget with a short snippet (shown in the panel).
  3. For each visitor, your server makes a small token saying who they are and signs it with the secret. The widget sends it with every call. The visitor's browser carries the token but cannot change it: any change breaks the signature.
  4. Determesh checks the signature, the expiry and the site, then treats the visitor as that user.

The secret never goes to the browser. Inside, a widget is an API key limited to chat: it counts as one channel like an API key, its conversations are listed under Threads, and its summary and restriction work as for any channel.

The snippet

<script>
  window.determeshWidget = {
    widgetId: "your widget's id",
    api: "https://api.eu.determesh.com",
    // Returns a token for this visitor, made and signed by your server.
    token: () => fetch("/determesh-widget-token").then((r) => r.text()),
  };
</script>
<script src="https://determesh.com/widget/v1.js" async></script>
  • token is a function returning the token (or a promise of it), or the token itself. The widget asks again when a token expires.
  • Open or close it from your own button with Determesh.open() and Determesh.close().
  • The widget is drawn in its own isolated layer, so your page's styles do not change it and its styles do not change your page.

The token

A JWT signed with HS256 using the widget's signing secret. Any JWT library makes one. Fields:

Field Required What it is
exp Yes When it expires, in Unix seconds. Keep it short, such as 1 hour; at most 24 hours ahead. Make a fresh one on each page load.
userId No Your own ID for the user, up to 200 characters. The same userId is the same user every time, on every device, and is the same user as the API's userId. Left out, the visitor is anonymous: no attributes, and no history after the token expires.
name No How to address them. Shown in the panel; access rules never check it.
attributes No The user's attributes, such as {"role": "staff", "department": ["hr"]}. Trusted, because your server signed them: each message writes them onto the user, replacing the same attributes and leaving others alone. Access rules check them. Ignored when there is no userId.
context No Anything that helps the assistant understand the request, such as the page, the plan or the cart: {"page": "/billing"}. Up to 4,000 characters. The assistant reads it as background; access rules never use it and it is not stored as attributes.
threadId No Your own key for one conversation, such as an order number. The widget then shows only that conversation, made the first time it is used. Left out, the visitor sees all their conversations on this widget and can start new ones.

Python

import time, jwt  # pip install PyJWT

def widget_token(user):
    return jwt.encode({
        "userId": str(user.id),
        "name": user.first_name,
        "attributes": {"role": user.role, "employee_id": user.employee_id},
        "context": {"page": "/account"},
        "exp": int(time.time()) + 3600,
    }, WIDGET_SECRET, algorithm="HS256")

Node.js

import jwt from "jsonwebtoken"; // npm install jsonwebtoken

function widgetToken(user) {
  return jwt.sign(
    { userId: String(user.id), name: user.firstName, attributes: { role: user.role }, context: { page: "/account" } },
    process.env.WIDGET_SECRET,
    { algorithm: "HS256", expiresIn: "1h" },
  );
}

Serve it from a route on your site that only your signed-in pages call, such as GET /determesh-widget-token, answering the token as plain text.

Safety

  • The sites list. The widget answers only on the sites you list, such as https://shop.example.com, or https://*.example.com for every subdomain (not example.com itself). Add http://localhost:3000 while you build. This stops another website from using your widget. A program outside a browser can pretend to be your site, so the token's short life is the main limit.
  • A leaked token lets someone act as that one user until it expires, and nothing more: never other users, never your settings.
  • A leaked secret lets someone sign tokens for any user. Add a new widget, move your pages to it and revoke the old one.
  • Message limit. Optionally, the most messages a minute each user may send (0 for no limit). Your plan's spending cap and balance floor still apply. Determesh adds no captcha; ask one on your site before making the token if you want it.

What the visitor sees

  • Their latest conversation opens with its history, and a list of earlier ones, each named by the date and time it started. New conversation starts another. History lives on Determesh's servers, so it follows the user to any device; the browser keeps only whether the box was open.
  • Replies appear as they come, with a typing indicator while the assistant works.
  • Files the assistant hands over appear as buttons to download, and images show in the chat. Every download is checked again. Report files can be downloaded for the company's download-link hours, as on the API.
  • Users cannot send files yet.
  • On a phone the chat fills the screen.
  • A small Powered by Determesh link is always shown.

The look

Set on the Channels page: title, logo (an https address), main colour, greeting, bottom right or left, and light, dark or following the visitor's setting. The panel shows a preview.

What a widget can call

Only these, each with the visitor's token in the X-Determesh-Widget-Token header (the widget does this for you):

Call What it does
GET /v1/widget/{widgetId}/config The widget's look. No token needed.
POST /v1/widget/{widgetId}/messages Sends {"text": "...", "threadId": "optional"}; answers 202 with messageId and threadId.
GET /v1/widget/{widgetId}/threads The user's conversations on this widget, newest first.
GET /v1/widget/{widgetId}/threads/{threadId}/messages?afterSeq=0 Messages and replies, with status (idle, working, failed).
GET /v1/widget/{widgetId}/threads/{threadId}/files/{fileId} A file handed over in that conversation.

Errors are JSON with a code: token_missing, token_expired, token_invalid (401), origin_not_allowed (403), widget_not_found (404), too_many_messages or too_many_requests (429), and the billing codes of the API (503).

Managing widgets by API

With a key holding the API keys section: GET and POST /v1/companies/{companyId}/widgets, PATCH .../widgets/{widgetId}, POST .../widgets/{widgetId}/revoke. A new widget's response carries its signing secret, once. See the API reference.

This page is made from /docs/v1/widget.md, the Markdown that AI assistants read. Questions: hi@determesh.com.