# Embed the release builder

> One script tag and a server-minted session; messages, features and security.

Put activQC's release builder inside your own site with one script tag. Artists fill in the release, the checks run
as they type, they answer plain questions instead of getting rejections, and you get a checked release back by
webhook. Your server never hands a key to the browser.

## Step 1 — Allowlist your origin

Open your tenant console at `/t/<your-slug>/developers/embed` (requires the `developers` capability or Owner role)
and add every origin that will frame the builder, one per line:

```text
https://portal.yourlabel.com
http://localhost:4001
```

Origins must be `scheme://host[:port]` with no paths, query strings, or wildcards (`*`). The engine refuses to mint
embed sessions (`422 embed_not_configured`) until at least one origin is configured.

## Step 2 — Mint a session on your server

Call `POST /v1/embed/sessions` from your **backend** with a live or test **secret API key** (`wqc_live_…` or
`wqc_test_…`). Never call this endpoint from a browser and never pass a console session key (`403 secret_key_required`).

```bash
curl -X POST $ACTIVQC_BASE_URL/v1/embed/sessions \
  -H "Authorization: Bearer $ACTIVQC_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "external_ref": "rel-2026-042",
    "end_user_ref": "artist-8842",
    "locale": "en",
    "allowed_origin": "https://portal.yourlabel.com",
    "theme": {
      "mode": "light",
      "--wb-accent": "#0b5fff"
    }
  }'
```

Response (`201 Created`):

```json
{
  "object": "embed.session",
  "id": "es_7fK2aBcDeFgHiJkLmNoP",
  "client_token": "wqc_es_9f3c...",
  "expires_at": "2026-11-23T04:00:00Z",
  "draft_id": "rd_7fK2aBcDeFgHiJkLmNoPqRsT",
  "url": "$ACTIVQC_BASE_URL/embed/builder?token=wqc_es_9f3c..."
}
```

- Pass `draft_id` to resume an existing draft, `external_ref` to find or create a draft under your own release ID, or
  omit both to start a fresh draft.
- `client_token` lasts **30 minutes** and is **single-use**. Opening the iframe exchanges it once for an encrypted
  `HttpOnly; SameSite=None; Secure; Partitioned` cookie on the engine's origin and redirects to `/embed/builder`
  without the token in the URL. A second use renders a plain "This link has expired" page (`410 Gone`).

## Step 3 — Mount `<wavbee-release-builder>` in your page

Load the plain ES module loader (`/embed/v1/embed.js`, < 6 KB, zero dependencies) and pass the `client_token` from
your server:

```html
<script type="module" src="https://YOUR-ACTIVQC-HOST/embed/v1/embed.js"></script>

<wavbee-release-builder
  client-token="wqc_es_9f3c..."
  locale="en"
  theme="light">
</wavbee-release-builder>

<script type="module">
  const el = document.querySelector('wavbee-release-builder')
  el.addEventListener('wavbee:ready', (e) => console.log('Builder ready', e.detail))
  el.addEventListener('wavbee:submitted', (e) => console.log('Submitted run', e.detail.run_id))
  el.addEventListener('wavbee:verdict', (e) => console.log('Verdict', e.detail))
  // A new session for the same draft, before this one ends: nothing the artist typed is lost.
  const renew = async () => el.setAttribute('client-token', await mintSessionOnMyServer())
  el.addEventListener('wavbee:session-expiring', renew)
  el.addEventListener('wavbee:session-expired', renew)
  el.addEventListener('wavbee:error', (e) => console.error('Embed error', e.detail.code))
</script>
```

A working static example ships at `/embed/v1/example.html`.

## Step 4 — Or mount the React wrapper (`@wavbee/release-builder-react`)

The React wrapper loads `embed.js` once and turns every `wavbee:*` event into a callback prop:

```jsx
import { ReleaseBuilder } from '@wavbee/release-builder-react'

export function ReleasePage({ clientToken }) {
  return (
    <ReleaseBuilder
      origin="https://YOUR-ACTIVQC-HOST"
      clientToken={clientToken}
      locale="en"
      theme={{ mode: 'light', '--wb-accent': '#0b5fff' }}
      onReady={({ draft_id }) => console.log('Ready', draft_id)}
      onSubmitted={({ run_id }) => console.log('Run queued', run_id)}
      onVerdict={({ verdict, final_decision }) => console.log('Verdict', verdict, final_decision)}
      onSessionExpired={() => window.location.reload()}
      onError={({ code }) => console.error('Error', code)}
    />
  )
}
```

## Step 5 — Trust webhooks on your backend, not browser events

