Skip to main content

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

  1. Go to Studio → Agents → Create Agent
  2. Under Architecture, select Chat (Text Only)
  3. Configure your system prompt and LLM model
  4. Save the agent and copy the Agent ID

2. Get Your API Key

  1. Go to Studio → Settings → API Keys
  2. Click Create API Key
  3. Copy the key — it starts with tk_live_
Keep your API key secret. Never expose it in frontend JavaScript. Use a backend server to make session creation requests.

Step 1: Create a Session

Call this endpoint from your backend server to create a chat session:
Response:

Request Fields

Always provide user_identifier (customer email) for returning user memory. Without it, every session is isolated and memory cannot be retrieved later.

Authentication Modes

Mode 1: API Key (External Websites)
  • Use X-API-Key header
  • For customer-facing chat widgets
  • API key stays on your backend (never expose to browser)
Mode 2: Studio Token (Internal)
  • Use X-Studio-Token header
  • For TalkifAI Studio agent preview/testing
  • Token from Better Auth session
Time: ~300–500ms (agent initialization + memory loading)

Step 2: Send a Message

Send messages from the client browser using the session_token:

Parsing the SSE Stream

The response is a Server-Sent Events (SSE) stream. Each event has an event: type line and a data: line, separated by blank lines (\n\n). You must parse both to correctly handle all event types:
SSE Event Types:

Step 3: Get Message History

Retrieve conversation history (useful for page refreshes or resuming sessions):
Response:

Step 4: End the Session

Always end sessions explicitly to trigger cleanup, memory ingestion, and webhooks:
What happens on end:
  1. Billing session closed and credits consumed
  2. Memory ingested into Graphiti (for returning user context)
  3. Post-call analysis triggered (if configured)
  4. Webhook fired (if agent has webhook configured)
Response:
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:

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

Response:

Send Message

Response: SSE stream (text/event-stream)

Get History

Response:

End Session

Response:

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
If a previous message is still streaming, the new request will wait for the lock.

Token Tracking

The API automatically tracks:
  • Input tokens (user messages)
  • Output tokens (assistant responses)
  • Total token usage per session
Token counts are returned in the stream_end event’s usage field and stored in the database.

Memory Ingestion

When a session ends:
  1. Credits consumed based on session cost
  2. Conversation saved to ChatMessages table
  3. Memory ingested into Graphiti (if enabled on agent)
  4. Post-call analysis triggered (if configured)
  5. 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.