Referencia API

Referencia completa del API del widget y los endpoints REST.

API del widget

El objeto global window.LaunchChatWidget proporciona métodos para controlar el widget de forma programática.

Métodos

open()

Abre la ventana de chat de forma programática.

window.LaunchChatWidget.open();
close()

Cierra la ventana de chat.

window.LaunchChatWidget.close();
destroy()

Elimina el widget de la página por completo. Útil para SPAs al navegar fuera de la página.

window.LaunchChatWidget.destroy();
init(config)

Reinicializa el widget con una nueva configuración.

window.LaunchChatWidget.init({ widgetId: "new-widget-id" });
on(event, callback)

Suscribirse a eventos del widget.

window.LaunchChatWidget.on('message', (data) => {
  console.log('New message:', data);
});
off(event, callback)

Cancelar suscripción a eventos del widget.

Eventos

EventoPayloadDescripción
open{ timestamp }Ventana de chat abierta
close{ timestamp }Ventana de chat cerrada
message{ content, role }Mensaje enviado o recibido
escalate{ email, message }El usuario solicitó soporte humano
feedback{ messageId, type }El usuario dio feedback (positivo/negativo)

API REST

POST /api/widget/chat

Envía un mensaje y recibe una respuesta generada por IA.

Cuerpo de la solicitud

{
  "widgetId": "string",      // Required
  "message": "string",       // Required - user's question
  "conversationId": "string", // Optional - for context
  "visitorId": "string",     // Optional - track visitor
  "pageUrl": "string"        // Optional - current page
}

Respuesta

{
  "conversationId": "uuid",
  "messageId": "uuid",
  "answer": "string",
  "citations": [
    {
      "pageId": "string",
      "pageTitle": "string",
      "pageUrl": "string",
      "excerpt": "string"
    }
  ],
  "relatedArticles": [...],
  "confidenceScore": 0.85,
  "wasRefused": false
}

Códigos de error

CódigoEstadoDescripción
400Solicitud incorrectaFaltan campos obligatorios
403ProhibidoDominio no incluido en la lista permitida
404No encontradoWidget no encontrado o inactivo
429Límite de solicitudesCuota de mensajes excedida
500Error del servidorError interno de procesamiento

Tipos TypeScript

Copia estos tipos en tu proyecto para tener seguridad de tipos completa:

interface LaunchChatConfig {
  widgetId: string;
  primaryColor?: string;
  greeting?: string;
  placeholder?: string;
  position?: 'bottom-right' | 'bottom-left';
  theme?: 'light' | 'dark' | 'auto';
}

interface WidgetAPI {
  open(): void;
  close(): void;
  destroy(): void;
  init(config: { widgetId: string }): Promise<void>;
  on<E extends keyof WidgetEvents>(
    event: E,
    callback: (data: WidgetEvents[E]) => void
  ): void;
  off<E extends keyof WidgetEvents>(
    event: E,
    callback: (data: WidgetEvents[E]) => void
  ): void;
}

interface WidgetEvents {
  open: { timestamp: number };
  close: { timestamp: number };
  message: { content: string; role: 'user' | 'assistant' };
  escalate: { email: string; message: string };
  feedback: { messageId: string; type: 'positive' | 'negative' };
}

declare global {
  interface Window {
    LaunchChatConfig?: LaunchChatConfig;
    LaunchChatWidget?: WidgetAPI;
  }
}