> ## 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.

# Widget Creator

> Let your customers make widgets with Widgetic's canvas and its agentic chat, inside your page

The **Widget Creator** is the whole Widgetic canvas, with its **agentic chat** beside it, in your page (see it in [the gallery](https://docs.widgetic.com/docs/partners/ui-components#the-pieces)). Your customer draws frames and describes the widget they want in the chat, and the chat's AI agent does the work: it makes a plan, asks what it needs to know before it starts, builds the widget, checks it and repairs what fails, and suggests what to improve next. The widget appears on the canvas. The customer keeps asking for changes, changes its settings by hand and publishes it.

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 surfaces `["creator", "editor", "files"]`. 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 Widget Creator.

## Mount it

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

const creator = await mountModule('widget-creator', {
  container: '#creator', // give it a height: it fills its container
  canvasId: savedCanvasIdOfThisCustomer, // optional: the one `canvasChanged` gave you last time
  getToken: async () => (await fetch('/widgetic-session', { method: 'POST' })).json(), // { token, expiresAt }
  theme: 'light',
  on: {
    ready: ({ canvasId }) => {},
    canvasChanged: ({ canvasId }) => rememberForNextVisit(canvasId),
    widgetRenamed: ({ widgetId, name }) => setTitleOfTheCustomersWork(name),
    widgetPublished: ({ widgetId, version }) => showInMyProduct(widgetId),
    limitReached: ({ message, code }) => offerMore(code),
    error: ({ message, code, fatal, sessionEnded }) => report(code, message),
  },
});

// later:
await creator.unmount();
```

* **One Widget Creator per page.** Its state (the signed-in customer, the chat, the file lists) belongs to the page, so a second one is refused with a clear message. Unmount the first before you mount another.
* **It is the largest piece.** It is downloaded when you mount it and not before, and a customer who previews a PDF downloads two more files then. To have it ready sooner, call `preload('widget-creator')` on a `WidgeticUI` when the page that will host it opens (see [Several pieces on one page](https://docs.widgetic.com/docs/partners/ui-components#several-pieces-on-one-page)). The size of each version is in its entry of the changelog (see [Versions & Changelog](https://docs.widgetic.com/docs/partners/module-versions.md)).

## The agentic chat

The chat beside the canvas is an agent. It does not answer once and stop: it works on the customer's widget until the widget is built, and keeps working on it as the customer asks for more. It is part of the Widget Creator, so there is nothing to mount or style on its own. It needs the same session, and the AI it uses is paid for by your account's credits (see [When a limit is reached](#when-a-limit-is-reached)).

* **It makes a plan.** Before it writes anything, the chat shows what it is going to do, as a plan whose steps are ticked off while it works.
* **It asks when it needs to know.** When a request leaves out something it cannot guess, the chat asks a few short questions on one card. Nothing is built until the customer has answered, or has chosen to skip them.
* **It builds, checks and repairs.** It writes the widget, builds it and, when the build fails, repairs it, so what reaches the canvas is a widget that runs.
* **It suggests what comes next.** Once the widget exists the chat offers ideas for improving it, and a click puts one in the message box.
* **It sees what the customer shows it.** A customer can attach pictures and documents to a message (up to 10 MB each: a bigger file is refused before anything is sent), and point at a part of the live preview and write a note about it. Attachments are uploaded in the customer's session, into your own storage (see [Every session needs your own storage](https://docs.widgetic.com/docs/partners/editor-sessions.md#every-session-needs-your-own-storage)), or by your own `upload` function when you give one (see `mediaLibrary` below).
* **It works on one widget at a time.** Each message after the first changes the widget on the canvas, and the customer can put the widget back as it was after an earlier message.
* **It can be stopped.** *Stop* cancels the generation on the server: the request to the model is aborted and nothing is built or saved from it. A generation stopped before the model began to write gives its credit back. Once the answer has begun to stream the credit is spent, because a generation costs one whole credit and there is no part of it to return.
* **A second tab is told.** Another tab of the same browser is told that a generation is running on the widget, follows it until it ends and then brings the widget up to date, so the customer is not left generating the same widget twice. A customer who opens the same widget in another browser or on another device is not told.
* **Your page can set three things:** the words (and pictures) of the first message of a new widget, `initialMessage`; whether a limit is told in the chat alone, `limitNotices`; and whether the chat's assistant is Widgetic's animated mascot, `mascot`. See [Options](#options).

## Options

| Option | |
| - | - |
| `getToken` | **Required.** Returns the editor session's token, as for every piece. It is called once before the creator appears (a `getToken` that fails fails the mount), and again when the token is about to run out or the API refuses it. |
| `canvasId` | The canvas to open, as `canvasChanged` gave it to you. It has to be this customer's. See [Where the creator starts](#where-the-creator-starts). |
| `widgetId` | The widget of that canvas to open (needs `canvasId`). |
| `startWithNewWidget` | Start from an empty canvas with a new widget instead of the latest canvas. |
| `initialMessage` | Words for the chat of a new widget, and the pictures that go with them: `{ text: 'Replicate this design: https://…', send: true }`, or `{ text: 'Replicate this photo', send: true, attachments: [file] }`. With `send: true` the creator sends it as the first message when the chat is ready, exactly as if the customer had typed it and pressed Enter, and the AI agent starts building. Without `send` the words and the pictures wait in the chat's message box. `text` is at most 4000 characters, and `attachments` are at most 5 files of your page's own (a photo the customer chose on your page, not a link), each a PNG, JPEG, WebP or GIF of at most 10 MB. Used once, for a new widget (`startWithNewWidget: true`): a widget that already has a draft keeps its own. |
| `limitNotices` | `'all'` (the default) or `'chat'`. When a limit refuses the customer, `'all'` tells them in a toast and in the chat, and `'chat'` in the chat alone, for a page that draws its own notice (see [When a limit is reached](#when-a-limit-is-reached)). |
| `offerPlanUpgrade` | `true` (the default) or `false`. When what failed in the chat is about credits or the plan, the chat draws an **Upgrade** button under it. `false` leaves the button out, for a page whose plan already includes making a widget. See [The Upgrade button](#the-upgrade-button). |
| `mascot` | `false` draws the chat's assistant as a plain round avatar instead of Widgetic's animated mascot (a scribble that thinks, writes and ends in its success or error look), and keeps the chat silent: the mascot's small sounds go with it. Default `true`. Nothing else about the chat changes. |
| `locale`, `messages` | The creator's language: the plural forms, numbers and dates, the built-in words of 65 languages, and the words you want to say differently as **one** object of message keys and texts for the whole creator (its own words, the chat's and the canvas's). A key you leave out keeps the built-in words of the `locale` (English when it has none). `creator.setLanguage({ locale?, messages? })` changes them while it is on screen. See [The Widget Creator](https://docs.widgetic.com/docs/partners/ui-components#the-widget-creator). |
| `mediaLibrary` | Your own media library for the file pickers of the settings panel. See [Show your own media library](https://docs.widgetic.com/docs/partners/ui-components/file-browser.md#show-your-own-media-library). When you also give its `upload` function, the chat's attachments and the pictures dropped on the canvas call it too, once per file (`purpose` is `'chat'` or `'canvas'`; the pickers pass `'field'`). Without one they go through Widgetic's upload call into your own storage, which every session requires (see [Every session needs your own storage](https://docs.widgetic.com/docs/partners/editor-sessions.md#every-session-needs-your-own-storage)). |
| `pickFile` | Your own picker for the file fields of the settings panel. See [Your own file picker for the editors](https://docs.widgetic.com/docs/partners/ui-components/file-browser.md#your-own-file-picker-for-the-editors). |
| `showCanvasFileBrowser` | `true` adds a **Files** entry to the canvas's Shapes menu (the "More shapes" button), right before **Image**. It opens the File Browser over your `mediaLibrary` (or the customer's Widgetic uploads when you gave none), and the pictures and SVG files the customer ticks go on the sketch. A `pickFile` of yours is asked for them instead. Default `false`. |
| `showSketchBar`, `showSaveButton`, `showZoomControls`, `toolbarPosition` | For a page that draws its own chrome around the creator. See [Your own header around the creator](#your-own-header-around-the-creator). |
| `widgetBuilderUrl` | The origin the live previews of widgets are served from. Default: Widgetic's. |
| `theme` | As for every piece: a mode, or your colours, corner radius and font. The creator follows the mode, your neutral colours, your brand colour and the font; its corners are not themed yet (see [What a theme cannot do yet](https://docs.widgetic.com/docs/partners/ui-components#what-a-theme-cannot-do-yet)). See [Make the pieces look like your product](https://docs.widgetic.com/docs/partners/ui-components#make-the-pieces-look-like-your-product). |
| `apiUrl` | As for every piece. |

## Events and methods

| Event | Payload | |
| - | - | - |
| `ready` | `{ canvasId }` | The canvas is on screen. `canvasId` is `null` for a new, empty one. |
| `canvasChanged` | `{ canvasId }` | The creator opened a canvas or made one. Keep the id and give it back as `canvasId` next time. |
| `widgetRenamed` | `{ widgetId, name }` | The main widget has this name. Said when the canvas opens and each time the customer renames it, never twice in a row for the same name, so you can keep a title for the customer's work. |
| `widgetPublished` | `{ widgetId, version? }` | The customer published a version of a widget. |
| `publishStateChanged` | `{ widgetId, canPublish, publishing, label, unpublished }` | What Publish would do now, for a page that draws its own Publish button. See [Your own header around the creator](#your-own-header-around-the-creator). |
| `limitReached` | `{ message, code? }` | The customer ran into a limit, or pressed **Upgrade**. See [When a limit is reached](#when-a-limit-is-reached). |
| `reloadRequested` | | The creator would have reloaded the page to recover. Unmount it and mount a new one. |
| `error` | `{ message, code?, fatal, during?, sessionEnded? }` | See [When something goes wrong](#when-something-goes-wrong). |

Methods on `creator.module`: `getCanvasId()` (the canvas that is open now, or `null`). `creator.setTheme(theme)` changes its look (a mode, or settings: see [Make the pieces look like your product](https://docs.widgetic.com/docs/partners/ui-components#make-the-pieces-look-like-your-product)). `creator.setLanguage({ locale?, messages? })` changes its language, and `creator.module.defaultMessages` and `creator.module.checkMessages(messages)` list its messages and check yours (see [The Widget Creator](https://docs.widgetic.com/docs/partners/ui-components#the-widget-creator)). `creator.unmount()` takes the creator away. The methods of [your own header](#your-own-header-around-the-creator) are `getPublishState()`, `publish()`, `flush()` and `setWidgetName(name, widgetId?)`.

## Where the creator starts

The Widget Creator is independent from your presets: it never puts a screen of its own in front of the canvas, and it never lists your presets, even when you have some. It opens on:

1. the canvas you give it, as `canvasId` (and, with `widgetId`, on one of its widgets);
2. otherwise the customer's latest canvas;
3. otherwise, for a customer who has made nothing yet, a blank canvas to make a new widget on. `startWithNewWidget: true` asks for one even when the customer has work.

The creator writes nothing to your `localStorage`, `sessionStorage`, cookies or address bar, and never opens a window or reloads your page. What it remembers lives in memory until `unmount()`. So keep the id from `canvasChanged` with your customer's record and give it back as `canvasId` the next time, and the customer finds their work where they left it.

### Start a customer from one of your presets

When you want your customers to begin from something ready-made, offer your [presets](https://docs.widgetic.com/docs/partners/managed-users.md#preset-widgets) on a page of your own, as Widgetic does with its templates: a list of cards, in your order, and the customer picks one. Your server makes the customer's copy, and the creator opens on it:

1. **List them.** Read your presets from your server (`GET /partners/{partnerId}/presets`, `listPartnerPresets` in the SDKs) and draw them as you like: a thumbnail, a title and a line each.
2. **Copy the one the customer picked.** Your server calls `POST /managed-users/{id}/presets/{key}/copy` (`copyPresetForManagedUser`) with an `Idempotency-Key`. The answer has the `canvasId` and the `widgetId` of the customer's own copy.
3. **Open the creator on it.** Mount it with that `canvasId` and `widgetId`, and keep the id `canvasChanged` gives you for next time.

```javascript theme={"theme":"css-variables"}
// Your page: the customer picked "rating" on your own page of starting points.
// Your server makes the copy (see Preset Widgets) and answers with the copy's `canvasId` and `widgetId`.
const { canvasId, widgetId } = await (await fetch('/start-from-preset', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ key: 'rating' }),
})).json();

