Create Chat Session
Text Chat
Create Chat Session
Create a new text chat session. Returns session token, conversation ID, and greeting message.
POST
Create Chat Session
Overview
Creates a new chat session for a text-based AI agent. This endpoint initializes the agent, loads memory context (if enabled), starts billing, and returns a JWT session token for subsequent requests. Authentication:X-API-Key or X-Studio-Token header required
Time: ~300-500ms (agent initialization + memory loading)
Request
Headers:Request Fields
| Field | Type | Required | Description |
|---|---|---|---|
agent_id | string | ✅ | Agent ID (UUID format) |
metadata | object | ❌ | Optional metadata for tracking |
user_identifier | string | ❌ | End-user email for memory linking |
User Identifier (Important)
Theuser_identifier should be the end-user’s email address. This enables:
- Cross-channel memory: Links web chat, voice calls, and telephony conversations
- Returning user context: Memory persists across sessions
- Personalized responses: Agent remembers past interactions
Response
Status:200 OK
Response Fields
| Field | Type | Description |
|---|---|---|
conversation_id | string | Unique session ID (use in all subsequent requests) |
session_token | string | JWT token for authentication (24h expiry) |
agent_name | string | Display name of the agent |
greeting | string | Agent’s opening message (null if user speaks first) |
agent_model | string | LLM model used (e.g., gpt-4o-mini) |
Authentication Modes
Mode 1: API Key (External Embeds)
Use Case: Customer-facing chat widgets on external websites Flow:- Your backend calls
/v1/chat/sessionswith your API key - Your backend receives
session_token - Your backend sends
session_tokento frontend - Frontend uses
session_tokenfor all chat requests
Mode 2: Studio Token (Internal Preview)
Use Case: TalkifAI Studio agent testing and preview Flow:- User logged into TalkifAI Studio
- Frontend has Better Auth session token
- Frontend sends
X-Studio-Tokenheader - Session created with user’s active organization
What Happens on Session Creation
-
Authentication Verified
- API key validated against database
- OR Studio token verified against session table
-
Agent Loaded
- Agent configuration fetched
- Custom functions loaded
- Subagents loaded (if configured)
- Knowledge bases linked
-
Memory Context Resolved
- If
user_identifierprovided: resolve Graphitimemory_id - Load historical context from knowledge graph
- Inject into system prompt
- If
-
Credit Check
- Organization credits verified
- Fail-closed: insufficient credits = 402 error
-
Billing Session Started
- Billing session created in database
- Session type:
chat - Ready for message-based billing
-
Session Stored
- Session stored in memory (SessionStore)
- Conversation input initialized
- Activity tracking started
-
Greeting Generated
- Based on agent’s
greetingType - Saved to ChatMessages table
- Returned in response
- Based on agent’s
-
JWT Token Signed
- Contains: conversation_id, agent_id, organization_id, user_id
- Valid for 24 hours
- Returned as
session_token
Error Responses
400 Bad Request
401 Unauthorized
402 Payment Required
404 Not Found
503 Service Unavailable
Examples
cURL (API Key)
JavaScript (Backend Proxy)
Next.js API Route
Python (FastAPI)
React (Frontend with Backend Proxy)
Best Practices
1. Always Use Backend Proxy
Never expose API key in frontend: ❌ Wrong:2. Provide User Identifier
Always provideuser_identifier for returning user memory:
3. Handle Greeting Properly
Check if greeting exists before displaying:4. Store Session Token Securely
Browser:5. Handle Errors Gracefully
Related Endpoints
- Send Message — Send messages with SSE streaming
- Get History — Retrieve message history
- End Session — End session and cleanup
- Create Voice Session — LiveKit voice session
Next Steps
Send Message
Use the session_token to send messages via SSE streaming.
Integration Guide
See complete examples in the Chat API Guide.
Authorizations
Your TalkifAI API key. Generate from Studio → Settings → API Keys. Format: tk_live_...
Body
application/json
ID of a Text architecture agent
Example:
"5b710eca-ee67-4c3a-aeb6-8b541f451b40"