Guide

Embedding the form preview

Show your users a live, phone-shaped preview of a form — exactly as a patient will see it — inside your own dashboard, by embedding Comflyant's preview in an iframe and driving it over postMessage.

Access: this is an Enterprise white-label feature. The preview only accepts a form from an allow-listed origin, so to use it you must first contact the Comflyant team to get your site's origin (scheme + host, e.g. https://app.yourclinic.com) whitelisted. Until then the preview stays on its “Waiting for preview…” state and silently ignores your messages.

How it works

The preview is a 100% client-side render of a form definition using the real recipient renderer. You pass it a set of questions and it draws the form — including validation, choice/scale controls and the completion screen. Nothing is ever sent to a Comflyant backend and nothing is persisted: there is no form instance, no recipient, and no patient data involved — only the form definition you hand it. That is why it's safe to run inside your own app.

The handshake is two messages:

  1. When the iframe loads, it posts { type: "comflyant-preview-ready" } to its parent window.
  2. In response, you post a comflyant-preview-form message containing the questions. Re-post any time the form changes to live-update the preview (it re-renders from scratch).

The message you send

interface PreviewFormMessage {
  type: "comflyant-preview-form";
  title?: string | null;
  completion_title?: string | null;
  completion_body?: string | null;
  questions: FormQuestion[];
}

interface FormQuestion {
  id: string;            // any unique string within this preview
  position: number;      // display order
  type:
    | "short_text" | "long_text" | "number" | "boolean"
    | "single_choice" | "multiple_choice" | "scale"
    | "date" | "time" | "informational";
  prompt: string;
  is_required: boolean;
  response_mode: "text" | "voice" | "text_or_voice";
  max_voice_seconds: number | null;
  max_length: number | null;
  config: {
    // single_choice / multiple_choice: bare strings OR { label, value } pairs
    options?: Array<string | { label: string; value: string }>;
    // scale: numeric endpoints + optional anchor labels
    min?: number; max?: number; min_label?: string; max_label?: string;
  } | null;
}

These are the same shapes the API reference documents for a form's questions — see the API reference. If you already fetch a form template from /v1/form-templates, map its questions onto FormQuestion and pass them straight through.

Example

<iframe
  id="comflyant-preview"
  src="https://r.comflyant.co.uk/preview"
  title="Form preview"
  style="width: 390px; height: 760px; border: 0; border-radius: 24px;"
></iframe>

<script>
  const PREVIEW_ORIGIN = "https://r.comflyant.co.uk";
  const iframe = document.getElementById("comflyant-preview");

  const config = {
    type: "comflyant-preview-form",
    title: "GAD-7",
    completion_title: "Thank you",
    completion_body: "Your responses have been recorded.",
    questions: [
      {
        id: "q1",
        position: 0,
        type: "scale",
        prompt: "Feeling nervous, anxious, or on edge",
        is_required: true,
        response_mode: "text",
        max_voice_seconds: null,
        max_length: null,
        config: { min: 0, max: 3, min_label: "Not at all", max_label: "Nearly every day" },
      },
    ],
  };

  // The preview announces readiness; send the config in response. It is safe to
  // re-post 'config' whenever the form changes — the preview re-renders live.
  window.addEventListener("message", (event) => {
    if (event.origin !== PREVIEW_ORIGIN) return;
    if (event.data?.type === "comflyant-preview-ready") {
      iframe.contentWindow.postMessage(config, PREVIEW_ORIGIN);
    }
  });
</script>

Security: always send the config with an explicit target origin (postMessage(config, "https://r.comflyant.co.uk")), and the preview will only accept it if your origin is on its allow-list. This two-way origin check is why the whitelisting step above is required.