# Laravel

> Embed the Muzekit editor in Laravel: a web route uses the Http client to open the session, and a Blade view mounts the editor.

<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 Laravel app." It writes these same files.</Callout>

## How it works in Laravel

The route sits in the `web` group with the `auth` middleware, so `auth()->user()` works with Breeze, Jetstream or Fortify. Laravel checks the CSRF token on every POST in `web`, so the view reads it from the `csrf-token` meta tag and sends it as `X-CSRF-TOKEN`. `Http::withToken` adds the bearer header for you.

## 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 for the signed-in user, in `routes/web.php`:

```php tab="routes/web.php"
use Illuminate\Support\Facades\Http;

Route::post('/api/muzekit-session', function () {
    $user = auth()->user(); // Your signed-in user: replace with your own auth
    abort_unless($user, 401);
    $response = Http::withToken(env('MUZEKIT_SECRET_KEY'))->post('https://api.muzekit.com/v1/sessions', [
        'user' => ['id' => (string) $user->id, 'name' => $user->name, 'email' => $user->email],
        'mode' => 'design',
    ]);
    abort_unless($response->successful(), $response->status(), $response->json('error.message'));
    return ['client_secret' => $response->json('client_secret')];
})->middleware('auth');
```

## 3. Mount the editor

The editor (the layout needs `<meta name="csrf-token" content="{{ csrf_token() }}">`), in `resources/views/editor.blade.php`:

```blade tab="resources/views/editor.blade.php"
<div id="muzekit-editor" style="height: 720px"></div>
<script src="https://muzekit.com/sdk/v1.js"></script>
<script>
  const editor = Muzekit.mount("#muzekit-editor", {
    fetchClientSecret: () =>
      fetch("/api/muzekit-session", { method: "POST", headers: { "X-CSRF-TOKEN": document.querySelector('meta[name="csrf-token"]').content } })
        .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),
    },
  });
</script>
```

## 4. Run it

```bash
php artisan serve
```

Sign in, then open `http://127.0.0.1:8000/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

### 419 Page Expired

The CSRF token is missing: the layout needs the `csrf-token` meta tag.

### Livewire re-renders over the editor

Add `wire:ignore` to the editor's container so Livewire leaves the iframe alone.

### Inertia with Vue or React

Keep the route, and mount the editor from the component in the Vue or React example instead of a Blade view.

## Notes

- Read the key through config/services.php in production (config:cache ignores env() outside config files).
- 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

On Forge or Vapor, set `MUZEKIT_SECRET_KEY` in the environment editor and read it through `config/services.php`, then run `php artisan config:cache`.

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

### Should this be a controller?

Move it to one when it grows: `php artisan make:controller MuzekitSessionController --invokable` gives you the file, and the body stays the same.

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