# Mira embed examples

Mira is a hosted AI shopping companion. Every storefront — regardless of stack — embeds the same zero-dependency, Shadow-DOM widget from your Mira API origin. These examples show how to wire it up on four common stacks.

| Store stack | Guide | What it covers |
| --- | --- | --- |
| WordPress + WooCommerce | [`plugins/wordpress/README.md`](../plugins/wordpress/README.md) | Official plugin, settings page, WooCommerce cart/order events, `[mira_chat]` shortcode |
| Next.js (App Router) | [`examples/nextjs/`](./nextjs/README.md) | Drop-in client component using `next/script`, auth-derived customer id |
| Custom PHP | [`examples/php/`](./php/README.md) | Framework-agnostic helper class rendering a safely escaped snippet |
| Python (Flask / Django / FastAPI) | [`examples/python/`](./python/README.md) | Stdlib snippet builder, Flask example, `Markup` guidance |

Deployment references: [`DEPLOYMENT.md`](../DEPLOYMENT.md) (Docker / standard Render), [`RENDER_FREE.md`](../RENDER_FREE.md) (Render free-tier node blueprint).

---

## The embed contract (same on every stack)

The widget reads four globals before it boots. Set them **before** loading the script:

```html
<script>
  window.MIRA_TENANT_ID = "YOUR_TENANT_ID";
  window.MIRA_API_URL = "https://api.mira.app";
  // Optional: ties conversations to a known shopper for long-term memory.
  window.MIRA_CUSTOMER_ID = "cus_123";
  // Optional: appearance / behaviour overrides.
  window.MIRA_CONFIG = {
    position: "bottom-right",
    primaryColor: "#7C5CFC",
    avatar: "🤖",
    autoMinimize: true,
    voice: false
  };
</script>
<script async src="https://api.mira.app/mira-widget.js"></script>
```

The script URL is always **`${MIRA_API_URL}/mira-widget.js`** (trailing slash optional). If you self-host or use a custom domain, replace `https://api.mira.app` with your Mira API base — for the free Render deployment in this repo that is `https://mira.imagiverseit.com`, so the widget URL is `https://mira.imagiverseit.com/mira-widget.js`.

### The four values you must get right

1. **Tenant ID** — public identifier from Mira dashboard → Settings → Store connections. It is safe in browser code; it scopes every widget request via `X-Mira-Tenant` and the request body. The tenant **API key** (`mira_sk_…`) is a server-only secret and must never appear in HTML, JS, or a client bundle.
2. **API base URL** — where the widget script and `/api/widget/*` endpoints live. Same-origin with your Mira deployment. Set `NEXT_PUBLIC_API_URL` on the server so generated snippets and redirects agree with the script `src`.
3. **Logged-in customer id** — an opaque, stable id from your own auth/session (`cus_123`, a user UUID, a WooCommerce session id). It enables long-term preference memory and cart continuity. Do **not** put emails, names, tokens, or secrets in it. Leave it unset for anonymous visitors; the widget generates a per-browser session id.
4. **CORS allowlist** — on the Mira server set `WIDGET_ALLOWED_ORIGINS` to the exact storefront origins that embed the widget, e.g. `https://shop.example.com,https://www.shop.example.com` (scheme + host + port, no trailing slash, no paths). `*` is acceptable for local development only. Every `/api/widget/*` call from the browser is checked against this list; a missing origin yields a CORS error even when the script loads fine.

> Note: the current widget bundle also accepts the same appearance settings as `data-*` attributes on the script tag (`data-tenant`, `data-api`, `data-color`, `data-avatar`, `data-position`, `data-auto-minimize`, `data-consent`, `data-store-url`). The helpers in these examples publish **both** `window.MIRA_CONFIG` and the matching data attributes, so branding works with older and newer widget builds.

---

## Choosing an integration

- **WordPress / WooCommerce:** install the official plugin from [`plugins/wordpress/`](../plugins/wordpress/). It enqueues the script in the footer, injects globals with `wp_add_inline_script(..., 'before')`, sends WooCommerce events with the server-side API key, and offers `[mira_chat]` for per-page placement. No custom snippets needed.
- **Next.js:** see [`examples/nextjs/MiraWidget.tsx`](./nextjs/MiraWidget.tsx). Mount it once in the root layout (or a product layout), pass the tenant id and, when available, the customer id from your auth session.
- **Custom PHP / other CMS:** see [`examples/php/mira-widget.php`](./php/mira-widget.php). Call `MiraWidgetEmbed::render(...)` (or `mira_widget_snippet(...)`) in your footer template. It works with plain PHP and with WordPress, without requiring WordPress.
- **Python:** see [`examples/python/mira_widget.py`](./python/mira_widget.py) for the snippet builder and [`flask_example.py`](./python/flask_example.py) for a complete app. Wrap the returned string in Jinja `Markup`/`|safe` — the values inside it are already escaped.

After embedding, verify with:

```bash
curl -fsS https://api.mira.app/api/health          # your Mira API base
curl -fsSI https://api.mira.app/mira-widget.js     # widget must be publicly served
```

Then open a store page, open the browser console, and check for `[Mira] Missing tenant id` (bad tenant) or CORS errors (bad `WIDGET_ALLOWED_ORIGINS`). Keep exactly one widget script per page to avoid duplicate hosts.
