# Embedded signing

By default, recipients follow a link in an email. To keep them in your app instead, give the
recipient a ceremony your app authenticates, and show its URL in an iframe or a WebView.

## Get a ceremony URL

Set the recipient's `ceremony` when you create the envelope, with `custom` authentication (you have
already signed the person in; `provider` and `data` go into the audit log) or `email_code`, and list
the origins allowed to frame it in `embeddable_in`:

```json
{
  "type": "signer",
  "key": "signer_1",
  "name": "Alex Doe",
  "email": "alex@example.com",
  "ceremony": {
    "authentication": [
      { "type": "custom", "provider": "Example App", "data": { "Account ID": "1234" } }
    ],
    "embeddable_in": ["https://app.example.com"]
  }
}
```

Once the envelope is `in_progress`, read `recipients[].ceremony.url` from the envelope, or create a
fresh URL with [Create a ceremony](https://docs.signapi.dev/api/CreateRecipientCeremony). Anyone holding the URL can act as
the recipient: keep it out of logs, analytics and shared links.

## Show it

Add `embedded=true` and `event_delivery=message` to the URL and load it in an iframe on an origin
listed in `embeddable_in`. Other pages cannot frame it.

| Parameter | Effect |
|---|---|
| `embedded=true` | Shows the ceremony for embedding. Signers see Cancel instead of Decline. |
| `event_delivery=message` | Sends events to the parent window with `postMessage`. |
| `event_delivery=redirect` | Ends by navigating to a `signatureapi-message://` URL, for native WebViews. |
| `allow_cancel=false` | Removes Cancel from an embedded ceremony. |
| `allow_decline=false` | Removes Decline from a standalone ceremony. |
| `accent_color=RRGGBB` | Colors the ceremony's buttons for this session. |

## Events

A ceremony sends at most one event:

| `type` | When | Extra fields |
|---|---|---|
| `ceremony.completed` | After the result page shows for `redirect_delay` seconds (default 3, 0 to 20) | |
| `ceremony.declined` | An approver rejected; after `redirect_delay` seconds | |
| `ceremony.canceled` | The recipient selected Cancel; at once | |
| `ceremony.failed` | The ceremony cannot be used; 3 seconds after the error page appears | `error_type`, `error_message` |

`error_type` is `unauthorized`, `already_completed`, `not_available`, `invalid_link` or
`unexpected_error`. Treat any other value as a general failure. `error_message` is English text
for your logs, not for the signer.

```js
window.addEventListener("message", (event) => {
  if (event.origin !== new URL(ceremonyUrl).origin || event.source !== iframe.contentWindow) return
  if (event.data?.type === "ceremony.completed") showThanks()
})
```

Messages are posted with target origin `*` and hold no secrets, so always check `event.origin`
and `event.source`. An event is a signal for your interface, not proof: confirm the recipient's
status with the API or a [webhook](https://docs.signapi.dev/webhooks-guide) before acting on it.

## Native apps

With `event_delivery=redirect`, the ceremony ends by navigating to
`signatureapi-message://<type>`, for example `signatureapi-message://ceremony.completed`. A failure
adds a query: `signatureapi-message://ceremony.failed/?error_type=not_available&error_message=…`.
Cancel that navigation in your WebView's navigation handler and read the event from the URL's
host. A WebView that loads the ceremony as its top-level page needs no `embeddable_in`.

## Examples

Working examples live in the Sign API repository under `web/examples/`:

| Folder | What it shows |
| --- | --- |
| `embed` | One HTML page with no dependencies that frames a ceremony URL and logs its events |
| `embed-react` | A React `SignatureCeremony` component with an `onEvent` callback |
| `embed-vue` | A Vue 3 `SignatureCeremony` component that emits `event` |
| `embed-nextjs` | A Next.js app whose server creates the ceremony with `custom` authentication and its own origin in `embeddable_in`, and whose page frames it |

Each one checks a message's origin and source before it trusts it, and none of them logs the
ceremony URL.
