Sponsorkit for JavaScript / TypeScript

Install and use @sponsorkit/sdk in browsers, Node.js, Bun, Deno and Cloudflare Workers.

On this page

@sponsorkit/sdk tells your app what the current user has contributed to your GitHub Sponsors and Open Collective accounts, and sends them to contribute. It is written in TypeScript, ships ESM and CommonJS builds with self-contained type declarations, and has no runtime dependencies.

Requirements#

RuntimeSupportedNotes
BrowsersEvergreen browsersNeeds fetch, Web Crypto (crypto.subtle, so a secure context: https or localhost) and localStorage (falls back to memory).
Node.js20 or laterengines.node is >=20.
BunCurrent releasesUses the node entry point.
DenoVia npm: specifiersUses the node entry point for CLIs, the root entry point for web code.
Cloudflare Workers and other edge runtimesYesUse @sponsorkit/sdk/server for backend verification.

TypeScript users need nothing extra: the type declarations are part of the package (.d.ts for ESM, .d.cts for CommonJS) and work with moduleResolution set to bundler, node16 or nodenext.

Install#

@sponsorkit/sdk is on npm. Install it with your package manager:

npm install @sponsorkit/sdk

Pick an entry point#

The package has four entry points. They share the same options, result shape, error codes and storage format; they differ in how sign-in works and where state is kept.

ImportUse it forSign-inState lives in
@sponsorkit/sdkWeb apps and anything that runs in a browserPopup, with an optional redirect fallbacklocalStorage
@sponsorkit/sdk/nodeCLIs, desktop apps and other programs one person runs (Node, Bun, Deno)Loopback redirect to the system browser, or paste-backA file in the per-user config directory
@sponsorkit/sdk/serverYour backend, verifying a session token sent by your frontendNone (verifySession())Nothing
@sponsorkit/sdk/elementsPlain HTML pages<sponsorkit-button> and <sponsorkit-badge>localStorage

Each entry point also re-exports the shared types and helpers, so you import everything from one place.

A first look#

app.ts
import { isSponsorkitError, Sponsorkit } from "@sponsorkit/sdk";

const sk = new Sponsorkit({ github: "octocat", openCollective: "my-collective" });

// Never throws because of the network: falls back to the cached result.
const fc = await sk.getFinancialContribution();
console.log(fc.signedIn ? `${fc.active().length} active contribution(s)` : "Signed out");

// signIn() and openCheckout() open a popup, so call them straight from a click handler.
document.querySelector("#sponsor")?.addEventListener("click", async () => {
  try {
    const result = await sk.openCheckout({ frequency: "monthly", amount: 500 });
    if (result.status === "contributed") console.log("Thank you!", result.contribution.amount);
  } catch (e) {
    // "cancelled": the user closed the popup before signing in.
    if (!isSponsorkitError(e, "cancelled")) console.error(e);
  }
});

In this section#

PageWhat it covers
QuickstartA browser app and a Node CLI, end to end.
ConfigurationEvery constructor option with its default, and the environment variables.
Sign-inPopup and redirect sign-in in browsers; loopback and paste-back on Node, Bun and Deno.
ContributionsgetFinancialContribution(), the result, its filter helpers, and caching.
CheckoutopenCheckout(), checkoutUrl() and the web components.
NagAsking for support every N runs or N days.
TestingMock mode, injecting fetch, storage and a clock, and the fake server.
Backend verificationChecking a supporter on your server with verifySession().
Errors and cancellationSponsorkitError, its codes, and AbortSignal.
Platform notesBundlers, SSR, CommonJS, Content Security Policy, storage locations.

The API reference lists every class, method, option and type, generated from the TypeScript source.