SvelteKit

Embed the Muzekit editor in SvelteKit: a +server.js endpoint opens the session with a private env variable, and onMount mounts the editor.

View as Markdown
Let your coding agent do it
Connect Claude Code, Cursor or Codex to the MCP server and ask: "Set up the Muzekit editor in this SvelteKit app." It writes these same files.

How it works in SvelteKit

SvelteKit refuses to build if browser code imports $env/dynamic/private, so the key can't leak by mistake. The signed-in user comes from locals, which your hooks.server.js fills from Lucia, Auth.js for SvelteKit or Supabase. onMount returns a cleanup that destroys the editor.

1. Add your secret key

Create an editor and copy its Test key (mk_test_…) from API keys in the dashboard. Put it in .env on your server:

.env
MUZEKIT_SECRET_KEY=mk_test_…

2. Open a session on your server

Opens a session for the signed-in user, in src/routes/api/muzekit-session/+server.js:

src/routes/api/muzekit-session/+server.js
import { json, error } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";

export async function POST({ locals }) {
  const user = locals.user; // Your signed-in user: replace with your own auth
  if (!user) error(401, "Sign in first");
  const response = await fetch("https://api.muzekit.com/v1/sessions", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${env.MUZEKIT_SECRET_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ user: { id: String(user.id), name: user.name, email: user.email }, mode: "design" }),
  });
  const session = await response.json();
  if (!response.ok) throw new Error(`Muzekit: ${session.error?.message} (${session.error?.code})`);
  return json({ client_secret: session.client_secret });
}

3. Mount the editor

The editor, in src/lib/MuzekitEditor.svelte:

src/lib/MuzekitEditor.svelte
<script>
  import { onMount } from "svelte";
  let el;

  onMount(() => {
    let editor;
    const start = () => (editor = window.Muzekit.mount(el, {
      fetchClientSecret: () =>
        fetch("/api/muzekit-session", { method: "POST" })
          .then((r) => r.json())
          .then((s) => s.client_secret),
      on: {
        ready: () => console.log("Muzekit editor ready"),
        save: (e) => console.log("Saved", e.template, e.revision),
        error: (e) => console.error("Muzekit", e.code, e.message),
      },
    }));
    if (window.Muzekit) start();
    else {
      const s = document.createElement("script");
      s.src = "https://muzekit.com/sdk/v1.js";
      s.onload = start;
      document.head.appendChild(s);
    }
    return () => editor?.destroy();
  });
</script>

<div bind:this={el} style="height: 720px"></div>

4. Run it

Shell
npm run dev

Sign in, then open http://localhost:5173/editor. The editor loads in its container and the first session shows on your dashboard's Home. With a Test key, sessions on localhost and 127.0.0.1 are free.

Common problems

locals.user is undefined

Set it in hooks.server.js, inside handle, from your session cookie; the endpoint expects it there.

The editor page is prerendered

It depends on the signed-in user, so don't prerender it: add export const prerender = false if your app prerenders by default.

adapter-static has no server

A fully static build can't run the endpoint. Use adapter-node, adapter-vercel or adapter-netlify, or move the session route to another backend.

Notes

  • Secret key: MUZEKIT_SECRET_KEY goes in .env (or your secret store) on the server only. Never in browser code, never committed.
  • Every user.id is your own id for that person: their designs and uploads follow it across sessions.
  • mode: "design" opens the full editor; "fill" opens a form over a template (send template: "<key or tpl_ id>" too).
  • Give the container a real height: the editor fills it.

Deploy and go live

Set MUZEKIT_SECRET_KEY in your adapter's environment: Vercel or Netlify settings, or the process environment for adapter-node. $env/dynamic/private reads it at runtime, so you can change it without rebuilding.

Then make a Live key (mk_live_…), add your domain to Live under Allowed domains and use Copy to Live for your templates, look and features.

Questions

Why dynamic and not static private env?

$env/static/private writes the value into the build. The dynamic module reads it at runtime, so Test and Live keys can differ per deploy without a rebuild.