Referensi API
Referensi lengkap untuk Widget API dan REST endpoints.
Widget API
Objek global window.LaunchChatWidget menyediakan metode untuk mengontrol widget secara programatis.
Metode
open()Membuka jendela chat secara programatis.
window.LaunchChatWidget.open();close()Menutup jendela chat.
window.LaunchChatWidget.close();destroy()Menghapus widget dari halaman sepenuhnya. Berguna untuk SPA saat berpindah halaman.
window.LaunchChatWidget.destroy();init(config)Menginisialisasi ulang widget dengan konfigurasi baru.
window.LaunchChatWidget.init({ widgetId: "new-widget-id" });on(event, callback)Berlangganan event widget.
window.LaunchChatWidget.on('message', (data) => {
console.log('New message:', data);
});off(event, callback)Berhenti berlangganan event widget.
Event
| Event | Payload | Deskripsi |
|---|---|---|
open | { timestamp } | Jendela chat dibuka |
close | { timestamp } | Jendela chat ditutup |
message | { content, role } | Pesan dikirim atau diterima |
escalate | { email, message } | Pengguna meminta dukungan manusia |
feedback | { messageId, type } | Pengguna memberikan umpan balik (positif/negatif) |
REST API
POST /api/widget/chat
Kirim pesan dan terima respons yang dihasilkan AI.
Request Body
{
"widgetId": "string", // Required
"message": "string", // Required - user's question
"conversationId": "string", // Optional - for context
"visitorId": "string", // Optional - track visitor
"pageUrl": "string" // Optional - current page
}Response
{
"conversationId": "uuid",
"messageId": "uuid",
"answer": "string",
"citations": [
{
"pageId": "string",
"pageTitle": "string",
"pageUrl": "string",
"excerpt": "string"
}
],
"relatedArticles": [...],
"confidenceScore": 0.85,
"wasRefused": false
}Kode Error
| Kode | Status | Deskripsi |
|---|---|---|
400 | Bad Request | Field yang diperlukan tidak ada |
403 | Forbidden | Domain tidak ada dalam daftar yang diizinkan |
404 | Not Found | Widget tidak ditemukan atau tidak aktif |
429 | Rate Limited | Kuota pesan terlampaui |
500 | Server Error | Kesalahan pemrosesan internal |
TypeScript Types
Salin type ini ke proyek Anda untuk keamanan type yang lengkap:
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;
}
}