---
title: ".channel()"
description: "Attach an inbound and outbound messaging channel to an agent."
---

> Documentation Index
> Fetch the complete documentation index at: https://xsaf.ilha.build/llms.txt
> Use this file to discover all available pages before exploring further.

# .channel()

## Signature

```ts
agent.channel(driver: XsafChannelDriver): XsafAgent
```

A channel receives messages from an external surface and sends agent results back to it.

## Usage

```ts
agent.channel({
  name: "support_chat",
  listen(context) {
subscribe((message) =>
  context.dispatch({
    sessionId: message.threadId,
    text: message.body,
  }),
);
  },
  async send(target, payload) {
await publish(target, payload);
  },
  async close() {
await unsubscribe();
  },
});
```

Channel names must be unique. Channels start in registration order and close in reverse order. Startup performs cleanup if a later channel fails.

Bundled adapters are available from `@xsaf/agent/channel/http`, `@xsaf/agent/channel/chat-sdk`, and `@xsaf/agent/channel/mock`. The mock channel is deterministic and offline.

The `chat-sdk` channel bridges the universal `chat` package (Chat SDK) to natively support Slack, Teams, Discord, Telegram, Google Chat, and other platforms.

```ts
import { Chat } from "chat";
import { createSlackAdapter } from "@chat-adapter/slack";
import chatSdk from "@xsaf/agent/channel/chat-sdk";

const bot = new Chat({
  userName: "mybot",
  adapters: { slack: createSlackAdapter() },
  // ... state adapter, etc.
});

agent.channel(chatSdk(bot));
```

The HTTP channel mounts a streaming chat endpoint on the shared Hono app:

```ts
import http from "@xsaf/agent/channel/http";

agent.channel(
  http({
path: "/chat",
apiKey: process.env.API_KEY,
  }),
);
```

Clients requesting `text/event-stream` receive typed response chunks and tool/delegate lifecycle events. Other clients retain the JSON response behavior. When `apiKey` is configured, the endpoint requires `Authorization: Bearer <key>`.

See [Sessions & Memory](/recipes/sessions-memory) for session serialization and [Testing](/recipes/testing) for the mock channel.

Source: https://xsaf.ilha.build/xsaf/channel/index.mdx
