AI chatbot JavaScript API Reference

Use these methods and events to control the chatbot and respond to its state changes.

// Available API Methods
window.KnowledgeOwlChatbot.setAuthToken(token)     // Set authentication token
window.KnowledgeOwlChatbot.setContext(contextObj)  // Set page context (replaces existing)
window.KnowledgeOwlChatbot.updateContext(partial)  // Update page context (merges with existing)
window.KnowledgeOwlChatbot.open()                  // Open the chatbot
window.KnowledgeOwlChatbot.close()                 // Close the chatbot
window.KnowledgeOwlChatbot.isReady()               // Check if chatbot is ready
window.KnowledgeOwlChatbot.isOpen()                // Check if chatbot is open
window.KnowledgeOwlChatbot.isAuthenticated()       // Check if user was successfully authenticated via token


// Available Events
window.addEventListener('ko-chatbot-ready', (event) => {
  // Fired when chatbot is loaded and ready
  // event.detail: { hasToken: boolean }
});


window.addEventListener('ko-chatbot-opened', () => {
  // Fired when chatbot is opened by user
});


window.addEventListener('ko-chatbot-closed', () => {
  // Fired when chatbot is closed by user
});


window.addEventListener('ko-chatbot-authenticated', (event) => {
  // Fired when authentication succeeds
  // event.detail: { success: true }
});


window.addEventListener('ko-chatbot-error', (event) => {
  // Fired when an error occurs
  // event.detail: { error: string, code: string }
});


window.addEventListener("message", function(event) {
  // Optional: verify origin for security
  // if (event.origin !== "https://your-ko-domain.knowledgeowl.com") return;

  if (event.data && event.data.type === "ko-ai-chatbot:cta-click") {
    // Check for your specific event ID if you have multiple CTA buttons
    if (event.data.eventId === "your-event-id") {
      // Fired when the custom response rule CTA button with the event ID "your-event-id" is clicked
    }
  }
});