15-day Pro trial · no credit cardStart free →

Web JS SDK

Single-page apps

Use the STINGIM widget in React, Vue and other single-page apps: install once, identify on login, logout and destroy.

The widget is designed for single-page apps: it notices URL and title changes on its own, queues calls made before it is ready, and can be torn down cleanly.

Rules of thumb

  • Insert the install code once, for example in your root layout. Route changes need no extra code.
  • After login or an account switch, call stingIm.identify(user). On logout, call stingIm.logout().
  • To remove the widget completely (for example when a component unmounts), call stingIm.destroy(). Removing the <script> tag alone does not remove the widget.
  • Inserting the script twice is harmless: the second load reuses the existing instance.

React

JSX
import { useEffect } from 'react';

const INSTALL_SRC = 'https://app.stingim.com/chat-bubble/install.js?BASE64_PARAMS';

export function ChatWidget() {
  useEffect(() => {
    const s = document.createElement('script');
    s.src = INSTALL_SRC;
    s.async = true;
    document.body.appendChild(s);
    return () => {
      window.stingIm?.destroy();
      s.remove();
    };
  }, []);
  return null;
}

Identify the user whenever your auth state changes:

JSX
useEffect(() => {
  if (!user) return;
  fetch('/api/me/stingim-identity')
    .then((r) => r.json())
    .then(({ userId, userHash, exp }) =>
      window.stingIm?.identify({ userId, userHash, exp, name: user.name, email: user.email }),
    );
}, [user]);

For TypeScript, declare the global once:

TypeScript
declare global {
  interface Window {
    stingIm?: {
      show(): void; hide(): void; open(): void; close(): void;
      identify(u: { userId: string; userHash?: string; exp?: number; name?: string; email?: string }): Promise<boolean>;
      logout(): Promise<boolean>;
      setTags(tags: string[]): void;
      setContext(ctx: { product?: string; orderNo?: string }): void;
      setStyle(css: Record<string, string>): void;
      getStatus(): { ready: boolean; visible: boolean; open: boolean; hidden: boolean;
        identityStatus: 'anonymous' | 'verified' | 'unverified' | 'rejected' | null; error: string | null; destroyed: boolean };
      onReady(cb: (api: NonNullable<Window['stingIm']>) => void): () => void;
      destroy(): void;
    };
    stingImSettings?: Record<string, unknown>;
  }
}
export {};

Vue 3

JavaScript
import { onMounted, onBeforeUnmount } from 'vue';

export function useStingim(src) {
  let script;
  onMounted(() => {
    script = document.createElement('script');
    script.src = src;
    script.async = true;
    document.body.appendChild(script);
  });
  onBeforeUnmount(() => {
    window.stingIm?.destroy();
    script?.remove();
  });
}

Next.js

Load the script once in the root layout with next/script; it runs only in the browser:

JSX
import Script from 'next/script';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <Script src="https://app.stingim.com/chat-bubble/install.js?BASE64_PARAMS" strategy="afterInteractive" />
      </body>
    </html>
  );
}

Logout on shared devices

JavaScript
async function signOut() {
  await window.stingIm?.logout(); // the next person starts as a new anonymous visitor
  await api.logout();
}

Found a mistake? Email [email protected]