const creator = await mountModule('widget-creator', { container: '#creator', canvasId, widgetId, getToken });
```

The customer changes the copy, never your preset. How to register, list and copy presets, what a copy carries and the errors it answers with are in [Preset Widgets](https://docs.widgetic.com/docs/partners/managed-users.md#preset-widgets).

## Your own header around the creator

A page that draws its own bar above the creator (a Back arrow, the name of the widget, a Publish button) can put the creator's own controls aside with `showSketchBar: false` (the bar with the title, Live and Publish), `showSaveButton: false` (the Save button and the dot that says the board has changes the server does not have: the creator saves by itself either way), `showZoomControls: false` (the zoom buttons: the wheel, a pinch and the keyboard still zoom) and `toolbarPosition` (`'BL'`, `'BC'`, the default, or `'BR'`: where the drawing toolbar stands along the bottom edge, for a page that puts something of its own over the middle of it). Then it takes what it needs from the creator:

* **`publishStateChanged`** `{ widgetId, canPublish, publishing, label, unpublished }` says what Publish would do now, for the widget the customer is on. It is said when the creator first knows and at every change, never twice with the same state. `label` is the words of the creator's own button, in your page's language. `creator.module.getPublishState()` gives the last one to a page that draws its button after the creator reported.
* **`creator.module.publish()`** does what the creator's own Publish button does and resolves when it is done, after `widgetPublished`. It rejects, and does nothing, when the creator is not on screen or cannot publish now, and the creator shows the customer the reason, as when its own button is pressed.
* **`creator.module.flush()`** puts what the customer changed on the canvas on the server now and resolves when that is done. Wait for it before your page takes the customer away from the creator.
* **`creator.module.setWidgetName(name, widgetId?)`** renames the widget the customer is on (or the one you name), shows the new name at once and saves it, and the creator says `widgetRenamed`.

## What the creator makes, and how you find it

Everything the creator makes is filed under the session's managed user, and the session can open only that customer's canvases, widgets and compositions. Another customer's canvas answers `404`, exactly as if it did not exist.

* **Find a customer's work** from your server with the filter you already know. Your key sees all of it:
  ```bash theme={"theme":"css-variables"}
  curl -sS -H "Authorization: Bearer $WIDGETIC_PARTNER_KEY" \
    "https://api.widgetic.com/v1/widgets?managedUserExternalId=your-platform-user-123"
  ```
  `GET /canvases` and `GET /compositions` take the same `managedUserId` and `managedUserExternalId` filters.
* **Show the result in your product** the way you show any widget: a [composition](https://docs.widgetic.com/docs/api/compositions.md) of the widget, and the embed code. The creator makes compositions of the widgets it builds to show them with sample values; you can list them with the filter above or make your own on the same widget with your key.
* **Private to your account.** A widget a customer builds is never made public in Widgetic's catalogue by a session. Publishing a version saves it and the customer can go back to an earlier one.
* **Your key reaches everything**, filed under a customer or not. Deciding which of your customers may see which widget stays in your own platform.

## When a limit is reached

The AI is paid for by your account (see [Credits & Billing](https://docs.widgetic.com/docs/partners/credits-billing.md)), and your credits and your plan are yours: **a customer is never told what they hold.** When one is used up, the API refuses the session with one plain sentence, "This is not available right now. Please try again later.", and the creator shows exactly that, in the chat (with an **Upgrade** button under the failed message: see [The Upgrade button](#the-upgrade-button)) and in a toast, and keeps working: the canvas, the chat and the other widgets are still there.

It also tells **your page**, with `limitReached` `{ message: 'The visitor reached a limit.', code }`. The `code` is what the API answered:

| `code` | What happened |
| - | - |
| `BILLING_INSUFFICIENT_CREDITS` | Your account is out of credits. Every customer is refused until you add some. |
| `AUTHZ_PLAN_RESTRICTION` | Your plan does not include what the customer asked for. |
| `COST_LIMIT_EXCEEDED` | This customer used their allowance for the hour or the day. It comes back by itself, and it is this customer's alone. |
| `MANAGED_USER_MONTHLY_CREDITS_EXHAUSTED` | This customer spent the monthly allowance you set for them (`PUT /managed-users/{id}/limits`; the month is the calendar month in UTC). It comes back when the month turns, or when you raise the allowance, and it is this customer's alone. Your webhook is told too: [`credits.managed_user_cap_reached`](https://docs.widgetic.com/api-reference/events/credits-managed-user-cap-reached). |
| `EDITOR_SESSION_CREDIT_LIMIT_REACHED` | This session spent the `maxCredits` you gave it when you made it. It is this session's alone: make a new one if the customer may spend more. See [A limit for one session](https://docs.widgetic.com/docs/partners/editor-sessions.md#a-limit-for-one-session). |
| `LIMIT_REACHED` | The customer pressed **Upgrade** in the chat. The creator opens nothing for it: what an upgrade means in your product is for you to show (see [The Upgrade button](#the-upgrade-button)). |

Your page hears each `code` once in half a minute, however many requests are refused meanwhile. Ordinary rate limiting (`RATE_LIMIT_EXCEEDED`) is **not** a limit: the creator asks the customer to wait a moment and tells your page nothing.

The allowance of the hour or the day is counted for each customer on their own, so one customer reaching it refuses and tells nobody else. Credits and plan are your account's, so when they run out every customer is refused.

A customer's upload that cannot be kept, because you have set up no storage of your own (`409` `PARTNER_STORAGE_REQUIRED`), is not a limit: your page hears no `limitReached` for it, and the customer reads the same plain sentence. You find out when you make the session, which is refused for the same reason. See [Every session needs your own storage](https://docs.widgetic.com/docs/partners/editor-sessions.md#every-session-needs-your-own-storage).

A paused, stopped or not yet activated partner account, or a partnership that has ended, is not a limit either: the customer reads one plain sentence, "This service is not available right now. Please contact the website that sent you here.", and your page hears no `limitReached` for it. See [Account Status](https://docs.widgetic.com/docs/partners/account-status.md) and [Ending a Partnership](https://docs.widgetic.com/docs/partners/ending-a-partnership.md).

### The Upgrade button

When what failed is about credits or the plan, the chat draws an **Upgrade** button under the failed message, and the notice that says a publish is not in the plan has one too. Your customer has no plan of their own to upgrade, so the creator opens no page and no window for it. A press tells **your page**, with `limitReached` and `code: 'LIMIT_REACHED'`, and your page decides what it means: your own plans or top-up page, a dialog, a message to the owner of the account.

```javascript theme={"theme":"css-variables"}
creator.on('limitReached', ({ code }) => {
  if (code === 'LIMIT_REACHED') openMyPlansPage(); // the customer pressed Upgrade
});
```

A page whose plan already includes making a widget passes `offerPlanUpgrade: false`. The chat then draws no **Upgrade** button (a failure about credits or the plan gets the same **Retry** as any other failure), and nothing reports `LIMIT_REACHED`. The other codes in the table above are told to your page as they happen, whatever this option says.

## When something goes wrong

The creator shows a problem to the customer itself. The `error` event is for your page. `fatal: true` means the creator cannot go on, and it has said so on its own screen:

* **The session is over** (`sessionEnded: true`, for example `EDITOR_SESSION_EXPIRED` or `EDITOR_SESSION_REVOKED`). Mount again, with a new token from `getToken`.
* **The `canvasId` you gave is not this customer's** (`during: 'canvas'`, code `NOT_FOUND`). Mount again without it.

Either way no `ready` comes, and the creator stops asking the API. With `fatal: false` the creator carries on (for example after a failed token refresh that will be retried).

## What it leaves alone

* **The keyboard.** The canvas's shortcuts (Delete, the arrows, `v`, `r`, `h`, Ctrl+A and so on) work only while the focus is inside the creator, so a customer who clicked on your page keeps every key of it. Your page's own zoom (Ctrl or Cmd with `+`, `-`, `0`, Ctrl with the wheel) works everywhere. A customer gives the creator the keyboard by clicking on it or tabbing into it.
* **Your page's scroll.** The chat follows its newest message inside its own box and never scrolls your page.
* **Your `<head>` and `<body>`.** The creator adds one `<style>` with font rules to `<head>` (named `Widgetic UI …`, shared with the other pieces and never added twice, so it stays when the creator is unmounted) and draws everything else inside its shadow root.
* **A widget's compositions.** The creator's *Edit* step (a widget's compositions) and its *Embed* step are not part of a session, and a page cannot switch them on. A customer edits a composition with the [Composition Editor](https://docs.widgetic.com/docs/partners/ui-components/composition-editor.md), and your page shows the widget with the embed code.

## Next Steps

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

> [card] [Preset Widgets](https://docs.widgetic.com/docs/partners/managed-users.md#preset-widgets)
>
> Offer your customers a ready-made start

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

> [card] [Compositions](https://docs.widgetic.com/docs/api/compositions.md)
>
> Show a widget in your product with the embed code


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