Skip to content
QwryAI
Documentation
Pages

Website Integration

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.

Use the 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.

    Deploy › Website iframe › 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
    <iframe
      id="qwryai-chat"
      src="https://www.qwryai.com/embed/YOUR_EMBED_KEY"
      width="400"
      height="600"
      frameborder="0"
      title="Chatbot"
      allow="microphone"
    > </iframe>
  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.

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 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 script, because they run through window.qwryai on your page:

Messages you can send

typeFieldsWhat it doesReply
WIDGET_SET_USERpayload: { 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_LOGOUTnoneForgets the visitor.WIDGET_LOGGED_OUT
WIDGET_START_NEW_CHATnoneStarts a new conversation.WIDGET_NEW_CHAT_STARTED
WIDGET_SEND_MESSAGEtext, optional requestIdSends text as the visitor's message.MESSAGE_SENT or MESSAGE_SEND_ERROR
WIDGET_SET_THEMEtheme: "light" or "dark"Switches the chat's theme.none
WIDGET_GET_CONTACTnoneAsks for the visitor's contact.WIDGET_CONTACT

Events you receive

typeDataWhen
MESSAGE_SENTmessage: { conversationId, messageId, sender, content }, requestIdThe visitor's message was accepted.
NEW_MESSAGEmessage: the reply, with content, sender and senderNameA reply arrived from the agent or a teammate.
WIDGET_IDENTIFIEDpayload: { success, contact } or { success: false, error }After WIDGET_SET_USER.
WIDGET_IDENTIFY_ERRORpayload: { 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
<iframe src="https://www.qwryai.com/embed/YOUR_EMBED_KEY?msg=Do%20you%20ship%20to%20Canada%3F" ...></iframe>

Complete examples

HTML
<iframe
  id="qwryai-chat"
  src="https://www.qwryai.com/embed/YOUR_EMBED_KEY"
  width="400"
  height="600"
  frameborder="0"
  title="Chatbot"
  allow="microphone"
> </iframe>
<button id="new-chat">New conversation</button>

<script>
  const chat = document.getElementById("qwryai-chat");
  const QWRYAI_ORIGIN = "https://www.qwryai.com";
  const send = (message) => chat.contentWindow.postMessage(message, QWRYAI_ORIGIN);

  chat.addEventListener("load", async () => {
    const { token } = await fetch("/api/qwryai-token", { credentials: "include" }).then((r) => r.json());
    send({ type: "WIDGET_SET_USER", payload: { token } });
  });

  document.getElementById("new-chat").addEventListener("click", () => {
    send({ type: "WIDGET_START_NEW_CHAT" });
  });

  window.addEventListener("message", (event) => {
    if (event.origin !== QWRYAI_ORIGIN) return;
    if (event.data?.type === "NEW_MESSAGE") console.log("Reply:", event.data.message.content);
  });
</script>

Mobile apps

Open the iframe's address, https://www.qwryai.com/embed/YOUR_EMBED_KEY, in a WebView to show the whole chat in your Android or iOS app. For a native chat screen of your own, use the mobile SDKs instead.

Security

Origins

Always post to https://www.qwryai.com, never "*", and check event.origin on every message you receive.

Allowed domains

With Specific domains on, only the sites in the list can show the iframe, and only they can send it commands.

Secrets and data

Never put your identity secret or API keys in page code. Sign identity tokens on your server.

Iframe attributes

Keep allow="microphone" only if you use voice or dictation. Keep the src exactly as copied: its key identifies your agent.

Troubleshooting

See the iframe's console

In your browser's developer tools, switch the console's context to the iframe to see the chat's own logs.

Common problems

The iframe is blank or refuses to load

If Specific domains is on, add your site's address to the list. Check that the src is exactly as copied.

Commands do nothing

Wait for the iframe's load event before you post, and post to https://www.qwryai.com, not "*".

You receive INVALID_ORIGIN

Your page's origin isn't allowed to send commands. Add it under Specific domains.

The user isn't identified

Check the error in WIDGET_IDENTIFIED, and sign a fresh token with the current identity secret. Tokens need user_id and an exp in the future.

Custom Actions or forms don't run

They need the Chat bubble script. See Feature support.

Next steps

Explore the documentation