Guides / Payments
In Lemon Squeezy dashboard: Settings → Webhooks → Add webhook.
https://yourdomain.com/api/webhooks/lemonsqueezyorder_created, subscription_created, subscription_cancellednpm install logit
// app/api/webhooks/lemonsqueezy/route.ts
import { createHmac } from "crypto";
import { init } from "logit";
const logit = init({ token: process.env.LOGIT_API_KEY! });
interface LSEvent {
meta: { event_name: string };
data: {
attributes: {
user_email: string;
total: number;
currency: string;
order_number: number;
user_name: string;
};
};
}
function verify(body: string, sig: string): boolean {
const hash = createHmac("sha256", process.env.LEMON_SQUEEZY_WEBHOOK_SECRET!)
.update(body)
.digest("hex");
return hash === sig;
}
export async function POST(req: Request) {
const body = await req.text();
if (!verify(body, req.headers.get("x-signature") ?? "")) {
return new Response("Unauthorized", { status: 401 });
}
const event = JSON.parse(body) as LSEvent;
const { event_name } = event.meta;
const attrs = event.data.attributes;
if (event_name === "order_created") {
await logit.now("payments", {
event: "New Lemon Squeezy order 🍋",
description: `${attrs.user_name} (${attrs.user_email}) — $${(attrs.total / 100).toFixed(2)} ${attrs.currency}`,
icon: "🍋",
notify: true,
tags: { currency: attrs.currency.toLowerCase() },
metadata: {
orderNumber: attrs.order_number,
email: attrs.user_email,
amount: attrs.total,
},
});
}
if (event_name === "subscription_cancelled") {
await logit.now("churn", {
event: "Subscription cancelled",
description: attrs.user_email,
icon: "😢",
notify: true,
metadata: { email: attrs.user_email },
});
}
return new Response("ok");
}
LOGIT_API_KEY=lk_live_xxxxxxxx
LEMON_SQUEEZY_WEBHOOK_SECRET=your_webhook_secret
Try LogIt free
7-day trial. No credit card required.