# Sell tickets on your own website

Source: https://docs.tuppence.ai/docs/events/embed/

> Put an event's page and checkout on your site with one tag or a React component, as a button, corner button or inline, and hear when tickets are bought.

For agents: [start here](https://docs.tuppence.ai/docs/agents.md), [every page](https://docs.tuppence.ai/docs/llms.txt), [the agent guide](https://docs.tuppence.ai/docs/SKILL.md), [the MCP server](https://docs.tuppence.ai/docs/mcp.md).

The event widget shows a published [event](/docs/events/) on your site, in a frame from
`book.tuppence.ai`: its dates, ticket types and the whole checkout. Buyers stay on your site, pay
in the frame, and get their tickets by email as they would on the event's own page.

*Diagram: embed.js draws a button or an inline frame from book.tuppence.ai. The buyer picks tickets and pays in the frame. Your page gets a tuppence:tickets.completed event; your server gets ticket_order.paid and ticket.issued webhooks.*

1. **Your page → Tuppence**: embed.js draws the event in a frame. One `<tuppence-event>` element (or `data-event` on the tag) draws a button, pop-up or inline frame.
2. **Tuppence**: buyer picks tickets and pays. Names, emails and card details stay in the frame; your page never sees them.
3. **Tuppence → Your page**: tuppence:tickets.completed. A `window` event with the order's `id`, `status`, total and ticket count, no buyer details.
4. **Tuppence → Your server**: ticket_order.paid webhook (later) `ticket_order.paid` and one `ticket.issued` per ticket, as for any order.

## 1. Publish the event

The widget shows an event that is on sale. Its slug is the end of its page's address:
`book.tuppence.ai/e/agents-after-dark` has the slug `agents-after-dark` (the `slug` field of the
event).

## 2. Add the tag

```html
<script async src="https://book.tuppence.ai/embed.js"></script>
<tuppence-event slug="agents-after-dark" mode="button"></tuppence-event>
```

Or on the tag itself, where it should draw:

```html
<script async src="https://book.tuppence.ai/embed.js"
        data-event="agents-after-dark" data-mode="inline"></script>
```

From your own button: `TuppenceBook.open({ event: "agents-after-dark" })`, and
`TuppenceBook.close()`.

### In React

The component loads `embed.js` once and draws the element. Save it as `tuppence-event.tsx`:

```tsx
import { useEffect, useRef } from "react";

type Order = { id: string; status: string; amount_total: number; currency: string; quantity: number };

export function TuppenceEvent(props: {
  slug: string;
  mode?: "button" | "inline" | "popup";
  label?: string;
  color?: string;
  onCompleted?: (order: Order) => void;
}) {
  const ref = useRef<HTMLElement | null>(null);
  const { onCompleted } = props;
  useEffect(() => {
    const src = "https://book.tuppence.ai/embed.js";
    if (!document.querySelector(`script[src="${src}"]`)) {
      const script = document.createElement("script");
      script.async = true;
      script.src = src;
      document.head.append(script);
    }
    const el = ref.current;
    if (!el || !onCompleted) return;
    const heard = (e: Event) => onCompleted((e as CustomEvent<{ order: Order }>).detail.order);
    el.addEventListener("tuppence:tickets.completed", heard);
    return () => el.removeEventListener("tuppence:tickets.completed", heard);
  }, [onCompleted]);
  return (
    // @ts-expect-error: a custom element, drawn by embed.js
    <tuppence-event ref={ref} slug={props.slug} mode={props.mode} label={props.label} color={props.color} />
  );
}
```

```tsx
<TuppenceEvent slug="agents-after-dark" onCompleted={(order) => console.log(order.id)} />
```

## Options

| Attribute (`data-` on the tag) | |
|---|---|
| `slug` (`data-event`) | **Required.** The event's slug. |
| `mode` | `button` (default: a button where the element is), `popup` (a floating corner button) or `inline` (the event in the page, full height, never scrolling itself). |
| `label`, `color` | Button text (default *Get tickets*) and colour (`#RRGGBB`); the text is white, or near-black where white wouldn't read. |
| `title` | Screen-reader name for the pop-up and frame (default *Get tickets*). |
| `ch` | Where buyers came from, for your [sales by source](/docs/events/#pixels-and-sources). |

## Access codes and hidden tickets

Ticket types that are `hidden` or `access_code_only` don't show in the widget, as on the event's
page. A buyer types the code in checkout's *Promo code* field, and the types it opens
appear. Give the code to the people it's for; the widget never needs it in your page's code. Promo
codes work the same way. See [promo and access codes](/docs/events/#2-add-ticket-types).

## Hear when tickets are bought

Two events bubble to `window` (and the element):

| Event | `detail` |
|---|---|
| `tuppence:tickets.completed` | `order`: `id`, `status`, `amount_total`, `currency`, `quantity` (no buyer details, no codes) |
| `tuppence:tickets.cancelled` | `reason: "closed"`: the pop-up was closed without buying |

```js
window.addEventListener("tuppence:tickets.completed", (event) => {
  console.log("tickets", event.detail.order.id);
});
```

Browser events are lost if the page closes. Rely on [webhooks](/docs/webhooks/) for anything that
matters (`ticket_order.paid`, `ticket.issued`).

## What the buyer sees

| | |
|---|---|
| **Pop-up** | A dialog that holds focus until closed (*Close*, Escape or a click outside), then gives it back to the button. Full-screen on a small screen. |
| **Inline** | As tall as the event's page; your page scrolls, not the frame. |
| **Paying** | Card fields, Apple Pay and Google Pay stay in the frame (`allow="payment *"`). |
| **Their tickets** | Links to a ticket open in a new tab: ticket pages are never framed. |

The widget follows the event's mode: a test-mode event takes test cards only. See
[Testing](/docs/testing/).

## A site with a Content-Security-Policy

Allow `https://book.tuppence.ai` in `script-src` and `frame-src`; no inline script or style is
needed. To pin the loader with Subresource Integrity, see
[the booking widget's notes](/docs/bookings/embed/#pin-the-loader-with-subresource-integrity): it
is the same `embed.js`.

## Next

- [Events](/docs/events/): create the event and its ticket types.
- [Webhooks](/docs/webhooks/): orders and tickets on your server.
