SponsorkitClient

The Sponsorkit client: reports what the current user has contributed to your GitHub Sponsors / Open Collective accounts, sends them to contribute, and helps you nag politely.

On this page

The Sponsorkit client: reports what the current user has contributed to your GitHub Sponsors / Open Collective accounts, sends them to contribute, and helps you nag politely.

namespace Sponsorkit;

public sealed class SponsorkitClient

Instances are thread-safe. Several instances (and several processes) may share a storage directory.

Example#

var sk = new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "my-cli" });
var fc = await sk.GetFinancialContributionAsync();
if (fc.SignedIn && fc.Active().Count == 0) Console.Error.WriteLine($"Support my-cli: {sk.CheckoutUrl()}");

Constructors#

MemberDescription
SponsorkitClient(SponsorkitOptions)Creates a client.

SponsorkitClient(SponsorkitOptions)#

public SponsorkitClient(SponsorkitOptions options)

Creates a client. Validates the configuration synchronously and performs no I/O.

Parameters

Exceptions

  • SponsorkitError: invalid_config: no targets, a malformed target, a malformed apiBaseUrl, siteBaseUrl or appName, a non-positive SignInTimeout, a negative RequestTimeout, or an unknown mock preset.
  • ArgumentNullException: options is null.

Properties#

MemberDescription
TargetsThe canonical target strings, for example github:octocat.
TargetsHashThe targets_hash that keys this target set's local storage.
IsDisabledWhether Sponsorkit is disabled through SPONSORKIT_DISABLE.
IsMockWhether mock mode is active.
ApiBaseUrlThe Sponsorkit API origin in use (normalized, no trailing slash).
SiteBaseUrlThe Sponsorkit site base URL in use for page URLs (normalized, no trailing slash).

Targets#

public IReadOnlyList<string> Targets { get; }

The canonical target strings, for example github:octocat.

Type: IReadOnlyList<string>

TargetsHash#

public string TargetsHash { get; }

The targets_hash that keys this target set's local storage.

Type: string

IsDisabled#

public bool IsDisabled { get; }

Whether Sponsorkit is disabled through SPONSORKIT_DISABLE.

Type: bool

IsMock#

public bool IsMock { get; }

Whether mock mode is active.

Type: bool

ApiBaseUrl#

public string ApiBaseUrl { get; }

The Sponsorkit API origin in use (normalized, no trailing slash).

Type: string

SiteBaseUrl#

public string SiteBaseUrl { get; }

The Sponsorkit site base URL in use for page URLs (normalized, no trailing slash).

Type: string

Methods#

MemberDescription
ResetProcessState()Forgets per-process state: which nag runs were already counted, the dev-time target check, and any Retry-After backoff.
IsSignedIn()Whether a supporter is signed in (a session token is stored).
GetFinancialContributionAsync(CancellationToken)Returns what the signed-in supporter has contributed to the targets, served from the local cache when it is younger than SponsorkitOptions.CacheTtl.
GetFinancialContributionAsync(TimeSpan, CancellationToken)Like GetFinancialContributionAsync, with a per-call cache TTL.
SignInAsync(SignInOptions, CancellationToken)Signs the supporter in (loopback or paste-back).
CheckoutUrl(CheckoutUrlOptions)Returns an anonymous, offline-safe link to the Sponsorkit checkout page (no sign-in, no PKCE challenge).
OpenCheckoutAsync(CheckoutOptions, CancellationToken)Sends the supporter to contribute and waits for the new contribution.
SignOutAsync(CancellationToken)Signs out locally: deletes the stored session token and cache, keeping nag state.
Nag(NagOptions)Creates a nag helper.

ResetProcessState()#

public static void ResetProcessState()

Forgets per-process state: which nag runs were already counted, the dev-time target check, and any Retry-After backoff. Call it to simulate a new run of the app in tests.

IsSignedIn()#

public bool IsSignedIn()

Whether a supporter is signed in (a session token is stored). Performs no network access.

Returns bool

GetFinancialContributionAsync(CancellationToken)#

public Task<ContributionResult> GetFinancialContributionAsync(CancellationToken cancellationToken = default)

Returns what the signed-in supporter has contributed to the targets, served from the local cache when it is younger than SponsorkitOptions.CacheTtl.

