# lemonsqueezy-webhooks

> lemon-squeezy webhooks types and an utility functions to handle webhooks in Node.js

Latest version **0.1.2** (published 2024-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install lemonsqueezy-webhooks
pnpm add lemonsqueezy-webhooks
yarn add lemonsqueezy-webhooks
bun add lemonsqueezy-webhooks
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2024-07-16 |
| First published | 2023-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 54.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Tommaso De Rossi, morse |
| Maintainers | xmorse |
| Keywords | lemon-squeezy, lemonsqueezy, webhooks |

## Links

- npm: https://www.npmjs.com/package/lemonsqueezy-webhooks
- Repository: https://github.com/remorses/lemonsqueezy-webhooks
- Homepage: https://github.com/remorses/lemonsqueezy-webhooks#readme
- Issues: https://github.com/remorses/lemonsqueezy-webhooks/issues
- npm.io page: https://npm.io/package/lemonsqueezy-webhooks

## Recent versions

- 0.1.2 (latest) — 2024-07-16
- 0.1.1 — 2024-07-16
- 0.1.0 — 2024-07-16
- 0.0.6 — 2023-05-11
- 0.0.5 — 2023-04-24
- 0.0.4 — 2023-04-24
- 0.0.3 — 2023-03-23
- 0.0.2 — 2023-03-23
- 0.0.1 — 2023-03-23
- 0.0.0 — 2023-03-23

## README

## Install

```
npm i lemonsqueezy-webhooks
```

## Usage

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](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions) work inside `onData`.

## Usage in Node.js

```ts
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.

```ts
// 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.

```ts
// 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 webhook
-   `Order`, the `payload.data` type for the events
    -   `order_created`
    -   `order_updated`
    -   `order_deleted`
-   `Subscription`, the `payload.data` type for the events
    -   `subscription_created`
    -   `subscription_cancelled`
    -   `subscription_resumed`
    -   `subscription_expired`
    -   `subscription_paused`
    -   `subscription_unpaused`
-   `SubscriptionInvoice`, the `payload.data` type for the events
    -   `subscription_payment_success`
    -   `subscription_payment_failed`
    -   `subscription_payment_recovered`
-   `LicenseKey`, the `payload.data` type for the events
    -   `license_key_created`

Exported functions

-   `nodejsWebHookHandler`, it handles webhooks signature check and parsing. It also adds a top level `event_name` field to the payload to make [Typescript discriminated unions](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions) work and infer the payload.data type under if blocks inside `onData`.

## Sponsors

[**Notaku**](https://notaku.so)

[![Notaku](https://notaku.so/github_banner.jpg)](https://notaku.so)

---

[Licensed under MIT]().

---
_Source: https://npm.io/package/lemonsqueezy-webhooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
