# The SDK

> Mount the editor in your page, hear what your users do in it, and handle errors.

One script mounts the editor in any page. It needs a [session](/docs/sessions) from your server and nothing else.

<Screenshot src="/docs/1-editor-design.webp" original="/docs/raw/1-editor-design.png" alt="The dashboard's Preview: the editor mounted with the SDK in design mode on a 7-page plant carousel, as your page will show it, with the events it sends listed below" />

```html
<div id="editor" style="height: 720px"></div>
<script src="https://muzekit.com/sdk/v1.js"></script>
<script>
  const editor = Muzekit.mount("#editor", {
    fetchClientSecret: () =>
      fetch("/api/muzekit-session", { method: "POST" })
        .then((r) => r.json())
        .then((s) => s.client_secret),
    on: {
      ready: (e) => console.log("Editor open", e.mode),
      save: (e) => console.log("Saved", e.template, e.revision),
      download: (e) => console.log("Downloaded a", e.format),
      error: (e) => console.error(e.code, e.message),
    },
  });
</script>
```

## Options

| Option | What it does |
|---|---|
| `fetchClientSecret` | Returns your server's `client_secret` (a string). Called each time the editor opens. |
| `clientSecret` | A secret you already have. It works once, so pass `fetchClientSecret` if the editor may reopen. |
| `height`, `minHeight` | The iframe's height. Defaults to the element's height, at least 560px. |
| `title` | The iframe's accessible title. Defaults to "Design editor". |
| `editorUrl` | The editor's address when it runs on your own domain (a custom editor domain). Defaults to Muzekit's. |
| `on` | Event handlers, below. Add more later with `editor.on(type, fn)`. |
| `features` | Switches to turn off on this page, e.g. `{ stock: false, developer: false }`. Keys as in [Features](/docs/editor-modes#turning-parts-of-the-editor-off). Only narrows: `true` can't turn on what the session has off. |
| `formats` | Download formats on this page, from the session's: `["png", "pdf"]`. |
| `videoFormats` | Video download formats on this page, from the session's: `["mp4"]`. |
| `templatePicker` | The templates picker: `"panel"` or `"page"`, or `{ layout, open, tab }` as in [Editor modes](/docs/editor-modes#starting-from-your-templates). |
| `assistantConnect` | `false` to ask your users yourself when they connect an AI app, instead of the editor's prompt. See [your users' AI apps](/docs/mcp#let-your-users-connect-their-ai-app). Defaults to `true`. |

```js
Muzekit.mount("#editor", {
  fetchClientSecret,
  features: { stock: false },
  formats: ["png"],
  templatePicker: { layout: "page", open: "always", tab: "templates" },
});
```

The options go to the editor with the client secret, never in a URL. Anything your users must not get around belongs on the [session](/docs/api-reference/sessions-create), which your server creates.

## Events

| Event | When | Detail |
|---|---|---|
| `ready` | The editor is open and the design loaded | `{ mode, template }` |
| `save` | A save finished (autosave or `editor.save()`) | `{ template, revision, created }` |
| `download` | The user downloaded the design | `{ format }`: `png`, `jpg`, `webp`, `pdf`… |
| `action` | The user clicked one of [your buttons](/docs/editor-modes#buttons-of-your-own) | `{ button, template, page, format?, mimeType?, base64? }` |
| `close` | The user closed the editor from inside it; hide or remove it on your page | none |
| `error` | Something stopped the editor | `{ code, message, docs_url }` |
| `assistantConnect` | The page opened with `?muzekit_connect=`: a user is connecting an AI app. The editor asks them unless you passed `assistantConnect: false` | `{ request }` |

`created` is `true` on the save that made a new template: a blank design's first save, or the user's own copy of a template you made. Store `template` against your user to reopen it later.

## Methods

| Method | What it does |
|---|---|
| `editor.save()` | Saves now; `save` follows. Useful before your page navigates away. |
| `editor.openTemplates(tab?)` | Opens the templates picker, on `"mine"` or `"templates"` if given. For your own "Browse templates" button. |
| `editor.closeTemplates()` | Closes the picker. |
| `editor.setButtons(changes)` | Relabels or hides your buttons: `{ "send-to-crm": { label: "Sent", visible: false } }`. |
| `editor.destroy()` | Removes the editor and stops its session. |
| `editor.on(type, fn)` | Adds an event handler. |
| `editor.iframe` | The editor's `<iframe>` element, to size or focus it. |
| `Muzekit.version` | The SDK's version. |

## Errors

The SDK reports every problem through `error`, and logs it to the console when your page has no error handler. The codes, with what to do about each, are in [Errors](/docs/errors#in-the-browser). The ones you will meet first:

- `client_secret_unavailable`: your session route failed or returned the whole JSON instead of the `client_secret` string.
- `origin_not_allowed`: the page isn't one of your [allowed domains](/docs/allowed-domains). With a Test key, localhost always works; a Live key never opens on localhost.
- `editor_unreachable`: the iframe never loaded; check your Content-Security-Policy `frame-src`.
