React Router and Remix

Embed the Muzekit editor in React Router 7 or Remix: a resource route's action opens the session and the editor mounts from useEffect.

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 React Router and Remix app." It writes these same files.

How it works in React Router and Remix

A resource route exports an action and no component, so POST /api/muzekit-session answers JSON instead of a page. Read the user from your session storage, like getSession(request.headers.get("Cookie")), or from your auth library. Effects never run during server rendering, so the SDK loads in the browser without a .client module.

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 (action at /api/muzekit-session; add it to app/routes.ts if you list routes by hand), in app/routes/api.muzekit-session.js:

app/routes/api.muzekit-session.js
export async function action({ request }) {
  const user = await getCurrentUser(request); // Your signed-in user: replace with your own auth
  if (!user) return Response.json({ error: "Sign in first" }, { status: 401 });
  const response = await fetch("https://api.muzekit.com/v1/sessions", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.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 Response.json({ client_secret: session.client_secret });
}

3. Mount the editor

The editor (mounts in the browser only, from useEffect), in app/components/muzekit-editor.jsx:

app/components/muzekit-editor.jsx
import { useEffect, useRef } from "react";

const SDK = "https://muzekit.com/sdk/v1.js";

function loadSdk() {
  if (window.Muzekit) return Promise.resolve();
  return new Promise((resolve, reject) => {
    const s = document.createElement("script");
    s.src = SDK;
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
}

export default function MuzekitEditor() {
  const ref = useRef(null);
  useEffect(() => {
    let editor;
    let gone = false;
    loadSdk().then(() => {
      if (gone) return;
      editor = window.Muzekit.mount(ref.current, {
      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),
      },
    });
    });
    return () => {
      gone = true;
      editor?.destroy();
    };
  }, []);
  return <div ref={ref} style={{ height: 720 }} />;
}

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

The route answers 404

If you list routes in app/routes.ts, add route("api/muzekit-session", "routes/api.muzekit-session.js"). Flat file routing finds it by name.

Remix v2 imports

Response.json works on Node 18 and later in both; on older setups, import json from @remix-run/node.

Mounting at module level

Never call Muzekit.mount outside the effect: the module also runs on the server, where there is no window.

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 where the server runs: Fly.io secrets, Vercel environment variables or your Node host. The action reads it on the server only.

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

Can I use a clientLoader?

No: a clientLoader runs in the browser, where the secret key must never be. Keep the call in the server action.