Quickstart

Build a small CLI that checks for a sponsorship, signs supporters in and opens the checkout, in about three minutes.

On this page

This page builds my-cli, a tiny command-line program with three behaviours:

  • my-cli checks whether the current user sponsors you and, if not, prints a link to contribute;
  • my-cli login signs the user in with GitHub or Open Collective;
  • my-cli sponsor opens the checkout and waits until the new contribution shows up.

Pick your language below; the choice is remembered across the docs. Every program on this page compiles as-is against the SDK.

Install#

Every SDK is on its package registry; Install lists the other ways to add it.

mkdir my-cli && cd my-cli && npm init -y
npm install @sponsorkit/sdk

To try an unreleased change instead, build from source covers Git dependencies, local checkouts and pinning a commit.

Write the program#

Replace octocat and my-collective with the accounts you receive sponsorships on. You need at least one of the two.

Works in Node 20+, Bun and Deno. The file is plain JavaScript and also valid TypeScript.

my-cli.mjs
import { Sponsorkit, SponsorkitError } from "@sponsorkit/sdk/node";

const sk = new Sponsorkit({
  github: "octocat",               // your GitHub Sponsors login
  openCollective: "my-collective", // and/or your Open Collective slug
  appName: "my-cli",               // shown on the consent screen
});

const command = process.argv[2];

try {
  if (command === "login") {
    // Opens the browser (or prints a link over SSH) and waits for the supporter.
    const fc = await sk.signIn();
    console.log(`Signed in. Active contributions: ${fc.active().length}`);
  } else if (command === "sponsor") {
    // Signs in if needed, opens the checkout, then waits for the new contribution.
    const result = await sk.openCheckout({ frequency: "monthly", amount: 500 });
    console.log(result.status === "contributed" ? "Thank you!" : "No new contribution detected yet.");
  } else {
    // Never throws because of the network: it serves the local cache instead.
    const fc = await sk.getFinancialContribution();
    const supporter = fc.active().some((c) => c.amount.currency === "USD" && c.amount.value >= 500);
    if (!supporter) {
      console.error(`Enjoying my-cli? Support it: ${sk.checkoutUrl({ frequency: "monthly" })}`);
      if (!fc.signedIn) console.error("Already a sponsor? Run `my-cli login`.");
    }
  }
} catch (e) {
  // Sign-in (in `login`, or in `sponsor` when signed out) didn't complete.
  if (!(e instanceof SponsorkitError)) throw e;
  if (e.code === "cancelled" || e.code === "timeout") {
    console.error("Sign-in was cancelled or timed out.");
  } else if (e.code === "network_error") {
    console.error(`Sponsorkit is unreachable; try again in ${e.retryAfter ?? 60} s.`);
  } else {
    console.error(`Sponsorkit error (${e.code}): ${e.message}`); // access_denied, invalid_code, disabled, ...
  }
  process.exitCode = 1;
}
Terminal
node my-cli.mjs          # or: bun my-cli.mjs
node my-cli.mjs login

In a browser app, import from @sponsorkit/sdk instead and call signIn() from a click handler: sign-in then uses a popup, and appName isn't needed because the consent screen shows your site's origin.

Try it without signing in#

Mock mode returns realistic data without touching the network or the disk, in every SDK. Set SPONSORKIT_MOCK and run the program as above:

Terminal
SPONSORKIT_MOCK=signed-out node my-cli.mjs   # or: dotnet run, python my_cli.py, go run ., cargo run
SPONSORKIT_MOCKThe supportermy-cli prints
signed-outNot signed inThe link and the login hint
noneSigned in, no contributionsThe link
active-monthlySigned in, an active $5/month sponsorshipNothing

In mock mode login and sponsor succeed immediately, and sponsor simulates a new contribution. The mock mode guide lists every preset and shows how to pass your own contributions in tests.

What happened#

  • No network when signed out. The plain my-cli run answers instantly for users who never signed in.
  • One sign-in. login showed a consent screen that names your accounts and my-cli (labelled "says it is", because a native app can't prove its name). The session token is stored in the user's config directory; see Sign-in & sessions.
  • Sign-in can fail. login and sponsor (when signed out) raise an error if the supporter closes the window, enters no code, declines, takes longer than signInTimeout (5 minutes), or the API is unreachable; the program reports it and exits with status 1. The plain check only fails on a bad configuration. See the error contract.
  • Cheap checks. Later runs are answered from the local cache for an hour; see Caching & freshness.
  • Your rule. "At least $5 a month on GitHub" is just one example. The result has every contribution's provider, amount, frequency and status; see the contribution shape.

Next steps#