Skip to main content

Overview

The TalkifAI Widget is a production-ready, dual-mode chatbot widget that provides:
  • Text Chat: Real-time SSE-streamed text conversations with AI agents
  • Voice Chat: LiveKit WebRTC-based voice conversations
  • Shadow DOM Isolation: No CSS bleeding from host page
  • Responsive Design: Mobile-first (300px → 800px width)
  • Voice Input/Output: Web Speech API for STT/TTS in text mode
  • Audio Visualization: Real-time frequency analysis for voice mode
Best for:
  • Customer support chatbots
  • Voice AI demos
  • Internal agent testing
  • Website live chat replacement

Quick Start

Step 1: Get Your Credentials

  1. Go to TalkifAI Studio → Organization → API Keys
  2. In the “Generate New Key” section, configure your widget key: a. Enter Key Name
    • Example: “Website Chat”, “Production Widget”, “Support Widget”
    • Max 50 characters
    b. Select Key Type
    • Click “Widget Key” (not “Secret Key”)
    • Widget Keys are browser-safe and domain-locked
    c. Add Allowed Domains (required for Widget Keys)
    • Enter comma-separated domains: mysite.com, staging.mysite.com
    • localhost is automatically allowed for development
    • Subdomains are automatically covered
    d. Click “Generate API Key”
  3. Copy the widget key immediately — it will only be shown once
    • Starts with tk_widget_ prefix
    • Store in password manager or environment variables
  4. Go to Agents and copy your Agent ID
    • Navigate to the agent you want to embed
    • Copy the Agent ID (UUID format)
Widget Key Features:
  • ✅ Safe to expose in browser/frontend code
  • ✅ Domain-locked (CORS restricted to your allowed domains)
  • localhost always allowed for development
  • ✅ Can be revoked without affecting other keys

Step 2: Add to Your Website

Add this code before </body>:
That’s it! The widget will appear as a floating button at bottom-right.

Configuration Options

Required Parameters

Optional Theme Parameters

Example Configuration


Alternative Embed Method: Data Attributes

