EK Hub API

Integration Flows

Common patterns for building chat integrations with the Headless Chat API.

Flow 1: New Conversation

The most common flow — start a fresh conversation with an AI agent.

GET  /agents                              → Pick an agent
POST /agents/{id}/sessions                → Create session
POST /agents/{id}/sessions/{id}/messages  → Send message, get response
POST /agents/{id}/sessions/{id}/messages  → Send another message

Step-by-step

new-conversation.ts
const BASE = 'http://localhost:3002';
const headers = {
  'X-API-Key': process.env.API_KEY!,
  'Content-Type': 'application/json'
};

// 1. List agents and pick one
const agentsRes = await fetch(`${BASE}/agents`, { headers });
const { agents } = await agentsRes.json();
const agent = agents[0];

// 2. Create a session
const sessionRes = await fetch(`${BASE}/agents/${agent.id}/sessions`, {
  method: 'POST',
  headers,
  body: JSON.stringify({ name: 'Support Chat' })
});
const { session } = await sessionRes.json();

// 3. Send messages
const chatRes = await fetch(
  `${BASE}/agents/${agent.id}/sessions/${session.id}/messages`,
  {
    method: 'POST',
    headers,
    body: JSON.stringify({ message: 'What is your return policy?' })
  }
);
const chat = await chatRes.json();
console.log('AI:', chat.aiMessage.content);

Flow 2: Resume Conversation

Pick up where a user left off by loading an existing session.

GET  /agents/{id}/sessions                → List sessions (optionally filter by user)
GET  /agents/{id}/sessions/{id}           → Load session + message history
POST /agents/{id}/sessions/{id}/messages  → Continue chatting

Step-by-step

resume-conversation.ts
const BASE = 'http://localhost:3002';
const headers = {
  'X-API-Key': process.env.API_KEY!,
  'Content-Type': 'application/json'
};

// 1. List sessions for a specific user
const sessionsRes = await fetch(
  `${BASE}/agents/${agentId}/sessions?externalUserId=user-123`,
  { headers }
);
const { sessions } = await sessionsRes.json();

// 2. Load the most recent session with history
const sessionRes = await fetch(
  `${BASE}/agents/${agentId}/sessions/${sessions[0].id}`,
  { headers }
);
const { session, messages } = await sessionRes.json();

// 3. Display previous messages
for (const msg of messages) {
  console.log(`${msg.role}: ${msg.content}`);
}

// 4. Continue the conversation
const chatRes = await fetch(
  `${BASE}/agents/${agentId}/sessions/${session.id}/messages`,
  {
    method: 'POST',
    headers,
    body: JSON.stringify({ message: 'Can you tell me more about that?' })
  }
);
const chat = await chatRes.json();
console.log('AI:', chat.aiMessage.content);

Tips

  • Store the session ID in your database or user session so you can resume conversations later
  • Use externalUserId when creating sessions to filter sessions by user
  • Follow-up suggestions can be displayed as clickable buttons in your UI — just send the text as the next message

On this page