Browse guides
Express and Hono
Credit Checkout Sessions from a Node or edge API server.
2 min read · For companies
When your frontend talks to a separate API server, the snippet goes on the frontend and the API reads the br_click cookie. The browser only sends it when the API is on the same domain (api.acme.com next to www.acme.com) and the request includes credentials.
import type Stripe from "stripe";
// betterrewards: copy this file into your app. It has no dependencies.
const CLICK_ID = /^clk_[0-9a-f]{32}$/;
/** A click id stored by the snippet, or null. Pass the br_click cookie's value. */
export function clickId(value: string | null | undefined): string | null {
return value && CLICK_ID.test(value) ? value : null;
}
/** Reads the click id from a raw Cookie header. */
export function clickIdFromCookieHeader(header: string | null | undefined): string | null {
const match = header?.match(/(?:^|;\s*)br_click=([^;]+)/);
return clickId(match?.[1] ? decodeURIComponent(match[1]) : null);
}
/** Credits a Checkout Session, and the subscription it starts, to the referring partner. */
export function withReferral(
params: Stripe.Checkout.SessionCreateParams,
id: string | null,
): Stripe.Checkout.SessionCreateParams {
if (!id) return params;
return {
...params,
client_reference_id: params.client_reference_id ?? id,
metadata: { ...params.metadata, br_click_id: id },
...(params.mode === "subscription" && {
subscription_data: {
...params.subscription_data,
metadata: { ...params.subscription_data?.metadata, br_click_id: id },
},
}),
};
}
/** Metadata for stripe.customers.create at signup, so later payments need no cookie. */
export function referralMetadata(id: string | null): Record<string, string> {
return id ? { br_click_id: id } : {};
}Express
import { clickIdFromCookieHeader, withReferral } from "../betterrewards";
app.post("/checkout", async (req, res) => {
const id = clickIdFromCookieHeader(req.headers.cookie);
const session = await stripe.checkout.sessions.create(withReferral({
mode: "subscription",
line_items: [{ price: "price_123", quantity: 1 }],
success_url: "https://app.acme.com/welcome",
cancel_url: "https://www.acme.com/pricing",
}, id));
res.json({ url: session.url });
});Hono
import { getCookie } from "hono/cookie";
import { clickId, withReferral } from "./betterrewards";
app.post("/checkout", async (c) => {
const id = clickId(getCookie(c, "br_click"));
const session = await stripe.checkout.sessions.create(withReferral({
mode: "subscription",
line_items: [{ price: "price_123", quantity: 1 }],
success_url: "https://app.acme.com/welcome",
cancel_url: "https://www.acme.com/pricing",
}, id));
return c.json({ url: session.url });
});await fetch("https://api.acme.com/checkout", { method: "POST", credentials: "include" });Create Stripe customers at signup? Add metadata: referralMetadata(id) to stripe.customers.create as well. Every later payment of that customer is then credited without the cookie, even weeks later or after Safari has cleared it.
Then check it in your instance under Integrations → Tracking & links: Open test page proves the snippet runs on your site, and a test payment followed by I've paid, check now proves the click reaches Stripe. Nobody is credited for the test.