> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fynn.eu/llms.txt
> Use this file to discover all available pages before exploring further.

# Embedding

> Embed the plan change into your own application: portal session, iframe, events over postMessage and theming through CSS variables.

When your customers are already signed in to your own application, they should be able to change their plan there without switching to Fynn and signing in a second time. You embed the plan page as an iframe.

Three things stay right while you do: your frontend never holds an API token, payment data never touches your DOM, and the truth about the contract still lives in the webhooks.

## The flow

**1. Create the session in your backend**

Your backend requests a [portal session](https://docs.fynn.eu/en/guide/customer-portal/login.md#a-portal-session-from-your-application) with the scope `plan-change` and passes the `token` to your frontend. A session with that scope cannot reach invoices or payment methods.

```json theme={null}
{ "customerId": "01a09507-a58e-79af-b3f4-43328ed14a1d", "scope": "plan-change" }
```

**2. Allow your domain**

The domain of your application has to be listed as an allowed origin for the sales channel. Without that entry the two sides exchange no messages. Send it to [support@fynn.eu](mailto:support@fynn.eu) to have it added.

**3. Embed the iframe**

The page lives on the portal domain of your sales channel:

```html theme={null}
<iframe
  id="fynn-plan"
  src="https://demo-acme.customerfront.app/embed/plan?token=fps_eyJ0eXAi…"
  style="width: 100%; border: 0"
  title="Change plan"
></iframe>
```

**4. Listen for events**

The embedded page reports its height and every decision the customer makes.

![Embedded plan page stating that this subscription has no plan change](https://mintcdn.com/fynnsubscriptionbilling/tv50Rqhs2iBwmkcc/images/customer-portal/plan-embed.png?fit=max\&auto=format\&n=tv50Rqhs2iBwmkcc\&q=85\&s=07da78436c9ae19380ff4ad28577edc6)

The embedded plan page, here for a subscription without a plan tier.

The language comes in with the portal session. If a customer switches the language in your application, send `fynn:locale` instead of remounting the iframe.

## Events

Messages from the iframe to your page:

| Message | Meaning |
| - | - |
| `fynn:ready` | The page is up. |
| `fynn:resize` | The height changed, `height` carries the new one. |
| `fynn:plan-changed` | The plan was switched. Carries `subscriptionId`, `tierId` and `effectiveAt`. |
| `fynn:change-scheduled` | The switch was scheduled for a later date. Same fields. |
| `fynn:cancelled` | The customer abandoned the flow. |
| `fynn:payment-method-missing` | There is no chargeable payment method on file. |
| `fynn:session-expired` | The session has expired. |
| `fynn:error` | Something failed, `code` names it. |

Messages from your page to the iframe:

| Message | Effect |
| - | - |
| `fynn:theme` | Sets CSS variables, see below. |
| `fynn:locale` | Sets the language of the embedded page, `de` or `en`. |

```js theme={null}
const frame = document.getElementById('fynn-plan');
const fynnOrigin = 'https://demo-acme.customerfront.app';

window.addEventListener('message', (event) => {
  if (event.origin !== fynnOrigin) return;

  switch (event.data?.type) {
    case 'fynn:resize':
      frame.style.height = `${event.data.height}px`;
      break;
    case 'fynn:plan-changed':
      closeDialog();
      break;
    case 'fynn:session-expired':
      remountWithFreshToken();
      break;
  }
});
```

> [!WARNING]
>
> Check `event.origin` before you act on a message. Fynn does the same in the other direction: messages from a domain that is not allowed are dropped, and outgoing messages go only to the allowed domains, never to arbitrary recipients.

## Theming

The look comes from the branding of your sales channel: colour, logo, font and corner radii are already set when the page loads. To match it to your product at runtime, send CSS variables:

```js theme={null}
frame.contentWindow.postMessage(
  { type: 'fynn:theme', variables: { '--fynn-color-brand': '#be185d' } },
  fynnOrigin,
);
```

Common variables:

| Variable | Used for |
| - | - |
| `--fynn-color-brand` | Primary buttons and links |
| `--fynn-color-text` | Body text |
| `--fynn-color-muted` | Secondary lines |
| `--fynn-color-line` | Separators and borders |
| `--fynn-color-surface` | Surfaces of cards and fields |
| `--fynn-color-page` | Page background |
| `--fynn-radius-field` | Corner radius of fields and buttons |
| `--fynn-font-family` | Font |

## When the session expires

A portal session lasts 15 minutes by default. When it expires, the embedded page says so and emits `fynn:session-expired`. Fetch a fresh session from your backend and remount the iframe. Nothing is stored in the browser, so remounting costs nothing.

## What is binding

`fynn:plan-changed` exists so your interface can update immediately, for example to close a dialog. The webhook is what is binding. If your application stores the new plan itself, do it when the webhook arrives, not when the message from the iframe does.
