# Sessions

> How your server opens the editor for one of your users, and how the editor proves where it is loaded.

A session is the only way into the editor. Your server creates it with a secret key, so your users never hold a key and never see a Muzekit login.

## The flow

<Sequence
  actors={[
    { key: "server", label: "Your server", sub: "holds the secret key" },
    { key: "api", label: "Muzekit API", sub: "api.muzekit.com" },
    { key: "page", label: "Your page", sub: "the SDK and the editor" },
  ]}
  steps={[
    { from: "server", to: "api", label: "`POST /v1/sessions`", detail: "the user, the mode, a template" },
    { from: "api", to: "server", label: "`client_secret`", detail: "single use, 10 minutes" },
    { from: "server", to: "page", label: "hands the secret to the page" },
    { from: "page", to: "api", label: "`Muzekit.mount` exchanges it", detail: "checked against your allowed domains" },
    { from: "api", to: "page", label: "editor opens for that user", detail: "token refreshes while it's open" },
  ]}
  caption="Your users never hold a key or see a Muzekit login."
/>

1. Your server calls `POST /v1/sessions` with the user, the [mode](/docs/editor-modes) and, optionally, a template to open.
2. Muzekit returns a `client_secret`: single use, valid for 10 minutes.
3. Your page mounts the editor with that secret. The editor exchanges it for a session token that lasts 15 minutes and refreshes while the editor is open, up to 12 hours.
4. The exchange checks the page that embeds the editor against your [allowed domains](/docs/allowed-domains). Any other page is refused.

<Callout type="warning" title="Never send a secret key to a browser">
Create sessions on your server. If a key leaks, revoke it in the dashboard: it stops working at once.
</Callout>

## Create a session

```bash
curl https://api.muzekit.com/v1/sessions \
  -H "Authorization: Bearer $MUZEKIT_SECRET_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5b0f6a2e-…" \
  -d '{
    "user": { "id": "user_42", "name": "Ada", "email": "ada@example.com" },
    "mode": "fill",
    "template": "tpl_vQ8VEOnjY5H"
  }'
```

Full reference: [Create a session](/docs/api-reference/sessions-create).

## Users are upserted

`user.id` is your own id for the person. The first session creates the Muzekit user; later sessions with the same id update their name, email and locale and reuse their templates and assets.
