Skip to content

team-gpt/paddle-billing-sdk

Repository files navigation

Paddle Billing SDK

Unofficial Paddle Billing API SDK for Node.js runtime

See https://developer.paddle.com/api-reference/overview

Official Paddle Billing SDK

See https://github.com/PaddleHQ/paddle-node-sdk/

Getting Started

Setup

npm i @team-gpt/paddle-billing-sdk
yarn add @team-gpt/paddle-billing-sdk
bun add @team-gpt/paddle-billing-sdk

Peer dependencies

Axios - https://axios-http.com/

npm i axios
yarn add axios

Endpoints

  • Prices
  • Products
  • Customer
  • Discounts
  • Addresses
  • Businesses
  • Transactions
  • Subscriptions
  • Adjustments
  • Event Types
  • Events
  • Notifications

Usage

Authentication

Create an apikey from the Authentication page in Paddle platform:

PADDLE_AUTH_SECRET=
NEXT_PUBLIC_PADDLE_VENDOR_ID=
NEXT_PUBLIC_PADDLE_SANDBOX=

Paddle Client

import { PaddleClient } from '@team-gpt/paddle-billing-sdk'

/**
 * @see https://developer.paddle.com/api-reference/overview
 */
export const paddleClient = new PaddleClient({
  authToken: process.env.PADDLE_AUTH_SECRET || 'MISSING',
  vendorId: Number(process.env.NEXT_PUBLIC_PADDLE_VENDOR_ID),
  sandbox: Boolean(process.env.NEXT_PUBLIC_PADDLE_SANDBOX),
})

Webhooks

Usage with Next.js API handlers

// /pages/api/webhooks/paddle-events.ts

import { WebhookEvents, signatureHeader } from 'paddle-billing-sdk'
import { NextApiRequest, NextApiResponse } from 'next'

const webhookSecret = process.env.PADDLE_WEBHOOK_SECRET

export const config = {
  api: {
    bodyParser: false,
  },
}

const handler = async function (req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') {
    res.setHeader('Allow', 'POST')
    res.status(405).end('Method Not Allowed')
    return
  }

  if (!req.headers[signatureHeader] || typeof req.headers[signatureHeader] !== 'string') {
    res.status(400).end('Invalid signature')
    return
  }

  try {
    const sig = req.headers[signatureHeader]
    const events = new WebhookEvents(sig, webhookSecret)
    const buf = await WebhookEvents.buffer(req)
    const event = events.constructEvent(buf)
    console.log(event)
  } catch (error) {
    console.log(error)
    if (error instanceof Error) {
      res.status(400).json({ error: error.message })
    }
  }
}

Receive webhooks

  1. Create an account in https://ngrok.com/

  2. Expose your local server

    ngrok http 3000
  3. Add the exposed server to Paddle Notifications at https://sandbox-vendors.paddle.com/notifications

    https://xxx-xx-xxx-xxx-xx.ngrok-free.app/api/webhooks/paddle-events
  4. Send a request

TypeError: adapter is not a function

If you're getting this error while using the client then you need to install axios as a dependency:

npm i axios
yarn add axios
bun add axios

Extend custom data

To extend the default custom data interfaces add the following to your codebase

// Custom interfaces for metadata
declare module '@team-gpt/paddle-billing-sdk' {
  export interface PriceMetadata {
    myKey: string
  }
  export interface ProductMetadata {
    myKey: string
  }
  export interface CustomerMetadata {
    myKey: string
  }
  export interface TransactionMetadata {
    myKey: string
  }
  export interface SubscriptionMetadata {
    myKey: string
  }
}

Testing

bun test

License

MIT