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
- Customer support chatbots
- Voice AI demos
- Internal agent testing
- Website live chat replacement
Quick Start
Step 1: Get Your Credentials
- Go to TalkifAI Studio → Organization → API Keys
-
In the “Generate New Key” section, configure your widget key:
a. Enter Key Name
- Example: “Website Chat”, “Production Widget”, “Support Widget”
- Max 50 characters
- Click “Widget Key” (not “Secret Key”)
- Widget Keys are browser-safe and domain-locked
- Enter comma-separated domains:
mysite.com, staging.mysite.com localhostis automatically allowed for development- Subdomains are automatically covered
-
Copy the widget key immediately — it will only be shown once
- Starts with
tk_widget_prefix - Store in password manager or environment variables
- Starts with
-
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)
- ✅
localhostalways allowed for development - ✅ Can be revoked without affecting other keys
Step 2: Add to Your Website
Add this code before</body>:
Configuration Options
Required Parameters
| Parameter | Type | Description | Example |
|---|---|---|---|
apiUrl | string | TalkifAI Chat API base URL | https://api.talkifai.dev/v1/chat |
widgetKey | string | Domain-locked widget API key | tk_widget_abc123... |
agentId | string | UUID of the agent to use | 5b710eca-ee67-4c3a-aeb6-8b541f451b40 |
Optional Theme Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
theme.primaryColor | string | #544AA8 | Primary brand color (purple) |
theme.primaryDark | string | #312D69 | Darker shade for gradients |
Example Configuration
Alternative Embed Method: Data Attributes
For plain HTML sites, you can usedata-* attributes instead of window.__TALKIFAI__:
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)
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)
2. Text Mode Features
| Feature | Description |
|---|---|
| SSE Streaming | Real-time text streaming as AI generates responses |
| Voice Input | Tap mic button to speak — converts to text automatically |
| Voice Output | Enable TTS to hear responses spoken aloud |
| Typing Indicator | Animated dots while AI is generating |
| Message History | Scrollable conversation history |
| Session Persistence | Conversation survives page refresh (until tab close) |
3. Voice Mode Features
| Feature | Description |
|---|---|
| LiveKit WebRTC | Professional-grade voice quality with echo cancellation |
| Audio Visualization | Real-time frequency bars show when agent is speaking |
| Mic Mute | Tap mic icon to mute/unmute yourself |
| Connection Status | Shows “Connecting…”, “Connected”, “Agent speaking…” |
| Auto-Reconnect | Attempts to reconnect on connection loss |
4. Responsive Design
| Screen Size | Widget Width | Widget Height |
|---|---|---|
| Mobile (less than 768px) | 300px | 350px (collapsed) to 400px (expanded) |
| Desktop (768px and up) | 470px to 800px (zoomed) | 470px |
Customization
Changing Colors
The widget uses a purple gradient theme by default (#544AA8 → #312D69). To customize:
Method 1: Via window.__TALKIFAI__
data-* attributes
Environment Variables
Frontend (Next.js)
Backend (API Proxy Routes)
NEXT_PUBLIC_*variables are exposed to browser (safe for widget key)TALKIFAI_API_KEYis server-side only (never exposetk_live_*keys)
Security Considerations
Widget Key vs Secret Key
| Key Type | Prefix | Usage | Security |
|---|---|---|---|
| Widget Key | tk_widget_... | Browser-safe, domain-locked | ✅ Safe for frontend |
| Secret Key | (no prefix, 64-char) | Server-side only | ❌ Never expose to browser |
- ✅ Domain-locked (only work on your allowed domains)
- ✅ Rate-limited for security
- ✅ Cannot create other API keys
- ✅ Can be revoked without affecting other keys
- ✅
localhostalways allowed for development
Creating a Widget Key in TalkifAI Studio
Steps:- Go to Organization → API Keys
- In “Generate New Key” section:
- Enter Key Name (e.g., “Website Widget”)
- Select “Widget Key” (not “Secret Key”)
- Add Allowed Domains (comma-separated)
- Click “Generate API Key”
- Copy immediately — shown only once
- Subdomains automatically covered (
mysite.comincludes*.mysite.com) localhostalways 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:- Go to Organization → API Keys
- Find your widget key in “Your API Keys” section
- Click to edit (or revoke and create new)
- Update allowed domains list
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
sessionStorage(cleared on tab close)- Memory (React state/ref)
localStorage(persists indefinitely)- Cookies without
HttpOnlyflag
Troubleshooting
Widget Not Appearing
Check:- Config script loaded before
widget.js window.__TALKIFAI__is defined (check browser console)- No JavaScript errors in console
- Widget script URL is correct
“Failed to Create Session”
Causes:- Invalid widget key
- Agent ID not found
- CORS origin not configured
- Insufficient credits
Voice Mode Not Working
Check:- Microphone permissions granted
- LiveKit URL is reachable
- Agent supports voice (Pipeline or Realtime architecture)
- Browser supports WebRTC (Chrome, Firefox, Safari, Edge)
Text Streaming Not Working
Check:- API returns
Content-Type: text/event-stream - SSE parser handles all event types
- Network tab shows streaming response
Customization
Changing Colors
The widget uses a purple gradient theme by default (#544AA8 → #312D69). To customize:
Method 1: Via window.__TALKIFAI__
data-* attributes
Performance Optimization
Lazy Loading
Load widget only when user interacts:Preloading
Preload widget script for faster load:Browser Support
| Browser | Version | Text Mode | Voice Mode |
|---|---|---|---|
| Chrome | 90+ | ✅ | ✅ |
| Firefox | 88+ | ✅ | ✅ |
| Safari | 14+ | ✅ | ✅ |
| Edge | 90+ | ✅ | ✅ |
| Opera | 76+ | ✅ | ✅ |
| Samsung Internet | 14+ | ✅ | ✅ |
- WebRTC support
- Microphone permissions
- Audio playback
- 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):- ✅ 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:- Remove old widget script
- Add TalkifAI config + widget
- Update agent in TalkifAI Studio
- Test on staging first
- 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: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
Related Documentation
- Chat API Reference — REST API for custom integrations
- Create a Text Agent — Set up your first agent
- Conversation Memory — Enable long-term memory
- Custom Functions — Add API integrations
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:- Email: support@talkifai.dev
- Documentation: https://docs.talkifai.dev
- Status: https://status.talkifai.dev