Dokumentacja API
Kompletna dokumentacja Widget API i endpointów REST.
Widget API
Globalny obiekt window.LaunchChatWidget udostępnia metody do programowego sterowania widgetem.
Metody
open()Otwiera okno czatu programowo.
window.LaunchChatWidget.open();close()Zamyka okno czatu.
window.LaunchChatWidget.close();destroy()Całkowicie usuwa widget ze strony. Przydatne w SPA przy nawigacji.
window.LaunchChatWidget.destroy();init(config)Ponownie inicjalizuje widget z nową konfiguracją.
window.LaunchChatWidget.init({ widgetId: "new-widget-id" });on(event, callback)Subskrybuj zdarzenia widgetu.
window.LaunchChatWidget.on('message', (data) => {
console.log('New message:', data);
});off(event, callback)Anuluj subskrypcję zdarzeń widgetu.
Zdarzenia
| Zdarzenie | Payload | Opis |
|---|---|---|
open | { timestamp } | Okno czatu otwarte |
close | { timestamp } | Okno czatu zamknięte |
message | { content, role } | Wiadomość wysłana lub odebrana |
escalate | { email, message } | Użytkownik poprosił o ludzkie wsparcie |
feedback | { messageId, type } | Użytkownik przekazał opinię (pozytywną/negatywną) |
REST API
POST /api/widget/chat
Wyślij wiadomość i otrzymaj odpowiedź wygenerowaną przez AI.
Treść żądania
{
"widgetId": "string", // Required
"message": "string", // Required - user's question
"conversationId": "string", // Optional - for context
"visitorId": "string", // Optional - track visitor
"pageUrl": "string" // Optional - current page
}Odpowiedź
{
"conversationId": "uuid",
"messageId": "uuid",
"answer": "string",
"citations": [
{
"pageId": "string",
"pageTitle": "string",
"pageUrl": "string",
"excerpt": "string"
}
],
"relatedArticles": [...],
"confidenceScore": 0.85,
"wasRefused": false
}Kody błędów
| Kod | Status | Opis |
|---|---|---|
400 | Nieprawidłowe żądanie | Brak wymaganych pól |
403 | Zabronione | Domena nie znajduje się na liście dozwolonych |
404 | Nie znaleziono | Widget nie znaleziony lub nieaktywny |
429 | Limit przekroczony | Przekroczono limit wiadomości |
500 | Błąd serwera | Wewnętrzny błąd przetwarzania |
Typy TypeScript
Skopiuj te typy do swojego projektu dla pełnego bezpieczeństwa typów:
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;
}
}