npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cashsdk-react

v1.0.0

Published

React bindings for CashSDK web customer sessions and entitlements

Downloads

104

Readme

cashsdk-react

React 18 and 19 bindings for cashsdk-web. Version 1.0.0 is generally available on npm. cashsdk-web is a peer dependency: install the same version of both, so your app and the bindings share one copy.

npm install [email protected] [email protected]

Create one client outside your components, then provide it.

import { createCashSDK } from "cashsdk-web";
import { CashSDKProvider, useEntitlements } from "cashsdk-react";

const cashsdk = createCashSDK({
  publishableKey: "csk_pk_…",
  environment: "sandbox",
  getSessionToken: async ({ signal }) => {
    const response = await fetch("/api/cashsdk/session", { method: "POST", signal });
    if (!response.ok) throw new Error("Sign in to continue");
    return response.json();
  },
});

function AccessStatus() {
  const { status, error, isActive } = useEntitlements();
  if (error?.code === "session_unavailable") return <p>Sign in to see your plan.</p>;
  if (error) return <p role="alert">Could not load access.</p>;
  if (status !== "ready") return <p>Loading access...</p>;
  return <p>{isActive("pro") ? "Pro is active" : "Choose a plan"}</p>;
}

export function App() {
  return <CashSDKProvider client={cashsdk}><AccessStatus /></CashSDKProvider>;
}

Hooks

| Hook | Returns | | --- | --- | | useCustomer() | customer, status, error | | useEntitlements() | snapshot, status, error, isActive(id), refresh() | | useSubscriptions() | subscriptions, status, error, refresh() | | useOfferings() | offerings, status, error, refresh() | | usePurchase() | start(packageId, { successUrl, cancelUrl }), checkout, status, error, reset() | | useCheckout() | start(input, { idempotencyKey }), checkout, status, error, reset(). Prefer usePurchase | | useBillingPortal() | start({ returnUrl }), portal, status, error, reset() | | useCashSDK() / useCashSDKState() | the client, and its whole snapshot |

Every error is a CashSDKError: decide on error.code. A lost connection is network_error with retryable: true; offer a retry that calls refresh(). A failing getSessionToken (nobody is signed in) is session_unavailable. After a failed first load, useEntitlements().refresh() loads the customer and access again.

import { useOfferings, usePurchase } from "cashsdk-react";

export function Plans() {
  const { offerings, status } = useOfferings();
  const purchase = usePurchase();
  if (status !== "ready") return <p>Loading plans...</p>;
  return (
    <>
      {offerings?.current?.packages.map((item) => (
        <button key={item.id} disabled={!item.purchasable || purchase.status === "starting"}
          onClick={async () => {
            const checkout = await purchase.start(item.id, {
              successUrl: `${location.origin}/billing/return?package=${encodeURIComponent(item.id)}`,
              cancelUrl: `${location.origin}/pricing`,
            });
            if (checkout.action) location.assign(checkout.action.url);
          }}>
          {item.product.displayName ?? item.identifier}
        </button>
      ))}
      {purchase.error?.code === "already_subscribed" && <p>You already have this plan.</p>}
      {purchase.checkout?.status === "complete" && <p>Paid. Your access arrives in a moment.</p>}
    </>
  );
}

usePurchase keeps one idempotency key per customer and package for you, and starts a new one once an attempt is over, so buying a plan a second time works. A double click is one purchase; a click on another package while one is starting is refused with purchase_in_progress. On the return page, useCashSDK().waitForPurchase(packageId) waits for the payment, then waitForEntitlement for the access.

Reads follow the signed-in customer: a result read for one customer is never shown for the next, and usePurchase, useCheckout and useBillingPortal drop the previous customer's result and error when the customer changes. start belongs in a click or submit handler, never in an effect.

import { useBillingPortal } from "cashsdk-react";

export function ManageBilling() {
  const portal = useBillingPortal();
  return (
    <>
      <button disabled={portal.status === "starting"}
        onClick={async () => {
          const { url } = await portal.start({ returnUrl: `${location.origin}/account` });
          location.assign(url);
        }}>
        Manage billing
      </button>
      {portal.error?.code === "no_billing_account" && <p>You have no paid plan yet.</p>}
    </>
  );
}

The session needs the billing:manage scope.

Works with the Next.js App Router: the package is marked "use client", server rendering shows an empty state and makes no request, and Strict Mode does not duplicate anything. The provider does not dispose the client: call logout on sign-out and destroy when you are done with it. When another tab of your app signs out, this one is signed out too; when another tab signs someone in, the provider loads them. ES modules only, like cashsdk-web.

Guide: docs.cashsdk.com/sdk/web. License: MIT.