---
title: "Put a Dewee agent on your website: a chatbox that thinks, uses tools, and never leaks your token"
url: "https://zuey.me/articles/gan-dewee-agent-len-website?lang=en"
locale: en
access: free
full_text: true
published_at: "2026-10-10T10:00:21.831Z"
tags: ["AI Agents", "Dewee", "Programming", "Developer Tools", "Open Source"]
translations:
  vi: "https://zuey.me/articles/gan-dewee-agent-len-website.md?lang=vi"
  zh: "https://zuey.me/articles/gan-dewee-agent-len-website.md?lang=zh"
  ko: "https://zuey.me/articles/gan-dewee-agent-len-website.md?lang=ko"
  ja: "https://zuey.me/articles/gan-dewee-agent-len-website.md?lang=ja"
---
# Put a Dewee agent on your website: a chatbox that thinks, uses tools, and never leaks your token

> One script tag, one backend route, and your website has a real AI agent on duty: streamed reasoning, tool calls, agent switching, running on Next.js, Astro, React, Vue, PHP. A detailed guide + the open-source dewee-webchat repo 😁

![dewee-webchat page: Your Dewee agent, on any website](https://webchat.dewee.sh/og.jpg)

*webchat.dewee.sh: the official dewee-webchat playground*

*One `<script>` tag, one backend route, and your website has a real agent on duty. Not an FAQ bot that answers "Sorry, I didn't understand your question".*

This week I sat down to do something that sounds super "boring": **putting a chatbox on a website**.

If you build websites, you've probably dropped in a chat widget or two. Copy the script, paste it in the footer, done. Easy peasy.

But the moment you want to plug in an **AI agent** (one with tools, MCP, reasoning), things start to feel off:

- Where does the gateway token live? Put it in client-side JavaScript and you're basically handing it out to the whole internet :))))
- The agent spends 10–20 seconds running tools, and visitors stare at a frozen screen thinking the site is dead.
- Every framework does it differently: Next.js one way, Astro another, Vue yet another, and the PHP folks… there are still a LOT of you out there.

So I just built **dewee-webchat**: an open-source package for putting a Dewee agent on any website.

> **The widget handles the display. Your backend holds the keys. The gateway brings the brains.**

[nextlevelbuilder/dewee-webchat](https://github.com/nextlevelbuilder/dewee-webchat)

> GitHub — Source code, 7 working examples, HTTP contract docs. MIT.

[Playground: webchat.dewee.sh](https://webchat.dewee.sh)

> dewee webchat — Tweak the config, chat with a real agent, copy the code for your framework.

---

### 1/ Why not call the gateway straight from the browser?

The first question everyone asks: "The gateway already has a WebSocket, why not connect straight from the browser and keep it simple?"

Because to connect, you need a token.

And a token sitting in the browser means **anyone who opens DevTools can grab it**. Grab the token and they can call every agent you have, burn through your model budget, and read other people's sessions.

So dewee-webchat's architecture is split into 3 layers:

```mermaid
sequenceDiagram
    autonumber
    participant B as Browser (dewee-chat)
    participant S as Backend /api/dewee
    participant G as Dewee gateway
    B->>S: POST /session
    S-->>B: visitor token (HMAC-signed)
    B->>S: POST /chat (visitor token + message)
    S->>S: check origin, rate limit, agent allowlist
    S->>G: WebSocket (gateway token) chat.send
    G-->>S: thinking / tool.call / tool.result / chunk
    S-->>B: SSE stream, event by event
    G-->>S: run.completed
    S-->>B: done
```

*The browser only holds a visitor token. The gateway token stays put on the backend.*

- The **Browser** only gets an HMAC-signed *visitor token*. That token only means something to your backend.
- The **Backend** holds the gateway token, checks origin, rate limit, and the agent allowlist, and only then opens a WebSocket to the gateway.
- The **Gateway** streams back: thinking, tool calls, tool results, and the answer chunk by chunk.

Each visitor is mapped to their own user on the gateway (`webchat-<id>`). So everyone only sees their own chat history; visitor A can't open the site and read visitor B's chats.

### 2/ When the agent thinks, let visitors… watch it think

This is my favorite part.

Good agents are usually **slow**. They have to read pages, call MCP, look up data. A question like "Read the pricing page for me and compare the 2 plans" can take 15 seconds.

15 seconds of staring at a spinning spinner is a long time.

So the widget streams the agent's whole working process:

1. 💭 **Thinking**: what the agent is thinking (can be turned off).
2. 🔧 **Tool call**: which tool the agent is calling, with which arguments.
3. ✅ **Tool result**: what the tool returned (collapsed, click to expand).
4. ✍️ **The answer** streams in word by word, with markdown, tables, code blocks.

![dewee-webchat widget showing Thought for 9s, tool read url done, and a summary table](https://webchat.dewee.sh/screenshots/chat-tool-call.jpg)

*The agent thinks for 9 seconds, calls the read_url tool to read dewee.sh, then answers with a table. The visitor sees the whole process.*

Visitors see the agent *working* instead of *hanging*. Totally different feeling.

> **TIP:** Running an online shop and don't want to show off the agent's "inner monologue"? One line of config: `show-reasoning="false" show-tool-calls="false"`. Done.

### 3/ Add it to your website in 3 steps

#### Step 1: Create a narrowly scoped API key

Don't use an admin token for your website. Create a separate key that can only chat:

```bash
dewee api-keys create --name "website chat" --scopes operator.read,operator.write
```

If the key ever leaks, revoke just that key and nothing else is affected.

#### Step 2: Add the backend route

```bash
npm i dewee-webchat
```

Set the environment variables:

```bash
DEWEE_GATEWAY_URL=https://gateway.your-domain.com
DEWEE_GATEWAY_TOKEN=...            # the key you just created, ONLY on the server
DEWEE_WEBCHAT_SECRET=...           # openssl rand -base64 32
DEWEE_AGENTS=support,sales         # which agents visitors may chat with
```

Then mount the handler. The handler is a standard `(Request) => Response` function, so it runs pretty much anywhere: Cloudflare Workers, Next.js, Astro, Bun, Deno, Node.

```ts
// Next.js: app/api/dewee/[...path]/route.ts
import { createDeweeHandler, optionsFromEnv } from "dewee-webchat/server";

export const dynamic = "force-dynamic";
const handler = createDeweeHandler(optionsFromEnv(process.env));
export { handler as GET, handler as POST, handler as OPTIONS };
```

**Express, Cloudflare Workers, PHP? Click to see**

```js
// Express
import { createDeweeHandler, optionsFromEnv, toNodeHandler } from "dewee-webchat/server";
app.all("/api/dewee/*path", toNodeHandler(createDeweeHandler(optionsFromEnv(process.env))));
```

```ts
// Cloudflare Workers
import { createDeweeHandler, optionsFromEnv } from "dewee-webchat/server";
let dewee;
export default {
  fetch(req, env) {
    if (new URL(req.url).pathname.startsWith("/api/dewee/"))
      return (dewee ??= createDeweeHandler(optionsFromEnv(env)))(req);
    return env.ASSETS.fetch(req);
  },
};
```

For PHP you only need **one file**, `dewee-webchat.php`. No Composer, just drop it on shared hosting and it runs:

```php
<?php
require __DIR__ . '/dewee-webchat.php';
DeweeWebchat::loadEnvFile(__DIR__ . '/.env');
DeweeWebchat::handle();
```

#### Step 3: Add the widget

Play with it right here, and the code for your framework is generated automatically 👇

**Interactive:** dewee-webchat embed code generator

*Tweak the options, pick your framework, copy it into your project.*

[Open the interactive version](https://zuey.me/articles/gan-dewee-agent-len-website?lang=en)

Under the hood it's all **one Web Component**, `<dewee-chat>`, living in Shadow DOM, so your site's CSS can't break it, and it won't break your CSS either.

### 4/ Commonly used config options

| Option | Default | What it does |
| --- | --- | --- |
| `mode` | `floating` | `floating` = bubble in the corner of the screen, `inline` = embedded right in the page |
| `agent` | default agent | Pick the agent |
| `allow-agent-switch` | `true` | Let visitors switch between agents |
| `show-reasoning` | `true` | Show/hide the thinking |
| `show-tool-calls` | `true` | Show/hide tool calls |
| `theme` | `light` | `light`, `dark`, `auto` |
| `accent-color` | `#5446e8` | Accent color |
| `persist` | `true` | Keep chat history across page reloads |
| `locale` | follows the page | `vi`, `en` |

There's also a JavaScript API: `open()`, `send(text)`, `newConversation()`… plus events like `dewee:reply` and `dewee:error` for firing analytics.

Little trick: want an "Ask the agent about this product" button on your product detail page? Just do:

```js
document.querySelector("dewee-chat").send("Help me out with this product: " + product.name);
```

### 5/ Security: what's already built in

Put AI on a public website and you have to think about people trying to break it. The package already handles:

- [x] The gateway token never reaches the browser.
- [x] HMAC-signed visitor tokens, each visitor gets their own user on the gateway.
- [x] Agent allowlist: visitors can only chat with agents you allow, even if they tamper with the request.
- [x] Origin allowlist: other sites can't piggyback on your backend.
- [x] Rate limit: by default 20 messages/minute per visitor, 10 new sessions/10 minutes per IP.
- [x] Message length limit (4000 characters by default).
- [x] Cloudflare Turnstile support to block bots.

> **WARN:** The default rate limit is **in-memory**. If you run multiple instances (serverless, for example), pass in a shared store via the `rateLimit` option. On Cloudflare Workers, for example, I just use Cloudflare's Rate Limiting binding.

### 6/ Real running examples, not slide-deck demos

I deployed all the examples to Cloudflare, so you can click in and chat right away:

| Stack | Demo | Code |
| --- | --- | --- |
| HTML + Worker (playground) | [webchat.dewee.sh](https://webchat.dewee.sh) | [examples/cloudflare](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/cloudflare) |
| Next.js | [webchat-next.dewee.sh](https://webchat-next.dewee.sh) | [examples/nextjs](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/nextjs) |
| Astro | [webchat-astro.dewee.sh](https://webchat-astro.dewee.sh) | [examples/astro](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/astro) |
| React + Vite | [webchat-react.dewee.sh](https://webchat-react.dewee.sh) | [examples/react-vite](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/react-vite) |
| Vue + Vite | [webchat-vue.dewee.sh](https://webchat-vue.dewee.sh) | [examples/vue-vite](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/vue-vite) |
| Node + Express | run locally | [examples/node-express](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/node-express) |
| PHP | run locally | [examples/php](https://github.com/nextlevelbuilder/dewee-webchat/tree/main/examples/php) |

The playground has a form for tweaking the config live: change the color, toggle reasoning, switch agents, and it generates the matching code for each framework. Tweak, copy, paste, and it runs.

### 7/ A few things I learned building this

**One: Web Components are the right choice for embeddable widgets.**

Write it once; React/Vue/Astro are just thin wrappers. Shadow DOM takes care of the CSS-conflict nightmare for you.

**Two: SSE is plenty, no need for WebSocket on the browser side.**

Browser → backend is an HTTP POST that returns an SSE stream. It goes through every proxy, CDN, and serverless platform. WebSocket is only needed for the backend → gateway leg.

**Three: Showing the agent's work is UX, not a side feature.**

Visitors trust the answer more when they can see the agent *actually* read the pricing page instead of making things up.

**Four: Don't forget the PHP crowd.**

A huge number of SME websites in Vietnam still run on WordPress and shared hosting. A single dependency-free PHP file is the fastest way to get them an AI agent on their site.

**Survey:** Which stack will you put dewee-webchat on?

- Plain HTML / WordPress (script tag)
- Next.js
- Astro
- React / Vue SPA
- PHP / Laravel
- Cloudflare Workers

(vote at https://zuey.me/articles/gan-dewee-agent-len-website?lang=en)

---

The package is MIT, so use it however you like. Try it on your site, and if you hit any bugs, open an issue on [GitHub](https://github.com/nextlevelbuilder/dewee-webchat).

Don't have a Dewee agent yet? Head over to [dewee.sh](https://dewee.sh), spin one up, then come back here and put it on your site 😁