Parameters

  • cancellationToken CancellationToken: Cancels the request. Optional (default default).

Returns Task<ContributionResult> (ContributionResult): The result. When signed out it has SignedIn: false and no network call is made. When the API is unreachable, failing or rate limited, the cached result is returned with Stale: true (or an empty signed-in result if nothing is cached). This method never throws for network or server problems.

Exceptions

GetFinancialContributionAsync(TimeSpan, CancellationToken)#

public Task<ContributionResult> GetFinancialContributionAsync(TimeSpan cacheTtl, CancellationToken cancellationToken = default)

Like GetFinancialContributionAsync, with a per-call cache TTL. TimeSpan.Zero always asks the API (unless a Retry-After backoff is in force).

Parameters

  • cacheTtl TimeSpan: The maximum age of a cached result to serve.
  • cancellationToken CancellationToken: Cancels the request. Optional (default default).

Returns Task<ContributionResult> (ContributionResult): The result; see GetFinancialContributionAsync.

SignInAsync(SignInOptions, CancellationToken)#

public Task<ContributionResult> SignInAsync(SignInOptions? options = null, CancellationToken cancellationToken = default)

Signs the supporter in (loopback or paste-back). While already signed in, this adds another identity (for example new SignInOptions { Provider = "open-collective" }) to the session.

Parameters

  • options SignInOptions?: Provider and sign-in method overrides. Optional (default null).
  • cancellationToken CancellationToken: Cancels the sign-in; it then throws cancelled. Optional (default default).

Returns Task<ContributionResult> (ContributionResult): The contribution result returned with the new session.

Exceptions

  • SponsorkitError: disabled; invalid_config (including when the new session could not be written to storage); access_denied, invalid_state or provider_error from the provider; invalid_code (the API rejected the code); cancelled (the cancellationToken fired or no paste-back code was entered); timeout (not finished within SponsorkitOptions.SignInTimeout); network_error (the API was unreachable or answered 5xx, 429 or an unexpected response).

CheckoutUrl(CheckoutUrlOptions)#

public string CheckoutUrl(CheckoutUrlOptions? options = null)

Returns an anonymous, offline-safe link to the Sponsorkit checkout page (no sign-in, no PKCE challenge). Suits nags, READMEs and --help output.

Parameters

  • options CheckoutUrlOptions?: Preferred provider, amount (minor units), currency and frequency. Optional (default null).

Returns string: An absolute URL such as https://ffmathy.github.io/sponsorkit/checkout/?targets=github%3Aoctocat&frequency=monthly (on SiteBaseUrl).

Exceptions

  • SponsorkitError: invalid_config: an unconfigured provider, or an invalid frequency, amount or currency.

OpenCheckoutAsync(CheckoutOptions, CancellationToken)#

public Task<CheckoutResult> OpenCheckoutAsync(CheckoutOptions? options = null, CancellationToken cancellationToken = default)

Sends the supporter to contribute and waits for the new contribution. Signs in first when needed (in the same browser flow), opens the checkout page, then polls every SponsorkitOptions.CheckoutPollInterval until a new or changed contribution appears.

Parameters

  • options CheckoutOptions?: Checkout preferences and the polling time limit (default 10 minutes). Optional (default null).
  • cancellationToken CancellationToken: Stops signing in or waiting, in either phase; the result is then CheckoutStatus.Unknown. Optional (default default).

Returns Task<CheckoutResult> (CheckoutResult): CheckoutStatus.Contributed with the contribution, or CheckoutStatus.Unknown when polling times out or on cancellation.

Exceptions

SignOutAsync(CancellationToken)#

public Task SignOutAsync(CancellationToken cancellationToken = default)

Signs out locally: deletes the stored session token and cache, keeping nag state. There is no server-side sign-out; the supporter can revoke Sponsorkit's access at the provider.

Parameters

Returns Task: A task that has already completed when the method returns.

Exceptions

Nag(NagOptions)#

public SponsorkitNag Nag(NagOptions options)

Creates a nag helper. See SponsorkitNag.

Parameters

  • options NagOptions: The nag id and how often it may be shown.

Returns SponsorkitNag: The helper.