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

# Embed Editor

> Let your customers decide how and where a widget of theirs shows on a page, and give them the embed code, beside an assistant that sets the options from a sentence

The **Embed Editor** lets a customer decide how and where a widget of theirs shows on a page: how big it is, whether it sits in the page or opens in an overlay and where, what makes it appear (the visitor leaving the page, a delay, scrolling, page views), which pages and visitors it is for, when it runs and how often it may be shown. It writes the embed code to paste from those options, and an assistant beside the options sets them from a sentence the customer types (see it in [the gallery](https://docs.widgetic.com/docs/partners/ui-components#the-pieces)). It is the Embed panel of Widgetic's own site, drawn in your page.

The [Composition Editor](https://docs.widgetic.com/docs/partners/ui-components/composition-editor.md) changes what a widget says; this one changes how it is shown, so a page that offers both mounts both. [Embed Settings](https://docs.widgetic.com/docs/api/embed-settings.md) says what each option is called and what it does.

Like every piece, it is mounted with the [UI SDK](https://docs.widgetic.com/docs/partners/ui-components) and uses an [editor session](https://docs.widgetic.com/docs/partners/editor-sessions.md) for one of your [managed users](https://docs.widgetic.com/docs/partners/managed-users.md), made with the surface `["editor"]`. It has no file picker, so `files` is not needed, and a session made for the Composition Editor (`["editor", "files"]`) serves it too. Installing the SDK, tokens, the theme, the language and the Content Security Policy are the same for every piece, and are on [the UI Components page](https://docs.widgetic.com/docs/partners/ui-components). This page covers what is specific to the Embed Editor.

## Mount it

Create the composition on your server with your key and file it under the customer (see [Filing Content Under a Managed User](https://docs.widgetic.com/docs/partners/managed-users.md#filing-content-under-a-managed-user)). A session opens only compositions filed under its customer. Then mount the editor on it:

```javascript theme={"theme":"css-variables"}
import { mountModule } from '@widgetic/ui-sdk';

const embed = await mountModule('embed-editor', {
  container: '#embed', // give it a height: it fills its container
  compositionId: 'cmp_123', // the composition of the customer who is on the page; one editor edits one composition
  getToken: async () => (await fetch('/widgetic-session', { method: 'POST' })).json(), // { token, expiresAt }
  on: {
    ready: ({ embedCode }) => showCode(embedCode),
    dirty: ({ dirty }) => markUnsaved(dirty),
    saved: ({ settings, embedCode, savedAt }) => markSaved(savedAt),
    error: ({ message, code, fatal, sessionEnded }) => report(code, message),
  },
});

// later: saves what is unsaved first, then takes the editor away
await embed.unmount();
```

**More than one Embed Editor can share a page.** Each takes one composition, in a box of its own, and keeps its own options.

## Options

| Option | |
| - | - |
| `compositionId` | **Required.** The composition whose embed options to edit. |
| `assistant` | `true` (the default) draws the assistant beside the options. `false` leaves it out. See [The assistant](#the-assistant). |
| `autosaveDelayMs` | How long after the last change the editor saves by itself. Default 1500. `0` turns autosave off, and the *Save* button still works. |
| `snippetBases` | `{ cdnBase, apiBase }`: the addresses the embed code names for the Widgetic script and for the frame, for a page that runs against another environment. |
| `helpUrl` | Where the *Learn more* link under the code leads. An `http` or `https` address. |
| `locale` | A BCP 47 tag such as `de`: plural forms, numbers and dates, and which way the screen reads. |
| `messages` | The editor's words in your language: one object for the whole editor, its assistant and its controls. A key you leave out stays English. See [The Embed Editor](https://docs.widgetic.com/docs/partners/ui-components#the-embed-editor) and [Show the pieces in the language of your customers](https://docs.widgetic.com/docs/partners/ui-components#show-the-pieces-in-the-language-of-your-customers). |
| `theme`, `apiUrl` | As for every piece. See [Make the pieces look like your product](https://docs.widgetic.com/docs/partners/ui-components#make-the-pieces-look-like-your-product). |

## Events

| Event | Payload | |
| - | - | - |
| `ready` | `{ embedCode }` | The options have loaded and the editor is on screen. `embedCode` is the code they make. |
| `dirty` | `{ dirty }` | There is, or is no longer, work that is not saved. |
| `saved` | `{ settings, embedCode, savedAt }` | A save went through. `settings` are the options as they were sent, so a number that a field gave may be text (`"481"`) where the API keeps the number. |
| `error` | `{ message, code?, fatal, during?, status?, retryable?, sessionEnded?, correlationId? }` | See [When something goes wrong](#when-something-goes-wrong). |

## The code

`ready` and `saved` carry `embedCode`, the code the options make, and `embed.module.getEmbedCode()` reads it at any moment, unsaved changes included, for a page that shows the code somewhere else too. It is `null` before the options are on screen. The code names the Widgetic embed script and the frame; `snippetBases` says where they are.

## Saving

* **The editor saves by itself** shortly after the last change, and when a tab that holds unsaved changes is closed or hidden. `embed.module.save()` saves what is unsaved now, `embed.module.flush()` saves everything unsaved and waits until it is (both resolve `true` when nothing is left unsaved), `embed.module.isDirty()` tells whether anything is unsaved, and `embed.module.reload()` reads the options again, which drops unsaved changes.
* **A save that fails** because of the network or the server is tried once more by itself a moment later, and every failed attempt is an `error`. A save the API refuses for good, because an option is not one it accepts, is not tried again: what failed stays unsaved, and the editor says so.
* **`unmount()` saves first**, for up to 10 seconds, so closing the editor never loses the last changes. If some could not be saved you hear an `error` with the code `UNSAVED_ON_UNMOUNT`. `embed.module.unmount({ discard: true })` throws unsaved changes away instead.
* **A customer's options are their own.** The options a customer saves stay on their composition. They never become the starting options of the widget for anyone else, so no other customer's composition begins from them.

## The assistant

The assistant is a second column beside the options. The customer writes what they want ("show it when they are about to leave, once a day"), and it sets the options, asks a question when it needs to, and shows every change it makes in the options before they are saved. It answers in the language the customer writes in.

```javascript theme={"theme":"css-variables"}
const embed = await mountModule('embed-editor', {
  container: '#embed',
  compositionId: 'cmp_123',
  getToken,
  assistant: false, // the options and the code only: no assistant column, and no call to the assistant
});
```

* **It uses no AI credits.** Nothing is charged to your account or to the customer for its answers. Other AI inside an editor session, such as the chat of the [Widget Creator](https://docs.widgetic.com/docs/partners/ui-components/widget-creator.md#the-agentic-chat), still spends your account's credits.
* **It needs nothing more than the options do.** It works with the same session token, so the session made with the `editor` surface is enough. If Widgetic has switched the assistant off for every caller, the assistant cannot answer while the options go on working.
* **A customer can send up to 30 messages a minute**, counted for each customer, so one busy customer does not slow another down. A message over the limit is refused with `429` and the code `RATE_LIMIT_USER_EXCEEDED`, and the assistant tells the customer to wait.
* **What it writes is shown as text.** Markup, a script, a picture or a `javascript:` link in an answer is shown as words and never runs or loads, and a plain `https` address becomes a link that opens in a new tab.

## Next to the Composition Editor

The Composition Editor saves the size of its preview in the same `embedSettings` that this editor saves everything else in, and each sends back what it loaded with every save. Without a word from your page, whichever saves second puts the other's options back. So tell each about the other's saves:

```javascript theme={"theme":"css-variables"}
embed.on('saved', ({ settings }) => editor.module.adoptEmbedSettings(settings));
editor.on('saved', ({ composition }) => embed.module.adoptEmbedSettings(composition.embedSettings));
```

Nothing becomes unsaved by it, and what the customer changed here and has not saved yet stays as it is.

## When something goes wrong

* **Problems** are shown to the customer by the editor itself, with *Try again* where that helps. `error` is for your page, and adds `during` (`'load'`, `'save'`, `'token'` or `'unmount'`), `status`, `retryable`, `sessionEnded` and `correlationId` to the usual fields. `fatal: true` means the editor cannot go on: the composition cannot be opened, or the session is over (`sessionEnded: true`). Make a new session or take the editor away. Quote `correlationId` to Widgetic's support.
* **Another customer's composition** is answered with `404`, and the editor says it cannot be opened.

## Next Steps

> [card] [Editor Sessions](https://docs.widgetic.com/docs/partners/editor-sessions.md)
>
> Make the token on your server

> [card] [UI Components](https://docs.widgetic.com/docs/partners/ui-components)
>
> The SDK, tokens, the look and the language of every piece

> [card] [Managed Users](https://docs.widgetic.com/docs/partners/managed-users.md)
>
> File your customers' compositions under them

> [card] [Embed Settings](https://docs.widgetic.com/docs/api/embed-settings.md)
>
> What each option is called and what it does


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.