Gate a feature or show an ad

Turn the supporter's contributions into your own rule, such as hiding an ad or unlocking a feature for sponsors.

On this page

Sponsorkit reports facts: every contribution the signed-in person (or one of their organizations) has made to your accounts, per provider, in the provider's currency. It never decides who "counts". That rule is yours, and this guide shows how to write it.

The basic rule#

A common rule is "hide the ad for anyone with an active contribution of at least $5". Ask for the result, then filter it:

ad.ts
import { Sponsorkit } from "@sponsorkit/sdk";

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

const fc = await sk.getFinancialContribution();
const isSupporter = fc.active().some((c) => c.amount.currency === "USD" && c.amount.value >= 500);

if (!isSupporter) {
  document.querySelector<HTMLElement>("#ad")?.removeAttribute("hidden");
}

A few things make this cheap and safe to call wherever you need it:

  • It's cached. The result is stored locally and served from the cache for cacheTtl (one hour by default), so calling it on every page load or every CLI run doesn't hit the network each time. See Caching.
  • It doesn't throw because of the network. If Sponsorkit or a provider is down, you get the cached result marked stale, never an exception. See Handle errors and offline.
  • Signed-out users cost nothing. Without a session there's no network call; the result has signedIn: false and no contributions, so the rule above shows them the ad.

Note

Amounts are in minor units (500 is $5.00) and are never converted. A €5 Open Collective contribution is { value: 500, currency: "EUR" }, so a rule that only checks USD ignores it. Decide which currencies you accept, or accept any active contribution regardless of amount.

Let people sign in#

Nobody is recognized as a supporter until they sign in through your app. Give them a way to do that, for example a "Already a sponsor? Sign in" link next to the ad, or a login command in a CLI. Sign-in returns the fresh result, so you can re-apply your rule straight away:

sign-in.ts
import { Sponsorkit, isSponsorkitError } from "@sponsorkit/sdk";

const sk = new Sponsorkit({ github: "octocat", openCollective: "my-project" });
const ad = document.querySelector<HTMLElement>("#ad");

// Call signIn() directly from the click handler so the popup isn't blocked.
document.querySelector("#sign-in")?.addEventListener("click", async () => {
  try {
    const fc = await sk.signIn();
    if (fc.active().length > 0) ad?.setAttribute("hidden", "");
  } catch (e) {
    if (!isSponsorkitError(e, "cancelled")) console.error("Sign-in failed", e);
  }
});

How sign-in works on each platform is covered in Add a sponsor button to a web app and Use Sponsorkit in a CLI or desktop app.

Writing your own rule#

The helpers only filter; they never add amounts together across providers or currencies. Combine them with plain code:

HelperKeeps
active()Contributions with status active (running recurring sponsorships).
recurring()Monthly and yearly contributions, whatever their status.
oneTime()One-time contributions, whatever their status. A completed one has status paid.
byProvider(p)Contributions on github or open-collective.
direct()Contributions the person made themselves, without those from their organizations.

Method names follow each language's conventions: Active() in .NET and Go, active() in Python and Rust, one_time() / by_provider() in Python and Rust. Some rules you might write:

  • Any support at all: active() is non-empty, or oneTime() has a paid contribution. Note that active() alone excludes one-time contributions, because a completed one-time payment has status paid.
  • Only personal sponsorships: filter direct() instead of all contributions. By default, a sponsorship from an organization the person belongs to counts too; it's marked via: "organization". See Organizations.
  • A tier: compare tier.name (GitHub tiers, when GitHub reports them).
  • Cancelled but still paid up: check status: "cancelled" together with endedAt. All statuses are explained in Statuses.

Warning

GitHub sometimes hides the tier of a sponsorship. Sponsorkit then reports amount: { value: 0, currency: "USD" } and tier: null. A rule like "at least $5" treats that sponsor as $0, so consider accepting any active contribution if you'd rather not miss them.

Client-side checks can be patched#

Anything checked on the user's own machine can be patched out by a determined user. That's usually fine for hiding an ad or a nag. If the feature has real cost to you (a hosted service, API quota, downloads), make the decision on your server instead, with backend verification.