Package Exports
- lemonsqueezy-webhooks
- lemonsqueezy-webhooks/dist/index.js
- lemonsqueezy-webhooks/esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lemonsqueezy-webhooks) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Install
npm i lemonsqueezy-webhooksUsage
This package exposes the lemon-squeezy webhooks types and an utility functions to handle webhooks in Node.js
nodejsWebHookHandler
Checks the signature of the request body and parses it to a WebhookPayload type.
It also adds a top level event_name field to make Typescript discriminated unions work inside onData.
Usage in Node.js
import { nodejsWebHookHandler } from 'lemonsqueezy-webhooks'
const secret = process.env.LEMON_SQUEEZY_WEBHOOK_SECRET
// ... Express app setup
app.post('/webhooks', async (req, res) => {
await nodejsWebHookHandler({
async onData(payload) {
console.log(payload)
// payload.event_name allows TypeScript to infer the type of payload.data
if (payload.event_name === 'order_created') {
// payload.data is an Order
console.log(payload.data.attributes.status)
}
},
req,
res,
secret,
})
})Usage in Next.js (with Node runtime)
You can also see the source code in the Next.js app example in this repo for a full example.
// api/webhook.ts
import type { NextApiResponse, NextApiRequest } from 'next'
import { nodejsWebHookHandler } from 'lemonsqueezy-webhooks'
export const config = {
api: {
// important! otherwise the body signature check will fail
bodyParser: false,
},
}
const secret = process.env.SECRET
if (!secret) {
throw new Error('SECRET is not set')
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
await nodejsWebHookHandler({
async onData(payload) {
console.log(payload)
if (payload.event_name === 'order_created') {
// payload.data is an Order
console.log(payload.data.attributes.status)
}
},
req,
res,
secret,
})
}Usage in Next.js (with Web Streams)
You can also use the whatwgWebhooksHandler function to handle webhooks in Next.js routes that export a POST and GET handler.
// api/webhook.ts
import { whatwgWebhooksHandler } from 'lemonsqueezy-webhooks'
const secret = process.env.SECRET
if (!secret) {
throw new Error('SECRET is not set')
}
export const POST = (request: Request) => {
return whatwgWebhooksHandler({
async onData(payload) {
console.log(payload)
if (payload.event_name === 'order_created') {
// payload.data is an Order
console.log(payload.data.attributes.status)
}
},
request,
secret,
})
}Exported types:
WebhookPayload, the lemonsqueezy json body of a webhookOrder, thepayload.datatype for the eventsorder_createdorder_updatedorder_deleted
Subscription, thepayload.datatype for the eventssubscription_createdsubscription_cancelledsubscription_resumedsubscription_expiredsubscription_pausedsubscription_unpaused
SubscriptionInvoice, thepayload.datatype for the eventssubscription_payment_successsubscription_payment_failedsubscription_payment_recovered
LicenseKey, thepayload.datatype for the eventslicense_key_created
Exported functions
nodejsWebHookHandler, it handles webhooks signature check and parsing. It also adds a top levelevent_namefield to the payload to make Typescript discriminated unions work and infer the payload.data type under if blocks insideonData.
