Skip to content
Widget SDK

JavaScript API

Complete reference for Gydr.chatbox(), Gydr.bubble(), Gydr.identify(), Gydr.setIdentityTokenProvider(), and Gydr.reset().

On this page

API Reference#

The SDK exposes five methods on the global window.Gydr object. Only one widget instance can be active at a time — calling chatbox() or bubble() when a widget is already active returns the existing instance.

Gydr.bubble(config)#

Creates a floating action button with a toggleable chat panel. Returns a BubbleInstance.

PropertyTypeRequiredDefaultDescription
apiKeystringRequired—Your public API key (pk_live_...).
position"bottom-right" | "bottom-left"Optional"bottom-right"Screen corner for the bubble button and chat panel.
bubbleColorstringOptional"#6366f1"Background color of the floating button (6-digit hex color, e.g. #6366f1).
bubbleSizenumberOptional56Diameter of the floating button in pixels.
bubbleIconstringOptional—Custom icon for the bubble button. Pass an inline SVG string (e.g., "<svg>...</svg>") or an image URL. Defaults to a chat icon.
bubbleIconSizenumberOptional—Override the icon size in pixels. Defaults to ~43% of the bubble diameter (24px for a 56px bubble).
offsetobjectOptional—Fine-tune button position: { bottom?, right?, left? } in pixels.

Returns: BubbleInstance

MemberDescription
open()Opens the chat panel.
close()Closes the chat panel.
toggle()Toggles the chat panel open/closed.
destroy()Removes the widget from the DOM and resets the singleton.
isOpenRead-only boolean indicating the current panel state.

Gydr.chatbox(config)#

Creates a full-size chat panel for desktop embedding. Returns a ChatboxInstance.

PropertyTypeRequiredDefaultDescription
apiKeystringRequired—Your public API key (pk_live_...).
containerHTMLElementOptional—DOM element to embed the chatbox in. If omitted, the chatbox fills the full viewport.

Returns: ChatboxInstance

MethodDescription
destroy()Removes the widget from the DOM and resets the singleton.

Gydr.identify(payload)#

Passes details about the visitor. Two different things go through it: a signed identity token that proves who a signed-in visitor is, and ordinary details (name, email, phone, company, location) that personalize greetings and help your team on hand-off but prove nothing. Returns void.

For signed-in visitors, prefer data-identity-endpoint or Gydr.setIdentityTokenProvider() — they refresh the token before it expires. identify({ identityToken }) is for sites with no refresh path. See Identify signed-in visitors.

Important: call identify() before chatbox() or bubble(). To change the visitor after the widget exists, call reset() first.
Property
identityToken
Type
string | null
Required
Optional
Description
A token signed on your server for the signed-in user. Held in memory only, sent with each message, never put in a URL or browser storage. null signs the visitor out. See Identify signed-in visitors.
Property
visitorId
Type
string
Required
Optional
Description
Leave unset — the widget generates a random id. If you set one, it must be random and unguessable (1–128 characters, letters, digits and _-.:@): never a user id or email. It is a label, not proof of identity.
Property
name
Type
string
Required
Optional
Description
Display name, used to personalize greetings (e.g. “Hi Sarah!”). Max 100 characters. Unverified.
Property
email
Type
string
Required
Optional
Description
Email address, stored as visitor context for greetings and hand-off. Unverified — never used to look up personal data.
Property
phone
Type
string
Required
Optional
Description
Phone number, stored as visitor context. Max 30 characters. Unverified.
Property
company
Type
string
Required
Optional
Description
Company or organization. Max 100 characters.
Property
latitude
Type
number
Required
Optional
Description
Latitude in decimal degrees (-90 to 90). With longitude, pre-fills the visitor’s location so “nearest store” questions skip the in-chat location prompt.
Property
longitude
Type
number
Required
Optional
Description
Longitude in decimal degrees (-180 to 180). Must be provided together with latitude.
<!-- Recommended: the widget fetches the token itself -->
<script
  src="https://cdn.gydr.ai/widget.js"
  data-api-key="pk_live_YOUR_KEY"
  data-identity-endpoint="/gydr/identity-token"
  async
></script>

Gydr.setIdentityTokenProvider(fn)#

Registers a function the widget calls for the current visitor’s identity token — before the first message and whenever the token is under 60 seconds from expiry. Return the token string, or null when nobody is signed in. Throw (or reject) when the token can’t be fetched: that changes nothing and the widget tries again, whereas null signs the visitor out. Pass null instead of a function to unregister. Register it from the script tag’s load event. Returns void.

Sign-out is automatic: null, no token after one was active, or a token for a different sub resets the widget to a new visitor and a new conversation.

script.addEventListener('load', () => {
  window.Gydr.setIdentityTokenProvider(async () => {
    const res = await fetch('/api/gydr-token', { cache: 'no-store' });
    if (!res.ok) throw new Error('token unavailable');
    const { token } = await res.json();
    return token; // string, or null for a guest
  });
});

Gydr.reset()#

Clears all visitor data from browser storage, destroys the active widget instance, and resets the SDK to a clean state. Call this when a user logs out to prevent the next user from inheriting the previous user’s chat history. Returns void.

With data-identity-endpoint or a token provider you rarely need this — sign-out is automatic. After calling reset(), you can call identify() with a new user and then chatbox() or bubble() to start a fresh session for the new user.
// On user logout
function handleLogout() {
  Gydr.reset();

  // Optionally start a fresh guest widget
  Gydr.bubble({ apiKey: 'pk_live_YOUR_KEY' });
}