Skip to content

Headless Agents (No UI Required)

An agent does not need a Wetel widget or any frontend. The workflow, the model calls and the session all run behind the GraphQL API, so anything that can make an authenticated HTTP request can drive an agent: a backend service, a scheduled job, a chat bridge, a CLI. There are two shapes, depending on whether someone is waiting for a reply.

A conversation: three operations and a subscription

Section titled “A conversation: three operations and a subscription”
StepOperationCredentialReturns
1sdkStartX-Api-KeysessionId and a session token (avatarToken)
2sessionEvents(sessionId) subscriptionBearer <avatarToken>the replies, as AiResponseEvents
3sdkSendMessage, once per user messageBearer <avatarToken>true when the message is accepted, no reply
4sdkEndSessionBearer <avatarToken>the duration and the evaluation

The reply never comes back from sdkSendMessage: it arrives on the subscription, and the turn is over when an AiResponseEvent has turnComplete: true. Open the subscription before you send, and pass sinceMessageId when you reconnect so no reply is lost. The calls in detail are in Core API Flow and Events and Subscriptions.

A task with no conversation: runWorkflowTask

Section titled “A task with no conversation: runWorkflowTask”

When an orchestrator already knows what it wants done and nobody waits on a reply, call runWorkflowTask instead. One API-key call runs an agent’s published workflow with structured variables (and an optional attachment) as real context keys your nodes can test, with no message text, no session token and no subscription.

It is asynchronous: a successful return means the run was accepted and queued, not that it succeeded. Read the outcome with workflowTaskOutcome and the sessionId it returned, using the same API key. Calls with the same externalConversationId share one session.

For Telegram you do not need a bridge: a channel connector takes a bot token and Wetel receives each message, runs the session and sends the reply. Which channel types are implemented is listed on Channels and Channel capabilities; for any other platform, build a bridge as below.

Worked example: building your own bridge (for a platform without a packaged connector)

Section titled “Worked example: building your own bridge (for a platform without a packaged connector)”

A bridge turns each message from a chat platform into a turn of a Wetel session and sends the reply back. It uses Telegram’s API below only because it is familiar; for Telegram itself, use the connector above. The same shape fits any platform with a webhook or polling API.

Receive messages. A webhook needs a public HTTPS URL. Long-polling (getUpdates on Telegram) needs only outbound calls, so the bridge can run anywhere, but one process serves one bot.

One Wetel session per external chat. Keep a map from the platform’s chat id to { sessionId, avatarToken }, in memory for one process or in Redis for several. Your bridge holds the API key, so it calls sdkStart itself:

// chatId -> { sessionId, avatarToken }
const sessions = new Map();
async function handleMessage(chatId, text) {
let session = sessions.get(chatId);
if (!session) {
session = await sdkStart(agentId); // X-Api-Key, once per new chat
sessions.set(chatId, session);
}
return turn(session, text);
}

Subscribe, send, collect until the turn is complete. This uses the graphql-ws client; on Node, pass the ws package as webSocketImpl.

import { createClient } from "graphql-ws";
import WebSocket from "ws";
async function turn({ sessionId, avatarToken }, text) {
const client = createClient({
url: WETEL_WS_ENDPOINT,
webSocketImpl: WebSocket,
connectionParams: {
Authorization: `Bearer ${avatarToken}`,
"x-huat-platform": "customer",
},
});
// sessionId is ID! on this subscription, Int! everywhere else.
const events = client.iterate({
query: `subscription Events($sessionId: ID!) {
sessionEvents(sessionId: $sessionId) {
__typename
... on AiResponseEvent { text isFinal turnComplete }
... on SessionEndedEvent { durationSeconds }
}
}`,
variables: { sessionId: String(sessionId) },
});
await sdkSendMessage(avatarToken, sessionId, text); // Bearer avatarToken
const parts = [];
let current = "";
for await (const { data } of events) {
const event = data?.sessionEvents;
if (event?.__typename === "SessionEndedEvent") break;
if (event?.__typename !== "AiResponseEvent") continue;
const piece = event.text.trim();
if (piece) current = current ? `${current} ${piece}` : piece;
if (event.isFinal && current) {
parts.push(current); // one complete message
current = "";
}
if (event.turnComplete) break;
}
client.dispose();
return parts.join("\n\n");
}

An agent without a workflow streams one message as several events and closes it with isFinal: true; a workflow sends each response as one event. A turn can also end with an empty event whose only job is turnComplete: true, which is why empty text is skipped. In production, add a timeout around the loop and keep one socket per session open instead of one per turn.

Send the reply back through the platform’s own API (for Telegram, sendMessage). Then refresh the session token before it expires: see Refreshing the session token.