Lemon squeezy

From TerminalSkills/skills · View interactive version

Lemon squeezy is an AI agent skill from the GitHub repository TerminalSkills/skills. Lemon Squeezy is a payments platform that acts as Merchant of Record for software, subscriptions and digital products, handling VAT and sales tax, invoices, license keys and a customer portal for you. Use this skill to create checkouts with the @lemonsqueezy/lemonsqueezy.js SDK, verify webhook signa

How to use this skill

Skill file

Lemon Squeezy

Overview

Lemon Squeezy is the seller of record for your sales: it collects and remits VAT and sales tax, issues invoices, runs fraud checks, hosts the checkout and a customer portal, and pays you out. You integrate through a JSON:API REST API (https://api.lemonsqueezy.com/v1), the official SDK @lemonsqueezy/lemonsqueezy.js (4.x, Node 20+), and signed webhooks. License keys have their own unauthenticated License API.

Server-side only: the API key gives full access to your stores, so never ship the SDK to the browser.

Instructions

  1. Install and create a key in Settings > API (keys made in test mode only touch test data):
npm install @lemonsqueezy/lemonsqueezy.js
  1. Call lemonSqueezySetup({ apiKey: process.env.LEMONSQUEEZY_API_KEY }) once at startup.
  2. Every SDK call resolves to { statusCode, error, data } and does not throw on API errors. data is the raw JSON:API body, so the resource is at data.data and its fields at data.data.attributes. Check error first.
  3. createCheckout(storeId, variantId, options) takes the store and variant IDs as positional arguments, then checkoutData, checkoutOptions, productOptions, testMode, expiresAt. Put your own user ID in checkoutData.custom. enabledVariants is an array of numeric variant IDs. The dark checkout option is deprecated; use the colour options.
  4. Webhooks: create one in the dashboard (or createWebhook) with a signing secret and the events you need. At minimum order_created, subscription_created, subscription_updated, subscription_payment_success; add license_key_created for license keys. Lemon Squeezy POSTs JSON with X-Event-Name and X-Signature headers and expects a 200; otherwise it retries up to three more times with exponential backoff.
  5. Verify X-Signature as the HMAC-SHA256 hex digest of the raw request body (not re-serialised JSON), compared with crypto.timingSafeEqual.
  6. Event name is meta.event_name; your checkout custom data arrives in meta.custom_data; the resource is data (data.id, data.attributes). Subscription status values: on_trial, active, paused, past_due, unpaid, cancelled, expired.
  7. License API (activateLicense, validateLicense, deactivateLicense) needs no API key, so it is safe to call from a desktop app. Activation returns an instance.id to store and pass to later validations.

Examples

Checkout link for a signed-in user

import { lemonSqueezySetup, createCheckout } from "@lemonsqueezy/lemonsqueezy.js";

lemonSqueezySetup({ apiKey: process.env.LEMONSQUEEZY_API_KEY! });

export async function proCheckoutUrl(userId: string, email: string) {
  const { data, error } = await createCheckout(
    process.env.LEMONSQUEEZY_STORE_ID!,
    process.env.LEMONSQUEEZY_PRO_VARIANT_ID!,
    {
      checkoutData: { email, custom: { user_id: userId } },
      productOptions: { redirectUrl: "https://app.northwind.dev/billing/success" },
    },
  );
  if (error) throw new Error(`Checkout failed: ${error.message}`);
  return data.data.attributes.url; // redirect the browser here
}

Result: a hosted checkout URL like https://northwind.lemonsqueezy.com/checkout/custom/....

Webhook endpoint (Express)

import crypto from "node:crypto";
import express from "express";

const app = express();

app.post("/api/webhooks/lemonsqueezy", express.raw({ type: "application/json" }), async (req, res) => {
  const digest = Buffer.from(
    crypto.createHmac("sha256", process.env.LEMONSQUEEZY_WEBHOOK_SECRET!).update(req.body).digest("hex"),
    "utf8",
  );
  const signature = Buffer.from(req.get("X-Signature") ?? "", "utf8");
  if (digest.length !== signature.length || !crypto.timingSafeEqual(digest, signature)) {
    return res.status(401).json({ error: "Invalid signature" });
  }

  const payload = JSON.parse(req.body.toString("utf8"));
  const { event_name, custom_data } = payload.meta;
  const { id, attributes } = payload.data;

  switch (event_name) {
    case "subscription_created":
    case "subscription_updated":
      await db.users.update(custom_data?.user_id, {
        lemonSubscriptionId: id,
        subscriptionStatus: attributes.status,
        renewsAt: attributes.renews_at,
        endsAt: attributes.ends_at,
      });
      break;
    case "license_key_created":
      await db.licenses.create({ key: attributes.key, userId: custom_data?.user_id });
      break;
  }
  res.json({ received: true });
});

Result: curl with a wrong signature gets 401; a real subscription_created updates the user and returns {"received":true}.

Activate and validate a license in a desktop app

import os from "node:os";
import { activateLicense, validateLicense } from "@lemonsqueezy/lemonsqueezy.js";

const key = "38b1460a-5104-4067-a91d-77b872934d51";

const { data: act } = await activateLicense(key, `${os.hostname()}-${os.platform()}`);
if (!act?.activated) throw new Error(act?.error ?? "Activation failed");
const instanceId = act.instance!.id; // persist locally

const { data: check } = await validateLicense(key, instanceId);
console.log(check?.valid, `${check?.license_key.activation_usage}/${check?.license_key.activation_limit}`);

Result: true 1/3 for a key with a three-device limit.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.