Integrations / Payments / Lemon Squeezy

๐Ÿ‹

Lemon Squeezy

Wire up Lemon Squeezy webhooks to LogIt and track every order, subscription, and refund in your inbox.


Setup

In your Lemon Squeezy dashboard, go to Settings โ†’ Webhooks โ†’ Add webhook.

Set the URL to https://yourdomain.com/api/webhooks/lemonsqueezy and select these events:

  • order_created
  • subscription_created
  • subscription_cancelled
  • subscription_payment_failed

Install

bash
npm install logit

Handler (Next.js App Router)

ts
// app/api/webhooks/lemonsqueezy/route.ts
import { createHmac } from "crypto";
import { init } from "logit";

const logit = init({ token: process.env.LOGIT_API_KEY! });

interface LSOrderEvent {
  meta: { event_name: string };
  data: {
    attributes: {
      user_email: string;
      total: number;
      currency: string;
      order_number: number;
    };
  };
}

function verifySignature(body: string, signature: string): boolean {
  const hash = createHmac("sha256", process.env.LEMON_SQUEEZY_WEBHOOK_SECRET!)
    .update(body)
    .digest("hex");
  return hash === signature;
}

export async function POST(req: Request) {
  const body = await req.text();
  const signature = req.headers.get("x-signature") ?? "";

  if (!verifySignature(body, signature)) {
    return new Response("Unauthorized", { status: 401 });
  }

  const event = JSON.parse(body) as LSOrderEvent;
  const eventName = event.meta.event_name;
  const attrs = event.data.attributes;

  if (eventName === "order_created") {
    await logit.now("payments", {
      event: "New order",
      description: `${attrs.user_email} โ€” $${(attrs.total / 100).toFixed(2)} ${attrs.currency.toUpperCase()}`,
      icon: "๐Ÿ‹",
      notify: true,
      tags: { currency: attrs.currency },
      metadata: { orderNumber: attrs.order_number, email: attrs.user_email, amount: attrs.total },
    });
  } else if (eventName === "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");
}

Environment variables

bash
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx
LEMON_SQUEEZY_WEBHOOK_SECRET=your_webhook_secret

Ready to connect Lemon Squeezy?

7-day free trial. No credit card required.

Start free