Skip to main content
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:
Body:

Request Fields

User Identifier (Important)

The user_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
If not provided, a throwaway UUID is generated and memory cannot be retrieved later.

Response

Status: 200 OK

Response Fields


Authentication Modes

Mode 1: API Key (External Embeds)

Use Case: Customer-facing chat widgets on external websites Flow:
  1. Your backend calls /v1/chat/sessions with your API key
  2. Your backend receives session_token
  3. Your backend sends session_token to frontend
  4. Frontend uses session_token for all chat requests
Security: API key never exposed to browser

Mode 2: Studio Token (Internal Preview)

Use Case: TalkifAI Studio agent testing and preview Flow:
  1. User logged into TalkifAI Studio
  2. Frontend has Better Auth session token
  3. Frontend sends X-Studio-Token header
  4. Session created with user’s active organization

What Happens on Session Creation

  1. Authentication Verified
    • API key validated against database
    • OR Studio token verified against session table
  2. Agent Loaded
    • Agent configuration fetched
    • Custom functions loaded
    • Subagents loaded (if configured)
    • Knowledge bases linked
  3. Memory Context Resolved
    • If user_identifier provided: resolve Graphiti memory_id
    • Load historical context from knowledge graph
    • Inject into system prompt
  4. Credit Check
    • Organization credits verified
    • Fail-closed: insufficient credits = 402 error
  5. Billing Session Started
    • Billing session created in database
    • Session type: chat
    • Ready for message-based billing
  6. Session Stored
    • Session stored in memory (SessionStore)
    • Conversation input initialized
    • Activity tracking started
  7. Greeting Generated
    • Based on agent’s greetingType
    • Saved to ChatMessages table
    • Returned in response
  8. 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:
Correct:

2. Provide User Identifier

Always provide user_identifier for returning user memory:
Without it, every session is isolated and memory cannot be retrieved.

3. Handle Greeting Properly

Check if greeting exists before displaying:

4. Store Session Token Securely

Browser:
React Native:

5. Handle Errors Gracefully



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

x-api-key
string
header
required

Your TalkifAI API key. Generate from Studio → Settings → API Keys. Format: tk_live_...

Body

application/json
agent_id
string
required

ID of a Text architecture agent

Example:

"5b710eca-ee67-4c3a-aeb6-8b541f451b40"

Response

Session created

conversation_id
string
Example:

"chat_5b710eca_user123_1705312800000"

session_token
string

JWT valid for 24 hours

agent_name
string
Example:

"Support Agent"

greeting
string | null
Example:

"Hello! How can I help you today?"

agent_model
string
Example:

"gpt-4o-mini"