Paddle client utilities
paddle/paddle-clientFreeUtility
Paddle.js initialization singleton, SDK type re-exports, checkout event mapper, and SDK-layer hooks used by all client-side Paddle blocks.
Install it
npx shadcn@latest add https://developer.paddle.com/r/paddle-client.jsonSource
1"use client"23import {4 type Environments,5 type Paddle,6 type PricePreviewParams,7 type PricePreviewResponse,8} from "@paddle/paddle-js"9import { useEffect, useState, useRef, useCallback } from "react"10import {11 formatBillingCycle,12 formatTrialPeriod,13} from "@/registry/new-york/blocks/paddle-helpers/lib/paddle-format"14import { getOrCreatePaddle } from "@/registry/new-york/blocks/paddle-client/lib/paddle-instance"15import type { PriceData } from "@/registry/new-york/blocks/paddle-helpers/lib/paddle-types"1617export type UsePaddlePricesArgs = {18 clientToken: string19 environment?: Environments20 priceIds: string[]21 countryCode?: string22 discountId?: string23}2425type PaddlePrices = Record<string, PriceData>2627function getPriceAmounts(prices: PricePreviewResponse): PaddlePrices {28 return prices.data.details.lineItems.reduce((acc, item) => {29 const hasDiscount = item.discounts.length > 030 acc[item.price.id] = {31 total: item.formattedTotals.total,32 originalTotal: hasDiscount ? item.formattedTotals.subtotal : undefined,33 interval: formatBillingCycle(item.price.billingCycle),34 trialPeriod: item.price.trialPeriod ? formatTrialPeriod(item.price.trialPeriod) : undefined,35 }36 return acc37 }, {} as PaddlePrices)38}3940export function usePaddlePrices(args: UsePaddlePricesArgs): {41 prices: PaddlePrices42 loading: boolean43 error: Error | null44} {45 const { clientToken, environment = "production", priceIds, countryCode, discountId } = args46 const [paddle, setPaddle] = useState<Paddle | null>(null)47 const [prices, setPrices] = useState<PaddlePrices>({})48 const [loading, setLoading] = useState<boolean>(true)49 const [error, setError] = useState<Error | null>(null)5051 // Stable keys — prevent unnecessary re-fetches52 const priceIdsKey = priceIds.join(",")53 const discountIdKey = discountId ?? ""5455 // Initialize Paddle once56 useEffect(() => {57 if (paddle || !clientToken) return5859 getOrCreatePaddle(clientToken, environment)60 .then((paddleInstance) => {61 if (paddleInstance) {62 setPaddle(paddleInstance)63 }64 })65 .catch((err) => {66 setError(err instanceof Error ? err : new Error("Failed to initialize Paddle"))67 setLoading(false)68 })69 }, [clientToken, environment, paddle])7071 // Fetch prices when Paddle is ready or inputs change72 const fetchedRef = useRef<string | null>(null)7374 const fetchPrices = useCallback(async () => {75 if (!paddle) return7677// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from paddle
All 14 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Paddle helperspaddle-helpers | paddle | Utilities | Free | no deps · 4 files |
