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

# Iframe events

> Receive events from the embedded YoLead iframe.

The YoLead iframe uses `window.postMessage` to notify the parent application about integration events.

## `chat.created`

After an outbound message is sent successfully, the iframe sends `chat.created` to its parent window before navigating to the resulting chat.

```json theme={null}
{
  "source": "yolead-embed",
  "event": "chat.created",
  "data": {
    "chatId": "64f000000000000000000002"
  }
}
```

Use `data.chatId` to associate the resulting YoLead chat with the record in your CRM or ERP.

## Listen for the event

```ts theme={null}
const iframe = document.querySelector<HTMLIFrameElement>("#yolead-embed");

if (!iframe) {
  throw new Error("YoLead iframe not found");
}

const yoleadOrigin = new URL(iframe.src).origin;

window.addEventListener("message", (messageEvent) => {
  if (
    messageEvent.origin !== yoleadOrigin ||
    messageEvent.source !== iframe.contentWindow
  ) {
    return;
  }

  const message = messageEvent.data;

  if (
    message?.source !== "yolead-embed" ||
    message?.event !== "chat.created" ||
    typeof message?.data?.chatId !== "string"
  ) {
    return;
  }

  const chatId = message.data.chatId;

  // Associate chatId with the corresponding record in your system.
});
```

<Warning>
  Always validate both `messageEvent.origin` and `messageEvent.source` before using event data. Derive the expected origin from the `iframeUrl` returned by `POST /v1/embed/sessions`.
</Warning>
