# End-to-end walkthrough

> Build a complete embedded integration on the preview endpoint: a blank page with the script tag, prefill, your lead list, webhooks and policy documents.

Build a complete embedded integration on the preview endpoint, from a blank page to quotes, policies, webhooks and documents. Each step adds one piece to the same small site: a page with Kinro's script tag and the server next to it. When every step works, your integration covers the whole lifecycle.

## Before you start

Ask your Kinro partnership contact for a sandbox setup on the preview endpoint:

- your public partner ID (`ap_...`);
- a `test` partner token (`kinro_pk_...`), kept on your server;
- your production origins registered as allowed origins when you are ready to deploy. Local pages on `http://localhost` or `http://127.0.0.1` work without registration; see [Develop locally](/docs/embedding#8-develop-locally);
- a `test` webhook for a public HTTPS URL that reaches your server, such as a tunnel to your machine, and its `whsec_...` signing secret.

Everything below runs against `https://api-preview.kinro.com/mcp`. Every lead your `test` token creates is a sandbox test lead: it never reaches live carriers or real customers. See [Sandbox & go-live](/docs/sandbox).

## 1. A blank page with the script tag

Start from the smallest page that can host Kinro: the script tag and an empty element for the quote widget.

```html
<!doctype html>
<html>
  <body>
    <button id="get-insured">Get business insurance</button>
    <div id="kinro-slot"></div>

    <script
      src="https://kinro.com/partners/embed.js"
      data-partner="ap_YOUR_PARTNER_ID"
      data-mcp-url="https://api-preview.kinro.com/mcp"
      data-launcher="none"
      defer
    ></script>
  </body>
</html>
```

`data-mcp-url` points the widget at the preview endpoint; remove it in production. When the page loads, `window.Kinro` is available and fires `ready`.

## 2. Open the widget for a signed-in customer

Your server sends what it knows about the customer and gets back a short-lived customer token. Before activation, Kinro opens the widget only with such a token. After activation, anonymous visitors can use it too.

```js
// Your server
app.post("/api/kinro-customer", async (req, res) => {
  const result = await callKinro("upsert_lead", {
    external_id: req.user.accountId,
    data: {
      business: { name: req.user.businessName, trade: "Landscaping" },
      address: { zip: "78701", state: "TX" },
    },
    mint_customer_token: true,
  });
  res.json({ customer_token: result.customer_token });
});

// callKinro: one JSON-RPC tools/call to Kinro with your partner token.
async function callKinro(name, args) {
  const response = await fetch("https://api-preview.kinro.com/mcp", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.KINRO_PARTNER_TOKEN}`,
      "Content-Type": "application/json",
      Accept: "application/json, text/event-stream",
    },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: crypto.randomUUID(),
      method: "tools/call",
      params: { name, arguments: args },
    }),
  });
  const message = await response.json();
  if (message.error || message.result.isError) throw new Error(JSON.stringify(message));
  return message.result.structuredContent;
}
```

```js
// Your page
document.getElementById("get-insured").addEventListener("click", async () => {
  const { customer_token } = await fetch("/api/kinro-customer", { method: "POST" })
    .then((r) => r.json());
  window.Kinro.mount(document.getElementById("kinro-slot"), {
    placement: "insurance_tab",
    customerToken: customer_token,
  });
});
window.Kinro?.on("error", (event) => console.warn("Kinro widget", event.code));
```

The widget opens with what you sent already filled in. Keep the token in memory only. [Prefill](/docs/prefill) lists every field you can send, and the [embed guide](/docs/embedding#4-lifecycle-events) lists the widget's events.

> **Note:** If Kinro answers with an SSE stream (`Content-Type: text/event-stream`), read the JSON-RPC message from its `data:` line.

## 3. List your leads and see how far they got

Call [`list_leads`](/docs/leads) from your server to show the leads you sent to Kinro, how many were quoted and how many bought a policy.

```js
app.get("/api/kinro-leads", async (req, res) => {
  const { counts, leads } = await callKinro("list_leads", { limit: 50 });
  res.json({ counts, leads });
});
```

The customer from step 2 appears with `stage: "transferred"`. Once they answer in the widget, the lead moves to `engaged`, and `last_customer_activity_at` and `last_contacted_at` show when they last wrote and when Kinro last replied. On preview your list also contains the [shared test lead](/docs/sandbox#shared-test-lead), `kinro_shared_test_lead`, which is `bound` and has a policy with documents, so `counts` shows at least one quoted and one bound lead from the start.

## 4. Receive webhooks

Add the endpoint Kinro sends signed events to. Verify each delivery against the raw body before you trust it, and acknowledge with a `2xx` status.

```js
app.post("/webhooks/kinro", express.raw({ type: "application/json" }), (req, res) => {
  const ok = verifyKinroWebhook(
    req.body,
    req.get("X-Kinro-Signature"),
    process.env.KINRO_WEBHOOK_SECRET,
  );
  if (!ok) return res.sendStatus(401);
  const event = JSON.parse(req.body);
  // Handle each event.id once: deliveries can repeat.
  res.sendStatus(200);
});
```

`verifyKinroWebhook` is the function in [Verifying webhook signatures](/docs/events-webhooks#verifying-webhook-signatures).

Then make Kinro send each event for the shared test lead:

```js
for (const action of ["quote_ready", "policy_bound", "policy_status_changed"]) {
  await callKinro("simulate_sandbox_event", {
    action,
    external_id: "kinro_shared_test_lead",
  });
}
```

Your endpoint receives `quote.ready`, `policy.bound` and `policy.status_changed` (a cancellation), each with `external_id: "kinro_shared_test_lead"` and `environment: "test"`. Only your webhooks receive them, and the shared lead itself does not change.

## 5. Show the policy documents

The shared test lead's sandbox policy lists its documents with a `download_url` that works for 15 minutes. Download the file on your server and serve it to authorized users from your own origin:

```js
app.get("/api/kinro-documents/:documentId", async (req, res) => {
  const { leads } = await callKinro("list_leads", { limit: 50 });
  const doc = leads
    .flatMap((lead) => lead.policies)
    .flatMap((policy) => policy.documents ?? [])
    .find((d) => d.document_id === req.params.documentId);
  if (!doc?.download_url) return res.sendStatus(404);
  const file = await fetch(doc.download_url);
  res.type(doc.content_type ?? "application/pdf");
  res.send(Buffer.from(await file.arrayBuffer()));
});
```

```html
<iframe src="/api/kinro-documents/DOCUMENT_ID" title="Policy document"></iframe>
```

The shared test lead has a declarations page, a policy and a certificate of insurance, all sample PDFs. Documents are listed for test leads only for now. See [Policy documents](/docs/leads#policy-documents).

## 6. Check the whole run

Your integration is ready to review with Kinro when:

- the widget opens for a signed-in customer with no `error` event;
- that customer appears in `list_leads` as `transferred`, then `engaged` once they answer, and your page's counts match `counts`;
- all three webhooks verify, and a repeated `event.id` is handled once;
- every document of the shared test lead downloads and displays.

Then work through the [staging QA checklist](/docs/sandbox#staging-qa-checklist) and [go live](/docs/sandbox#going-live).

---

Source: https://kinro.com/docs/walkthrough. All Kinro docs: https://kinro.com/docs/llms.txt
