The SDK

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

View as Markdown

One script mounts the editor in any page. It needs a session from your server and nothing else.

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

OptionWhat it does
fetchClientSecretReturns your server's client_secret (a string). Called each time the editor opens.
clientSecretA secret you already have. It works once, so pass fetchClientSecret if the editor may reopen.
height, minHeightThe iframe's height. Defaults to the element's height, at least 560px.
titleThe iframe's accessible title. Defaults to "Design editor".
editorUrlThe editor's address when it runs on your own domain (a custom editor domain). Defaults to Muzekit's.
onEvent handlers, below. Add more later with editor.on(type, fn).
featuresSwitches to turn off on this page, e.g. { stock: false, developer: false }. Keys as in Features. Only narrows: true can't turn on what the session has off.
formatsDownload formats on this page, from the session's: ["png", "pdf"].
videoFormatsVideo download formats on this page, from the session's: ["mp4"].
templatePickerThe templates picker: "panel" or "page", or { layout, open, tab } as in Editor modes.
assistantConnectfalse to ask your users yourself when they connect an AI app, instead of the editor's prompt. See your users' AI apps. Defaults to true.
JavaScript
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, which your server creates.

Events

EventWhenDetail
readyThe editor is open and the design loaded{ mode, template }
saveA save finished (autosave or editor.save()){ template, revision, created }
downloadThe user downloaded the design{ format }: png, jpg, webp, pdf…
actionThe user clicked one of your buttons{ button, template, page, format?, mimeType?, base64? }
closeThe user closed the editor from inside it; hide or remove it on your pagenone
errorSomething stopped the editor{ code, message, docs_url }
assistantConnectThe 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

MethodWhat 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.iframeThe editor's <iframe> element, to size or focus it.
Muzekit.versionThe 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. 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. 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.