Treat `wavbee:submitted` and `wavbee:verdict` as **UI hints only**. Subscribe to the signed server webhooks
(`release_draft.submitted` and `qc.run.completed`) and verify `X-Wqc-Signature` (HMAC-SHA256) before writing the
release or verdict into your own database ([Webhooks](https://activqc.wavbee.com/docs/webhooks.md)).


## Messages from the builder (`wavbee.builder.v1`)

Every message sent from the iframe has the envelope:

```json
{
  "protocol": "wavbee.builder.v1",
  "type": "submitted",
  "version": 1,
  "nonce": "a8c9...",
  "data": { "run_id": 42 }
}
```

The iframe posts **only** to the session's `allowed_origin` (never `*`). The `<wavbee-release-builder>` loader
validates `event.origin` against the engine origin and `event.source` against its iframe before re-emitting each
message as a `CustomEvent` named `wavbee:<type>` with `event.detail === data`:

| Message `type` | `CustomEvent` name | `detail` payload |
|---|---|---|
| `ready` | `wavbee:ready` | `{ session_id, draft_id }` |
| `resize` | `wavbee:resize` | `{ height }` (the loader also sets iframe height automatically) |
| `step.changed` | `wavbee:step.changed` | `{ step }` (`basics`, `tracks`, `artwork`, `people`, `details`, `review`) |
| `draft.saved` | `wavbee:draft.saved` | `{ lock_version }` |
| `question.opened` | `wavbee:question.opened` | `{ rule }`, the first question open on the steps the artist has reached |
| `submitted` | `wavbee:submitted` | `{ run_id }` (also emitted server-side as signed `release_draft.submitted` webhook) |
| `verdict` | `wavbee:verdict` | `{ verdict, final_decision }`, when the checks end and again when a person decides |
| `draft.opened` | `wavbee:draft.opened` | `{ draft_id }`, when the artist opens the release again or starts a new one |
| `talk_to_person` | `wavbee:talk_to_person` | `{}`, when the artist uses your contact button |
| `session.expiring` | `wavbee:session.expiring`, `wavbee:session-expiring` | `{ expires_at }`, five minutes before the session ends |
| `session.expired` | `wavbee:session.expired`, `wavbee:session-expired` | `{}` |
| `error` | `wavbee:error` | `{ code }` |


The element names and the `wavbee:*` event names stay as they are, so a page you have built keeps working. Nothing an
artist sees carries our name: the chat, the results and the frame titles use yours.

## What the artist meets

A new release starts with one question: what it is called. The title is checked there, before the rest of the form opens.
Prefill `release.title` when you mint the session and the artist goes straight to the form. Whether it is a single, an EP or an
album is worked out from the tracks and never asked. Each step is checked while the artist is on it; a step they have not
reached yet says "Not started", and the review at the end shows everything.

## After the artist submits

The same page shows the result. The artist answers what the check asks, opens the release again to change it, or starts
a new one; a reload shows the same. A release opened again keeps its draft id and is sent as a new run, so you get a new
`release_draft.submitted` webhook with a new `run_id`. A new release is a new draft for the same `end_user_ref`, without
your `external_ref`. An accepted release cannot be opened again.

## When a session ends

A session lasts 30 minutes. The draft is kept on our servers, so ending loses nothing. Five minutes before, the frame
sends `session.expiring`; mint a new session with the same `draft_id` and set the new `client-token` on the element,
which opens it in the same frame. Without that, the artist reads that the session has ended and what they saved is safe.

## Set up the builder for your account

Under **Builder set-up** in your console you decide, for the hosted and the embedded builder alike:

- which fields show and which are required, in your own words;
- the stores, languages and territories you offer and what a new release starts with;
- documents to ask for, such as a licence for a cover;
- a welcome text, a contact button, the chat and its languages;
- your name, logo, accent, corners and letters.

Fields every store needs always stay required. A session keeps the set-up it was minted with. Required fields and
documents are checked on our servers too, on validate and on submit: a missing one is asked as a question (rules `RF…`
and `RD…`), with a way to say there is none, and never fails a release by itself.

## What your account switches on, and your look

Your tenant's enabled features are **snapshotted onto the session at mint**.
Disabled features are absent from the UI (never greyed out) and enforced on `/embed/api/*`:

- **`builder.cover_fix` off:** no "Remove it for me" cover repair UI.
- **`builder.audio_fix` off:** no "Fix it for me" / "Trim it for me" audio repair UI.
- **`builder.chat` off:** no assistant chat widget.
- **`checks.tier2_audio` off:** no audio upload/waveform/audio-check UI; tracks display *"Your distributor checks audio separately."*
- **`branding` on:** your name, logo, accent, corners and letters apply automatically, with per-session `theme`
  overrides layered on top. Text on your accent is black or white, whichever reads better. Without a `theme` mode all
  three embeds follow the artist's device. Severity colours (`--rb-sev-*`) are never themeable so blocking issues
  cannot be hidden.


## Security

- **No API key in the browser:** after token exchange, the iframe talks exclusively to `/embed/api/*` using its
  encrypted, `HttpOnly`, `SameSite=None; Secure; Partitioned` cookie.
- **Strict session scoping:** every `/embed/api/*` route is scoped to the session's tenant and bound draft only;
  requesting any other draft or run returns `404`.
- **Per-tenant CSP `frame-ancestors`:** `/embed/builder` sends `Content-Security-Policy: frame-ancestors <your origins>`
  (and `'none'` when no tenant is identified), with `X-Frame-Options` removed so cross-origin framing works strictly
  according to your allowlist.
- **Origin-checked postMessage:** never `postMessage(..., '*')` in either direction.
- **Per-session rate limiting:** each embed session gets its own rate-limit bucket on `/embed/api/*`.
