Integrations / Payments / Stripe
Connect your Stripe webhook to LogIt and track every charge, subscription, refund, and failed payment in real time.
In your Stripe dashboard, go to Developers โ Webhooks โ Add endpoint.
Point it at https://yourdomain.com/api/webhooks/stripe and select these events:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.deletedinvoice.payment_failedcharge.refundednpm install logit stripe
// app/api/webhooks/stripe/route.ts
import Stripe from "stripe";
import { init } from "logit";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const logit = init({ token: process.env.LOGIT_API_KEY! });
export async function POST(req: Request) {
const body = await req.text();
const sig = req.headers.get("stripe-signature")!;
const event = stripe.webhooks.constructEvent(
body,
sig,
process.env.STRIPE_WEBHOOK_SECRET!
);
switch (event.type) {
case "checkout.session.completed": {
const session = event.data.object;
await logit.now("payments", {
event: "Payment received",
description: `${session.customer_email} โ $${((session.amount_total ?? 0) / 100).toFixed(2)}`,
icon: "๐ฐ",
notify: true,
tags: { currency: session.currency ?? "usd" },
metadata: {
sessionId: session.id,
email: session.customer_email,
amount: session.amount_total,
},
});
break;
}
case "customer.subscription.deleted": {
const sub = event.data.object;
await logit.now("churn", {
event: "Subscription cancelled",
description: `Customer ${sub.customer}`,
icon: "๐ข",
notify: true,
metadata: { subscriptionId: sub.id, customerId: sub.customer },
});
break;
}
case "invoice.payment_failed": {
const invoice = event.data.object;
await logit.now("errors", {
event: "Payment failed",
description: `${invoice.customer_email} โ ${invoice.last_finalization_error?.message ?? "unknown"}`,
icon: "โ",
notify: true,
metadata: { invoiceId: invoice.id, email: invoice.customer_email },
});
break;
}
}
return new Response("ok");
}
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx
STRIPE_SECRET_KEY=sk_live_xxxxxxxxxxxxxxxx
STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxx
Ready to connect Stripe?
7-day free trial. No credit card required.