Django
Embed the Muzekit editor in Django: a login_required view opens the session with urllib, and the template sends Django's CSRF token.
How it works in Django
request.user is the signed-in user from django.contrib.auth. Django's CSRF middleware checks every POST, so the page reads the csrftoken cookie and sends it back. No package is needed: urllib.request is in the standard library, though requests or httpx work the same.
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:
MUZEKIT_SECRET_KEY=mk_test_…2. Open a session on your server
Opens a session; add path("api/muzekit-session", views.muzekit_session) to urls.py, in yourapp/views.py:
import json, os, urllib.request
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.views.decorators.http import require_POST
@login_required
@require_POST
def muzekit_session(request):
user = request.user # Your signed-in user: replace with your own auth
body = json.dumps({"user": {"id": str(user.pk), "name": user.get_full_name() or user.get_username(), "email": user.email}, "mode": "design"}).encode()
req = urllib.request.Request("https://api.muzekit.com/v1/sessions", data=body, method="POST", headers={
"Authorization": f"Bearer {os.environ['MUZEKIT_SECRET_KEY']}",
"Content-Type": "application/json",
})
with urllib.request.urlopen(req) as res:
session = json.load(res)
return JsonResponse({"client_secret": session["client_secret"]})3. Mount the editor
The editor (render it with {% csrf_token %} once, so the csrftoken cookie is set), in templates/editor.html:
<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-CSRFToken": document.cookie.match(/csrftoken=([^;]+)/)?.[1] ?? "" } })
.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
python manage.py runserverSign 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
403 CSRF verification failed
The csrftoken cookie isn't set yet: decorate the view that renders the editor page with ensure_csrf_cookie.
HTTPError from urllib
Muzekit answered with an error, such as an invalid key. Catch urllib.error.HTTPError and read its body for error.code.
Django REST Framework
In an APIView, the same body works; return a Response with the client_secret.
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 for your WSGI or ASGI server: the Gunicorn systemd unit, Heroku config vars or Fly secrets. Locally, django-environ or python-dotenv can load it.
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 the view be async?
Yes: make it async def and use httpx.AsyncClient, as in the FastAPI example.