JavaScript API for Iframe AddOns

Overview

The JavaScript API enables communication between your external iframe AddOn and the Beefree editor using the browser's postMessage API. This bidirectional messaging system is the foundation of all iframe-based addons—it's how your addon tells Beefree it's ready, receives context about the editor environment, sends content to be inserted, and signals when operations are complete or canceled. Understanding this API is essential for building functional iframe addons.

You do not need this if:

  • You're using the Content Dialog Method

Communication Protocol

Message Structure

All messages follow a consistent JSON structure with an action property identifying the message type and an optional data property containing the payload. This standardization makes the API predictable and easy to implement—every message you send or receive follows this same pattern, reducing cognitive overhead and making debugging straightforward. The action field is always a string, and the data field can contain any JSON-serializable data appropriate for that action.

All messages use this format:

{
  action: string,  // Action type: 'loaded', 'init', 'onSave', etc.
  data: any        // Optional payload specific to the action
}

Communication Flow

Understanding the message flow sequence is critical for proper addon implementation. Your iframe must send loaded first to signal readiness, then wait for Beefree to respond with init containing context. If the user is editing existing content, Beefree also sends load with the current content. Finally, your addon sends either onSave (to insert content) or onCancel (to abort) based on user actions. Missing any step in this flow causes the addon to malfunction.

Your Iframe                          Beefree Editor
──────────────────────────────────────────────────
1. Send 'loaded' ──────────────────→
                  ←──────────────── 2. Send 'init'
                  ←──────────────── 3. Send 'load' (if editing)
[User interacts with your UI]
4. Send 'onSave' ──────────────────→
   OR
   Send 'onCancel' ─────────────────→

Messages Your AddOn Sends

1. loaded — Iframe is Ready

The loaded message is your addon's first communication with Beefree—it signals that your iframe has finished loading, initialized its UI, and is ready to receive messages. This is a critical handshake; without it, Beefree doesn't know your addon is ready and won't send the init message or display your interface. Always send this message as early as possible in your application's lifecycle, typically in a DOMContentLoaded or useEffect hook.

When: Immediately after your iframe loads Purpose: Tells Beefree the AddOn is ready to communicate

With Modal Configuration

The data object in the loaded message controls the visual presentation of your addon within Beefree. Setting appropriate dimensions ensures your UI displays optimally. The isRounded property adds modern rounded corners, hasTitleBar provides a close button and context, and showTitle displays your addon's name. These visual options let you match Beefree's design language while maintaining your addon's identity.

Options:

Property
Type
Default
Description

isRounded

Boolean

false

Rounded modal corners

hasTitleBar

Boolean

false

Show title bar with close button

showTitle

Boolean

false

Display AddOn name in title

width

String

'100%'

Modal width ('600px', '80%', '80vw')

height

String

'100%'

Modal height ('400px', '90%', '90vh')

2. onSave — Send Content to Editor

The onSave message is how your addon delivers content to be inserted into the email template. The data object must contain a valid content object matching your addon type's schema—this is where all your addon's work culminates in structured data that Beefree can process. The schema validation is strict: missing required properties, incorrect property names, or wrong data types cause silent failures. Always validate your content object before sending to avoid frustrating debugging sessions.

When: User clicks save/submit button Purpose: Insert content into the editor

Examples by Type

Each addon type has its own schema requirements. These examples show the minimal valid structure for common types—understanding these patterns is essential for successful content insertion. Note the subtle differences: images need src and alt, buttons need label, mixed content needs an array. Learning these schemas thoroughly prevents the frustration of silently failing insertions.

Image:

Button:

Paragraph:

Mixed Content:

3. onCancel — User Canceled

The onCancel message tells Beefree to close your addon's modal without inserting any content. This is essential for the user experience, every interaction path must end with either onSave or onCancel. Users might cancel by clicking a cancel button, closing the dialog, or hitting escape.

When: User clicks cancel or closes modal Purpose: Close modal without inserting content

Messages Your AddOn Receives

1. init — Editor Context

After Beefree receives your loaded message, it responds with init containing context about the editor environment. This context lets your addon adapt its behavior: the locale property enables internationalization, showing your UI in the user's language. The hasOpenOnDrop boolean indicates whether Direct Open triggered the addon (automatic on drop), allowing different workflows for quick inserts versus detailed configuration. The optional data property can pass through custom information configured in the host application.

When: After Beefree receives your loaded message Contains: Editor locale, Direct Open status, and optional custom data

Structure:

2. load — Edit Existing Content

When users click to edit content they previously inserted with your addon, Beefree sends the load message containing the existing content object. This enables stateful addons where users can modify their previous choices rather than starting from scratch each time. Your addon should use this data to pre-fill its UI, showing the current configuration. Users see what they created before and can make targeted changes.

When: User is editing previously inserted content Contains: The existing content object that was originally inserted

Structure:

Complete Implementation

This complete example demonstrates a production-ready iframe addon with proper message handling, user input validation, edit mode support, and comprehensive comments. It shows the full lifecycle: initialization, message listening, content creation, and cleanup. Use this as a template for your own addons, replacing the HTML/textarea with your actual UI components. The pattern shows immediate loaded, centralized message listener, validation before sending

Troubleshooting

Issue
Solution

Messages not received

Check browser console for errors, verify postMessage syntax is exact, ensure listener is added before messages arrive

Modal not appearing

Ensure loaded message is sent immediately on page load, check modal config properties are valid, verify health check endpoint is responding

Content not inserting

Validate content object schema matches addon type exactly, check for missing required properties, test with minimal valid object first

Can't edit existing content

Implement load message listener, pre-fill UI with event.data.value contents, handle case where value might be undefined

Cross-browser issues

Test in all major browsers (Chrome, Firefox, Safari, Edge), check for browser-specific postMessage quirks, use polyfills if needed

Button not displaying

Verify padding and border-radius values are numbers, not strings

Last updated

Was this helpful?