# Vue

> Embed the Muzekit editor in a Vue 3 app: a single-file component mounts it in onMounted, and your backend opens the session.

<Callout title="Let your coding agent do it">Connect Claude Code, Cursor or Codex to the [MCP server](/docs/mcp#set-up-the-editor-in-your-app) and ask: "Set up the Muzekit editor in this Vue app." It writes these same files.</Callout>

## How it works in Vue

The component uses a template ref for the container and keeps the editor instance in a plain variable, not in `ref()` or `reactive()`, so Vue never wraps it in a proxy. The backend route (Express here, or any server) checks the signed-in user and opens the session. `VITE_` variables are bundled for the browser, so the key stays on the backend.

## 1. Add your secret key

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

```bash tab=".env"
MUZEKIT_SECRET_KEY=mk_test_…
```

<Screenshot src="/docs/1-embed-code.webp" original="/docs/raw/1-embed-code.png" alt="The Embed code page in the Muzekit dashboard: the Test key for the server, then the session route and mount code" />

## 2. Open a session on your server

Opens a session at POST /api/muzekit-session, in `server: your backend`:

```js tab="server: your backend"
// Add to your Express app (Node 18+). Needs express.json() only if you send a body.
app.post("/api/muzekit-session", async (req, res) => {
  const user = req.user; // Your signed-in user: replace with your own auth
  if (!user) return res.status(401).json({ error: "Sign in first" });
  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})`);
  res.json({ client_secret: session.client_secret });
});
```

## 3. Mount the editor

The editor, in `src/components/MuzekitEditor.vue`:

```vue tab="src/components/MuzekitEditor.vue"
<script setup>
import { onBeforeUnmount, onMounted, ref } from "vue";

const el = ref(null);
let editor;

function loadSdk() {
  if (window.Muzekit) return Promise.resolve();
  return new Promise((resolve, reject) => {
    const s = document.createElement("script");
    s.src = "https://muzekit.com/sdk/v1.js";
    s.onload = resolve;
    s.onerror = reject;
    document.head.appendChild(s);
  });
}

onMounted(async () => {
  await loadSdk();
  editor = window.Muzekit.mount(el.value, {
      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),
      },
    });
});
onBeforeUnmount(() => editor?.destroy());
</script>

<template>
  <div ref="el" style="height: 720px" />
</template>
```

## 4. Run it

```bash
npm run dev (front end and backend)
```

Sign in, then open `http://localhost:5173`. 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

### Your session cookie doesn't reach the backend

Proxy `/api` with `server.proxy` in `vite.config.js` during development, so both run on one origin.

### The same editor shows after you navigate back

Inside `KeepAlive`, the component and its editor are cached. Without it, the editor is destroyed and opens with a fresh session.

### Options API

Use the same code in `mounted()` and `beforeUnmount()`, and keep the editor on `this`, not in `data()`.

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

Build with `npm run build` and serve `dist` from any static host. Run the backend with `MUZEKIT_SECRET_KEY` set; behind one reverse proxy on one domain, no CORS setup is needed.

Then make a Live key (`mk_live_…`), add your domain to Live under [Allowed domains](/docs/allowed-domains) and use **Copy to Live** for your templates, look and features.

## Questions

### Is there a Vue plugin?

No plugin needed: the SDK is plain JavaScript, and this component is the whole integration.

<Cards>
  <Card title="Editor modes" description="Design, fill-in and view, and what each shows." href="/docs/editor-modes" />
  <Card title="Browser SDK" description="Events, saving from your own button, and destroy." href="/docs/embed-sdk" />
</Cards>
