Browse and search developers

Embed SDK

Use the embed in React, Next.js and HTML

Load embed.js once, mount Formit embeds from React or Next.js components with createWidget, receive submit events and clean up on unmount.

On this page
  1. Plain HTML
  2. React
  3. Next.js
  4. Overlay modes and route changes

The SDK is a plain script that defines window.FormitEmbed. There is no npm package or React component, so every framework pattern comes down to the same steps: load the script once per page, then call its API on an element you control.

Plain HTML

For a static page, the two-line snippet from Share is all you need. To receive events, mount through the API instead of using the attribute, because elements mounted automatically on load have no callbacks and calling mount() on them again does nothing.

index.html
<div id="signup-form"></div>

<!-- Load once per page, before any script that calls FormitEmbed -->
<script src="https://embed.formitai.com/embed.js?v=7"></script>
<script>
  var handle = FormitEmbed.createWidget('YOUR_EMBED_ID', {
    container: '#signup-form',
    autoResize: true,
    onReady: function () {
      console.log('Form loaded');
    },
    onSubmit: function (payload) {
      // payload: { formId, formVersionId }
      console.log('Submitted', payload);
    },
    onQuestionChanged: function (payload) {
      // payload: { questionId, index, fieldType }
    },
    onClose: function () {
      console.log('Closed');
    },
  });
</script>

The script tag loads synchronously, so FormitEmbed exists by the time the next script runs. If you add async or defer, call the API from the script’s load event instead.

For markup inserted after the page loaded, call FormitEmbed.init() with the new container:

Content added later
<button type="button" id="show-form">Get in touch</button>
<div id="form-slot"></div>

<script src="https://embed.formitai.com/embed.js?v=7"></script>
<script>
  document.getElementById('show-form').addEventListener('click', function () {
    // Markup added after the page has loaded is not scanned automatically.
    var slot = document.getElementById('form-slot');
    slot.innerHTML = '<div data-formit-embed="YOUR_EMBED_ID"></div>';
    FormitEmbed.init(slot); // mounts every unmounted [data-formit-embed] inside #form-slot
  });
</script>

React

Load the script with a small helper that injects it once and caches the promise. Then mount from an effect and clean up when the component unmounts.

lib/formit-embed.ts

// lib/formit-embed.ts
export type FormitEmbedHandle = {
  open: () => void;
  close: () => void;
  toggle: () => void;
  refresh: () => void;
  unmount: () => void;
};

export type FormitEmbedOptions = {
  container?: HTMLElement | string;
  autoResize?: boolean;
  onReady?: () => void;
  onSubmit?: (payload?: unknown) => void;
  onClose?: () => void;
  onStarted?: () => void;
  onQuestionChanged?: (payload?: unknown) => void;
  onHeightChanged?: (height: number) => void;
};

type FormitEmbedSdk = {
  init: (root?: ParentNode) => void;
  createWidget: (embedId: string, options?: FormitEmbedOptions) => FormitEmbedHandle;
  createPopup: (embedId: string, options?: FormitEmbedOptions) => FormitEmbedHandle;
  createSlider: (embedId: string, options?: FormitEmbedOptions) => FormitEmbedHandle;
  createPopover: (embedId: string, options?: FormitEmbedOptions) => FormitEmbedHandle;
  createSidetab: (embedId: string, options?: FormitEmbedOptions) => FormitEmbedHandle;
};

declare global {
  interface Window {
    FormitEmbed?: FormitEmbedSdk;
  }
}

const EMBED_SRC = 'https://embed.formitai.com/embed.js?v=7';
let loading: Promise<FormitEmbedSdk> | null = null;

/** Loads embed.js once per page and resolves with window.FormitEmbed. */
export function loadFormitEmbed(): Promise<FormitEmbedSdk> {
  if (window.FormitEmbed) return Promise.resolve(window.FormitEmbed);
  if (!loading) {
    loading = new Promise((resolve, reject) => {
      const script = document.createElement('script');
      script.src = EMBED_SRC;
      script.async = true;
      script.onload = () =>
        window.FormitEmbed
          ? resolve(window.FormitEmbed)
          : reject(new Error('FormitEmbed global not found'));
      script.onerror = () => {
        loading = null;
        reject(new Error('Failed to load embed.js'));
      };
      document.head.appendChild(script);
    });
  }
  return loading;
}
  • Do not render data-formit-embed in JSX. If the script loads after React rendered that element, it auto-mounts it without callbacks, and a later createWidget on the same element can mount it twice.
  • The cancelled flag covers components that unmount before the script finishes loading, and React Strict Mode running effects twice in development.
  • Keep callbacks in a ref, as above, so a new onSubmit function on each render does not remount the iframe.
  • The type declarations in the loader describe the parts of the SDK used here. The SDK does not ship its own types.

Next.js

The SDK touches window and document, so it must run in the browser. In the App Router, mark the component with use client (as in the component above) and render it from a Server Component page.

app/contact/page.tsx
// app/contact/page.tsx (Server Component)
import { FormitForm } from '@/components/FormitForm';

export default function ContactPage() {
  return (
    <main>
      <h1>Contact us</h1>
      <FormitForm embedId="YOUR_EMBED_ID" />
    </main>
  );
}

In the Pages Router, the same component works without the directive because the effect only runs in the browser. If you prefer to load the script with your framework’s script component and use data-formit-embed markup, remember that client-side navigation does not reload the script. Call window.FormitEmbed?.init() after the new page renders, so its embed divs get mounted.

Overlay modes and route changes

Popup, slider and full page overlays are attached to the document root, not to your container. unmount() removes the full page overlay but not an open popup or slider. If a visitor can navigate away with one open, call handle.close() before handle.unmount() in your cleanup. Note that close() removes every open Formit popup, slider and full page overlay on the page, not only this embed’s, and calls onClose.

Ready?

Build your first AI form in minutes.

No credit card. Generate from a prompt, enable follow-ups, and publish a link or embed the same day.