Overview
The Text Chat API lets you embed a TalkifAI agent as a text chat interface on any website or application. Unlike voice sessions (which use LiveKit), the Chat API uses standard REST + Server-Sent Events (SSE) streaming. Best for:- Website chatbots and live chat widgets
- Mobile app chat interfaces
- Customer support portals
- Any text-first interaction (no microphone needed)
Text agents only. The Chat API requires agents with text architecture. Voice agents (Pipeline/Realtime) use the LiveKit-based voice flow instead.
How It Works
Prerequisites
1. Create a Text Agent
- Go to Studio → Agents → Create Agent
- Under Architecture, select Chat (Text Only)
- Configure your system prompt and LLM model
- Save the agent and copy the Agent ID
2. Get Your API Key
- Go to Studio → Settings → API Keys
- Click Create API Key
- Copy the key — it starts with
tk_live_
Step 1: Create a Session
Call this endpoint from your backend server to create a chat session:Request Fields
Authentication Modes
Mode 1: API Key (External Websites)- Use
X-API-Keyheader - For customer-facing chat widgets
- API key stays on your backend (never expose to browser)
- Use
X-Studio-Tokenheader - For TalkifAI Studio agent preview/testing
- Token from Better Auth session
Step 2: Send a Message
Send messages from the client browser using thesession_token:
Parsing the SSE Stream
The response is a Server-Sent Events (SSE) stream. Each event has anevent: type line and a data: line, separated by blank lines (\n\n). You must parse both to correctly handle all event types:
Step 3: Get Message History
Retrieve conversation history (useful for page refreshes or resuming sessions):Step 4: End the Session
Always end sessions explicitly to trigger cleanup, memory ingestion, and webhooks:- Billing session closed and credits consumed
- Memory ingested into Graphiti (for returning user context)
- Post-call analysis triggered (if configured)
- Webhook fired (if agent has webhook configured)
Sessions automatically expire after 30 minutes of inactivity. Always call
/end explicitly to ensure billing finalization, memory ingestion, and webhooks fire.Complete Integration Example
Here’s a full working example with a simple chat UI:Backend Proxy Pattern
Never expose your API key in frontend code. Use a backend proxy:- Node.js / Express
- Next.js API Route
- Python / FastAPI
React / Next.js Integration
A minimal React hook that handles session lifecycle, SSE streaming, error display, and reliable cleanup on unmount or tab close:The
keepalive: true flag in the cleanup fetch is critical. Without it, browsers cancel in-flight requests when the page unloads — the /end call would never reach the server, leaving billing sessions open and memory ingestion skipped.API Reference
Create Session
Send Message
text/event-stream)
Get History
End Session
Error Codes
Session Management
Session Lock
The API automatically acquires a session lock when processing messages. This prevents:- Concurrent message processing
- Race conditions in message history
- Duplicate responses
Token Tracking
The API automatically tracks:- Input tokens (user messages)
- Output tokens (assistant responses)
- Total token usage per session
stream_end event’s usage field and stored in the database.
Memory Ingestion
When a session ends:- Credits consumed based on session cost
- Conversation saved to
ChatMessagestable - Memory ingested into Graphiti (if enabled on agent)
- Post-call analysis triggered (if configured)
- Webhook fired (if agent has webhook URL)
Next Steps
Create a Text Agent
Set up an agent with text architecture in Studio.
API Keys
Manage your API keys and rate limits.
Conversation Memory
Enable long-term memory so agents remember returning users.
Webhooks
Get notified when conversations end or tools are called.
Related Documentation
- Custom Functions — Add API integrations to your chat agent
- Conversation Memory — Enable long-term user memory
- API Reference — Full Chat API specification