# Example: Express

> Add 'Pay with crypto' to an Express checkout: one server file and one HTML page. Create the invoice, open the pop-up, ship on the raw-body webhook.

One server file and one page. The webhook route takes the **raw** body with `express.raw()`,
mounted before `express.json()`, because the signature covers the exact bytes we sent. **Ship the
order on the `invoice.paid` webhook**, never on the pop-up's `onPaid`. This is
[`examples/checkout-express`](https://github.com/devluke24/intentsfi/tree/main/examples/checkout-express),
run in CI against a test key.

```bash
npm install @intents-fi/sdk express
```

```bash
# .env
# A test key while you build: dashboard.intents.fi → API keys → Test mode.
INTENTS_SECRET_KEY=ik_test_...
# The whsec_… shown when you added the webhook endpoint (Webhooks → Add an endpoint).
INTENTS_WEBHOOK_SECRET=whsec_...
```

## The server

`server.js`

```js
import { createInvoice, isTestEvent, verifyWebhook, WebhookSignatureError } from "@intents-fi/sdk";
import express from "express";

// Your order store. Here: memory. markPaid must be safe to call twice for one order.
export const orders = new Map([
  ["order_1001", { id: "order_1001", amount: "25.00", status: "pending" }],
]);
const markPaid = (id) => {
  const order = orders.get(id);
  if (!order || order.status === "paid") return; // unknown, or already shipped: do nothing
  orders.set(id, { ...order, status: "paid" });
  console.log(`order ${id} paid: ship it`); // …your fulfilment
};

export const app = express();

// 3. The webhook, mounted BEFORE express.json(): verifyWebhook needs the raw bytes (a Buffer).
app.post("/webhooks/intents", express.raw({ type: "application/json" }), async (req, res) => {
  try {
    const event = await verifyWebhook(req.body, req.headers, process.env.INTENTS_WEBHOOK_SECRET);
    // Ship only on a real invoice.paid; "Send test event" samples are acknowledged and ignored.
    if (event.type === "invoice.paid" && !isTestEvent(event)) markPaid(event.data.reference);
    res.sendStatus(204); // any 2xx; anything else is retried
  } catch (e) {
    if (e instanceof WebhookSignatureError) return res.status(400).send("bad signature");
    throw e;
  }
});

app.use(express.json());
app.use(express.static(new URL("./public", import.meta.url).pathname));

// 1. "Pay with crypto" + Place order: create the invoice. The amount comes from your records.
app.post("/checkout", async (req, res) => {
  const order = orders.get(req.body.orderId);
  if (!order) return res.status(404).json({ error: "unknown order" });
  const invoice = await createInvoice({ amount: order.amount, reference: order.id }); // reads INTENTS_SECRET_KEY
  res.json({ invoiceId: invoice.id, clientSecret: invoice.clientSecret });
});

if (process.argv[1] === new URL(import.meta.url).pathname) {
  app.listen(3600, () => console.log("http://localhost:3600"));
}
```

## The page

`public/index.html`: the [Pay with crypto row](/docs/checkout/payment-option) beside your card
option, and `IntentsCheckout.open()` when the order is placed.

```html
<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pay with crypto: Express example</title>
<link rel="stylesheet" href="https://pay.intents.fi/v1/option.css">
<script src="https://pay.intents.fi/v1/checkout.js"></script>

<!-- 2. Your checkout: "Pay with crypto" beside your other methods. -->
<main style="max-width: 420px; margin: 48px auto; display: grid; gap: 8px">
  <h1>$25.00</h1>
  <label><input type="radio" name="payment-method" value="card" checked> Card</label>
  <label class="ifpm-Option ifpm-Option--light">
    <input class="ifpm-Radio" type="radio" name="payment-method" value="crypto">
    <span class="ifpm-Text">
      <span class="ifpm-Label">Pay with crypto</span>
      <span class="ifpm-Subtitle">Any token, any network</span>
    </span>
    <span class="ifpm-Icons" aria-hidden="true"><img src="https://pay.intents.fi/v1/brand/token-marks.svg" width="80" height="20" alt=""><span class="ifpm-More">+1,000</span></span>
  </label>
  <button id="place" type="button">Place order</button>
</main>

<script>
  document.getElementById("place").addEventListener("click", async () => {
    if (document.querySelector('[name="payment-method"]:checked').value !== "crypto") return; // your card flow
    const res = await fetch("/checkout", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ orderId: "order_1001" }),
    });
    const { invoiceId, clientSecret } = await res.json();
    // onPaid only updates this page; the order ships from the webhook.
    IntentsCheckout.open({ invoiceId, clientSecret, onPaid: () => location.assign("/?paid=1") });
  });
</script>
```

## Try it

1. In the dashboard, switch to **Test mode**, create a secret key, and add a webhook endpoint for
   `invoice.paid` (a tunnel's URL for a local server).
2. `node --env-file=.env server.js`, open http://localhost:3600, pick **Pay with crypto** and pay
   in the pop-up. Test mode moves no money.
3. **Send test event** delivers a sample that the server acknowledges and ignores; **Resend** on
   the real delivery leaves the order paid once.

---

Source: https://intents.fi/docs/checkout/examples/express · The whole corpus: https://intents.fi/docs/llms-full.txt