For plain HTML sites, you can use data-* attributes instead of window.__TALKIFAI__:
Data Attributes:
  • data-api-url — API endpoint (required)
  • data-widget-key — Widget API key (required)
  • data-agent-id — Agent ID (required)
  • data-primary-color — Primary color (optional, default: #544AA8)
  • data-primary-dark — Dark primary color (optional, default: #312D69)
Note: If both window.__TALKIFAI__ and data-* attributes are provided, data-* attributes take precedence.

Framework Integrations

Next.js (App Router)

React (Vite, CRA)

Plain HTML

WordPress

Add to Appearance → Theme Editor → footer.php (before </body>):

Shopify

Add to Online Store → Themes → Edit code → layout/theme.liquid (before </body>):

Widget Features

1. Dual-Mode Interface

Users can switch between:
  • Text Mode: Traditional chat interface with typing
  • Voice Mode: Real-time voice conversation (like a phone call)
Mode Switcher: Located in header — tap “Text” or “Voice” to switch.

2. Text Mode Features

3. Voice Mode Features

4. Responsive Design

Expand Button: Tap the expand icon in header to zoom on desktop.

Customization

Changing Colors

The widget uses a purple gradient theme by default (#544AA8#312D69). To customize: Method 1: Via window.__TALKIFAI__
Method 2: Via data-* attributes

Environment Variables

Frontend (Next.js)

Backend (API Proxy Routes)

Important:
  • NEXT_PUBLIC_* variables are exposed to browser (safe for widget key)
  • TALKIFAI_API_KEY is server-side only (never expose tk_live_* keys)

Security Considerations

Widget Key vs Secret Key

Best Practice: Always use a Widget Key for frontend integrations. Widget Key Features:
  • ✅ Domain-locked (only work on your allowed domains)
  • ✅ Rate-limited for security
  • ✅ Cannot create other API keys
  • ✅ Can be revoked without affecting other keys
  • localhost always allowed for development

Creating a Widget Key in TalkifAI Studio

Steps:
  1. Go to Organization → API Keys
  2. In “Generate New Key” section:
    • Enter Key Name (e.g., “Website Widget”)
    • Select “Widget Key” (not “Secret Key”)
    • Add Allowed Domains (comma-separated)
  3. Click “Generate API Key”
  4. Copy immediately — shown only once
Allowed Domains Format:
Domain Rules:
  • Subdomains automatically covered (mysite.com includes *.mysite.com)
  • localhost always allowed for development
  • Multiple domains: separate with commas
  • Protocol (https://) optional

CORS Configuration

Widget keys are automatically configured with CORS restrictions based on your allowed domains: Example Configuration:
To Update Domains:
  1. Go to Organization → API Keys
  2. Find your widget key in “Your API Keys” section
  3. Click to edit (or revoke and create new)
  4. Update allowed domains list
Multiple domains: Add each domain separately during creation.

Session Token Security

Session tokens (session_token) returned from /sessions:
  • Valid for 24 hours
  • Tied to specific conversation_id
  • Cannot be used to create new sessions
  • Automatically invalidated when session ends
Safe to store in:
  • sessionStorage (cleared on tab close)
  • Memory (React state/ref)
Do NOT store in:
  • localStorage (persists indefinitely)
  • Cookies without HttpOnly flag

Troubleshooting

Widget Not Appearing

Check:
  1. Config script loaded before widget.js
  2. window.__TALKIFAI__ is defined (check browser console)
  3. No JavaScript errors in console
  4. Widget script URL is correct
Debug:

“Failed to Create Session”

Causes:
  • Invalid widget key
  • Agent ID not found
  • CORS origin not configured
  • Insufficient credits
Check:

Voice Mode Not Working

Check:
  1. Microphone permissions granted
  2. LiveKit URL is reachable
  3. Agent supports voice (Pipeline or Realtime architecture)
  4. Browser supports WebRTC (Chrome, Firefox, Safari, Edge)
Debug:

Text Streaming Not Working

Check:
  1. API returns Content-Type: text/event-stream
  2. SSE parser handles all event types
  3. Network tab shows streaming response
Debug:

Customization

Changing Colors

The widget uses a purple gradient theme by default (#544AA8#312D69). To customize: Method 1: Via window.__TALKIFAI__
Method 2: Via data-* attributes

Performance Optimization

Lazy Loading

Load widget only when user interacts:

Preloading

Preload widget script for faster load:

Browser Support

Voice Mode Requirements:
  • WebRTC support
  • Microphone permissions
  • Audio playback
Text Mode Requirements:
  • Fetch API
  • SSE (Server-Sent Events)
  • Modern JavaScript (ES6+)

Migration Guide

From Chat API to Widget

If you already have a custom chat integration: Before (Custom Implementation):
After (Widget):
Benefits:
  • ✅ No custom UI code needed
  • ✅ Built-in voice mode
  • ✅ Automatic session management
  • ✅ Responsive design included
  • ✅ Shadow DOM isolation

From Other Chat Widgets

From Intercom/Drift/Zendesk:
  1. Remove old widget script
  2. Add TalkifAI config + widget
  3. Update agent in TalkifAI Studio
  4. Test on staging first
Migration Checklist:
  • Remove old widget code
  • Create TalkifAI agent (import conversation history if needed)
  • Configure widget key with CORS
  • Test text mode
  • Test voice mode
  • Verify analytics tracking
  • Deploy to production

Best Practices

1. Always Use Widget Keys

Wrong:
Correct:

2. Configure CORS Properly

Add only trusted domains to widget key CORS allowlist:
  • Production domain
  • Staging domain (for testing)
  • Localhost (for development)

3. Handle Session Cleanup

Widget automatically ends sessions on close, but you can explicitly end:

4. Test on Multiple Devices

Test widget on:
  • Desktop (Chrome, Firefox, Safari, Edge)
  • Mobile (iOS Safari, Chrome Android)
  • Tablets (iPad, Android tablets)

5. Monitor Widget Performance

Track metrics:
  • Widget load time
  • Session creation time
  • Message latency
  • Voice connection success rate


Next Steps

Create Your Agent

Set up the agent you want to embed in the widget.

Get Widget Key

Generate a domain-locked widget key in Studio.

Customize Widget

Learn how to customize colors and positioning.

View Analytics

Track widget usage and performance.

Support

Issues? Contact TalkifAI support: