Reading contributions in JavaScript / TypeScript

getFinancialContribution(), the FinancialContribution result and its filter helpers, caching, and gating features.

On this page

getFinancialContribution() answers "what has the current user contributed to my targets?". It is safe to call on every start-up or page load:

  • Signed out: it returns a signed-out result without any network request.
  • Fresh cache: it returns the cached result (fromCache: true) without a request. The cache lasts cacheTtl seconds (default 3600).
  • Otherwise it asks the Sponsorkit API and caches the answer.
  • Expired or revoked session: the SDK deletes the token and cache and returns a signed-out result.
  • Never throws for network or server problems. If the API can't be reached, times out, or answers with a 5xx or 429, it returns the last cached result marked stale: true, or a signed-in empty result marked stale: true when there is no cache. After a 429 it honours Retry-After before calling the API again.

The details are in Caching and the error contract.

import { Sponsorkit } from "@sponsorkit/sdk";

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

const fc = await sk.getFinancialContribution();
if (fc.active().length > 0) console.log("Thanks for your support!");

// Bypass the cache, e.g. right after the user says "I just sponsored":
const fresh = await sk.getFinancialContribution({ cacheTtl: 0 });
console.log(fresh.fromCache); // false, unless the API couldn't be reached

The result#

The promise resolves to a FinancialContribution: the plain result object every Sponsorkit SDK returns, plus helper methods. JSON.stringify(fc) gives exactly the protocol shape, so you can hand it to your own API or store it.

FieldTypeMeaning
signedInbooleanA session exists.
disabledbooleanSPONSORKIT_DISABLE is set; everything else is empty.
mockbooleanThe result comes from mock mode.
targetsstring[]Canonical targets, e.g. ["github:octocat"].
supporterSupporter | nullThe signed-in supporter: a pairwise id and their linked identities.
contributionsContribution[]Every contribution to your targets, in any status.
fetchedAtstring | nullWhen the API produced the data (ISO 8601).
stalebooleanSome or all data could not be refreshed and comes from the cache.
fromCachebooleanThe whole result was served from the local cache.
unavailableProvider[] (optional)Providers the API could not query this time. Their contributions, if cached, are filled in from the cache.

Each Contribution has, among others, provider, target, status (active, paused, cancelled, paid, refunded, disputed, failed or pending), kind (recurring or one_time), frequency (one_time, monthly or yearly), amount ({ value, currency } in minor units), via (direct or organization), sponsor, tier and dates. What each status means: Statuses.

Filter helpers#

The helpers only filter. They never add amounts across providers or currencies, because a sum of USD and EUR amounts, or of a monthly and a one-time contribution, isn't meaningful. Decide what counts for your app.

HelperReturns
fc.active()Contributions with status active.
fc.recurring()Recurring contributions, any status.
fc.oneTime()One-time contributions, any status.
fc.byProvider("github")Contributions on one provider.
fc.direct()Contributions the user made themselves (excludes sponsorships paid by an organization they belong to).
fc.toJSON()The plain ContributionResult object.

Gating a feature#

A typical check: "an active recurring contribution of at least $5 a month". Compare within one currency, and decide whether organization sponsorships count:

import type { Contribution } from "@sponsorkit/sdk";
import { Sponsorkit } from "@sponsorkit/sdk";

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

function qualifies(c: Contribution): boolean {
  const monthly = c.frequency === "monthly" ? c.amount.value : c.frequency === "yearly" ? c.amount.value / 12 : 0;
  return c.amount.currency === "USD" && monthly >= 500;
}

const fc = await sk.getFinancialContribution();
const isSupporter = fc.active().some(qualifies);

Running this check in the browser is fine for cosmetic perks (hiding a banner, a badge). Anything a user could gain from bypassing it belongs on your server: see Backend verification.

Stale results#

stale: true means Sponsorkit couldn't confirm the data just now. The SDK still returns what it knew last, so a supporter whose perks depend on it isn't locked out by an outage. Usually you can ignore stale; show a hint if your UI displays contribution details. Read unavailable to see which provider was down.

Cache lifetime#

cacheTtl is set per client (minimum 60 seconds) and can be overridden per call. The cache is shared by every client with the same targets and storage, so a second tab or a second process reuses it.

import { Sponsorkit } from "@sponsorkit/sdk/node";

const sk = new Sponsorkit({ github: "octocat", cacheTtl: 6 * 3600 }); // a CLI can cache for hours

const fc = await sk.getFinancialContribution({ cacheTtl: 0 }); // always ask the API this time

signIn() and a completed openCheckout() refresh the cache themselves, so you don't need cacheTtl: 0 after them.