Referência da API
Referência completa da API do Widget e endpoints REST.
API do Widget
O objeto global window.LaunchChatWidget fornece métodos para controlar o widget programaticamente.
Métodos
open()Abre a janela de chat programaticamente.
window.LaunchChatWidget.open();close()Fecha a janela de chat.
window.LaunchChatWidget.close();destroy()Remove o widget da página completamente. Útil para SPAs ao navegar para outra página.
window.LaunchChatWidget.destroy();init(config)Reinicializa o widget com uma nova configuração.
window.LaunchChatWidget.init({ widgetId: "new-widget-id" });on(event, callback)Inscreve-se em eventos do widget.
window.LaunchChatWidget.on('message', (data) => {
console.log('New message:', data);
});off(event, callback)Cancela a inscrição em eventos do widget.
Eventos
| Evento | Payload | Descrição |
|---|---|---|
open | { timestamp } | Janela de chat aberta |
close | { timestamp } | Janela de chat fechada |
message | { content, role } | Mensagem enviada ou recebida |
escalate | { email, message } | Usuário solicitou suporte humano |
feedback | { messageId, type } | Usuário deu feedback (positivo/negativo) |
API REST
POST /api/widget/chat
Envia uma mensagem e recebe uma resposta gerada por IA.
Corpo da requisição
{
"widgetId": "string", // Required
"message": "string", // Required - user's question
"conversationId": "string", // Optional - for context
"visitorId": "string", // Optional - track visitor
"pageUrl": "string" // Optional - current page
}Resposta
{
"conversationId": "uuid",
"messageId": "uuid",
"answer": "string",
"citations": [
{
"pageId": "string",
"pageTitle": "string",
"pageUrl": "string",
"excerpt": "string"
}
],
"relatedArticles": [...],
"confidenceScore": 0.85,
"wasRefused": false
}Códigos de erro
| Código | Status | Descrição |
|---|---|---|
400 | Requisição inválida | Campos obrigatórios ausentes |
403 | Proibido | Domínio não está na lista de permitidos |
404 | Não encontrado | Widget não encontrado ou inativo |
429 | Limite excedido | Cota de mensagens excedida |
500 | Erro do servidor | Erro interno de processamento |
Tipos TypeScript
Copie estes tipos para seu projeto para total segurança de tipos:
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;
}
}