# Iframe Developer Guide
> Embed your agent inline with a plain iframe, and talk to it with postMessage
The iframe runs the same chat as the Chat bubble, built into your page instead of floating over it. The Chat bubble's script is a wrapper that creates this same iframe and posts messages to it. With a plain iframe there's no `window.qwryai`, so your page talks to the chat with `postMessage`, in both directions.
## When to use the iframe
Use the iframe when:
* The chat sits inside your page, such as a help center section, a dashboard panel or a full-page chat.
* You want the chat always visible, with no button to click.
* Your platform blocks third-party scripts but allows iframes.
* You show the chat in a mobile app's WebView. See [Mobile apps](#mobile-apps).
Use the [Chat bubble](/docs/developer-guides/website-integration/chat-bubble) when you want a floating button, the full JavaScript API and client-side Actions.
## Quick start
**1. Copy the iframe code**
In your agent, open **Channels**, select **Manage** on the **Chat bubble** card, open **Deploy** and choose **Website iframe**. Select **Copy** under **Iframe setup**.

**2. Add it to your page, with an id**
Paste the code where the chat should appear. Give it an `id` so your code can find it, and size it to your layout. Keep `allow="microphone"` if visitors use voice or dictation.
```html
```
**3. Listen to the chat**
Add a script after the iframe to log the agent's replies:
```javascript
const QWRYAI_ORIGIN = "https://www.qwryai.com";
window.addEventListener("message", (event) => {
if (event.origin !== QWRYAI_ORIGIN) return;
if (event.data?.type === "NEW_MESSAGE") {
console.log("Agent replied:", event.data.message.content);
}
});
```
**4. Test it**
Load the page and send a message in the chat.
> [!TIP]
>
> The agent answers in the iframe, and its reply appears in your browser console.
## Send commands to the iframe
Post commands to the iframe's window, targeting QwryAI's origin:
```javascript
const chat = document.getElementById("qwryai-chat");
const QWRYAI_ORIGIN = "https://www.qwryai.com";
function sendToChat(message) {
chat.contentWindow.postMessage(message, QWRYAI_ORIGIN);
}
```
### Timing
The chat listens once it has loaded. Wait for the iframe's `load` event before you post:
```javascript
chat.addEventListener("load", () => {
sendToChat({ type: "WIDGET_START_NEW_CHAT" });
});
```
### Who may send commands
The iframe only accepts commands from pages allowed to embed it:
* With **Specific domains** off, the page that embeds the iframe is allowed.
* With **Specific domains** on, the page's origin must be in the list.
A command from any other origin is refused with `WIDGET_IDENTIFY_ERROR` and the code `INVALID_ORIGIN`.
## Common tasks
### Identify a logged-in user
Send the JWT your server signed with the identity secret. See [Identity verification](/docs/developer-guides/website-integration/identity-verification) for creating the token.
```javascript
const { token } = await fetch("/api/qwryai-token", { credentials: "include" }).then((r) => r.json());
sendToChat({ type: "WIDGET_SET_USER", payload: { token } });
```
The chat answers with `WIDGET_IDENTIFIED`, whose `payload.success` says whether the token was accepted.
### Start a new conversation from a button
```javascript
document.getElementById("new-chat").addEventListener("click", () => {
sendToChat({ type: "WIDGET_START_NEW_CHAT" });
});
```
The chat answers with `WIDGET_NEW_CHAT_STARTED`.
### Ask a question for the user
```javascript
sendToChat({ type: "WIDGET_SEND_MESSAGE", text: "What are your opening hours?", requestId: "faq-1" });
```
The chat answers with `MESSAGE_SENT` and the same `requestId` once the message is accepted, or `MESSAGE_SEND_ERROR` if it was refused.
### Match your page's theme
```javascript
sendToChat({ type: "WIDGET_SET_THEME", theme: "dark" }); // "light" or "dark"
```
### Log the user out
```javascript
sendToChat({ type: "WIDGET_LOGOUT" });
```
The chat answers with `WIDGET_LOGGED_OUT`.
### Read the visitor's contact
```javascript
sendToChat({ type: "WIDGET_GET_CONTACT" });
// The chat answers with { type: "WIDGET_CONTACT", payload: { success: true, contact } }
```
### Track messages in analytics
```javascript
window.addEventListener("message", (event) => {
if (event.origin !== QWRYAI_ORIGIN) return;
if (event.data?.type === "MESSAGE_SENT") analytics.track("Chat message sent");
if (event.data?.type === "NEW_MESSAGE") analytics.track("Chat reply received");
});
```
## Feature support
### Works in the iframe
* Chatting with your agent, including voice and dictation when `allow="microphone"` is set.
* Identifying users with a signed token, logging out and starting a new conversation.
* Sending messages for the visitor, switching the theme and reading the visitor's contact.
* `MESSAGE_SENT` and `NEW_MESSAGE` events for your analytics.
### Embed script only
These need the [Chat bubble](/docs/developer-guides/website-integration/chat-bubble) script, because they run through `window.qwryai` on your page:
* [Client-side custom Actions](/docs/developer-guides/website-integration/client-side-custom-actions) and [custom forms](/docs/developer-guides/website-integration/client-side-custom-forms).
* The [welcome popup](/docs/developer-guides/website-integration/floating-initial-messages) and the floating button.
* `window.chatbot.on()` [event listeners](/docs/developer-guides/website-integration/chatbot-event-listeners), `open`, `close` and the unread count.
> [!NOTE]
>
> Need custom Actions or forms in an inline layout? Use the Chat bubble script and open the chat from your own button with `window.qwryai("open")`.
## Messages you can send
| `type` | Fields | What it does | Reply |
| - | - | - | - |
| `WIDGET_SET_USER` | `payload`: `{ token }`, or `{ email, name, phone, externalUserId, accountId, customAttributes }` | Identifies the visitor. A `token` is verified; other details are taken as given and need an `email`, `externalUserId` or `accountId`. | `WIDGET_IDENTIFIED` |
| `WIDGET_LOGOUT` | none | Forgets the visitor. | `WIDGET_LOGGED_OUT` |
| `WIDGET_START_NEW_CHAT` | none | Starts a new conversation. | `WIDGET_NEW_CHAT_STARTED` |
| `WIDGET_SEND_MESSAGE` | `text`, optional `requestId` | Sends `text` as the visitor's message. | `MESSAGE_SENT` or `MESSAGE_SEND_ERROR` |
| `WIDGET_SET_THEME` | `theme`: `"light"` or `"dark"` | Switches the chat's theme. | none |
| `WIDGET_GET_CONTACT` | none | Asks for the visitor's contact. | `WIDGET_CONTACT` |
## Events you receive
| `type` | Data | When |
| - | - | - |
| `MESSAGE_SENT` | `message`: `{ conversationId, messageId, sender, content }`, `requestId` | The visitor's message was accepted. |
| `NEW_MESSAGE` | `message`: the reply, with `content`, `sender` and `senderName` | A reply arrived from the agent or a teammate. |
| `WIDGET_IDENTIFIED` | `payload`: `{ success, contact }` or `{ success: false, error }` | After `WIDGET_SET_USER`. |
| `WIDGET_IDENTIFY_ERROR` | `payload`: `{ code, error }` | A command came from an origin that isn't allowed. |
## URL parameters
Add `msg` to the iframe's address to send a first message as soon as the chat connects:
```html
```
> [!NOTE]
>
> `msg` sends the message for the visitor. Use it for a page that answers one question, such as a product's help panel.
## Complete examples
> [tab] HTML
>
> ```html
>
>
>
>
> ```
> [tab] React
>
> ```jsx
> import { useEffect, useRef } from "react";
>
> const QWRYAI_ORIGIN = "https://www.qwryai.com";
>
> export function SupportChat({ token }) {
> const frame = useRef(null);
> const send = (message) => frame.current?.contentWindow?.postMessage(message, QWRYAI_ORIGIN);
>
> useEffect(() => {
> const onMessage = (event) => {
> if (event.origin !== QWRYAI_ORIGIN) return;
> if (event.data?.type === "WIDGET_IDENTIFIED" && !event.data.payload?.success) {
> console.warn("Sign-in failed", event.data.payload?.error);
> }
> };
> window.addEventListener("message", onMessage);
> return () => window.removeEventListener("message", onMessage);
> }, []);
>
> return (
>