> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tryhamsa.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat with your agent from your page

> Hold a text conversation with a Hamsa agent with the Web SDK: start a chat-only session, send the visitor's messages, and show the agent's replies as they stream in.

The same agent that takes voice calls can hold a text chat. A chat never asks for the microphone and plays no audio: the visitor types, and the agent's replies stream into your page.

## Start a chat

```javascript theme={null}
const agent = new HamsaVoiceAgent({ publicKey: "pk_..." });

agent.on("chatMessageReceived", (message) => {
  renderMessage(message); // see below
});

await agent.start({ agentId: "YOUR_AGENT_ID", isChatOnly: true });
```

With a [call token](https://docs.tryhamsa.com/developers/sdks/web-sdk/start-calls.md#with-a-call-token-from-your-server), also send `isChatOnly: true` when your server asks for the token.

The agent's greeting arrives as its first message.

## Send the visitor's message

```javascript theme={null}
form.addEventListener("submit", async (event) => {
  event.preventDefault();
  const text = input.value.trim();
  if (!text) return;
  renderMessage({ id: crypto.randomUUID(), role: "user", text, isFinal: true });
  input.value = "";
  await agent.sendMessage(text); // fires messageSent
});
```

`sendMessage()` throws if the chat hasn't started, or if the text is empty. `chatMessageReceived` brings you the agent's messages, so show the visitor's message yourself when they send it, as above.

## Show the agent's replies as they stream in

`chatMessageReceived` fires several times for one reply while the agent writes it, then once more when it's complete. Each message has:

| Field | What it is |
| - | - |
| `id` | The same for every update of one message, so you can update one bubble in place. |
| `role` | `agent` or `user`. |
| `text` | All the text so far, not only the new part. |
| `isFinal` | `false` while the message is still coming, `true` when it's complete. |
| `timestamp` | When the SDK received this update, in milliseconds. |

```javascript theme={null}
const bubbles = new Map();

function renderMessage(message) {
  let bubble = bubbles.get(message.id);
  if (!bubble) {
    bubble = document.createElement("p");
    bubble.className = message.role;
    messages.append(bubble);
    bubbles.set(message.id, bubble);
  }
  bubble.textContent = message.text; // the full text so far
  bubble.classList.toggle("typing", !message.isFinal);
}
```

## Show that the agent is typing

```javascript theme={null}
agent.on("agentStateChanged", (state) => {
  typingIndicator.hidden = !(state === "thinking" || state === "speaking");
});
```

## End the chat

```javascript theme={null}
agent.end(); // fires callEnded
```

## Good to know

* `chatMessageReceived` fires only for chats. `messageReceived` also fires for chat, but it fires for voice transcripts as well, so prefer `chatMessageReceived` for a chat window.
* In a chat, the voice events (`transcriptionReceived`, `answerReceived`) and the audio events don't fire.
* A chat is a call: it has a `jobId`, shows in **Call history**, and sends the agent's [webhook](https://docs.tryhamsa.com/agents/connect/webhooks.md).

## Related

> [card] [Events](https://docs.tryhamsa.com/developers/sdks/web-sdk/events.md)
>
> Every event the SDK sends.

> [card] [Start calls](https://docs.tryhamsa.com/developers/sdks/web-sdk/start-calls.md)
>
> Keys, call tokens, and why a chat can't start.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.