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
- Example
- Constructors
- SponsorkitClient(SponsorkitOptions)
- Properties
- Targets
- TargetsHash
- IsDisabled
- IsMock
- ApiBaseUrl
- SiteBaseUrl
- Methods
- ResetProcessState()
- IsSignedIn()
- GetFinancialContributionAsync(CancellationToken)
- GetFinancialContributionAsync(TimeSpan, CancellationToken)
- SignInAsync(SignInOptions, CancellationToken)
- CheckoutUrl(CheckoutUrlOptions)
- OpenCheckoutAsync(CheckoutOptions, CancellationToken)
- SignOutAsync(CancellationToken)
- Nag(NagOptions)
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 SponsorkitClientInstances 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#
| Member | Description |
|---|---|
SponsorkitClient(SponsorkitOptions) | Creates a client. |
SponsorkitClient(SponsorkitOptions)#
public SponsorkitClient(SponsorkitOptions options)Creates a client. Validates the configuration synchronously and performs no I/O.
Parameters
optionsSponsorkitOptions: The configuration. At least one ofSponsorkitOptions.GitHubandSponsorkitOptions.OpenCollectiveis required.
Exceptions
SponsorkitError:invalid_config: no targets, a malformed target, a malformedapiBaseUrl,siteBaseUrlorappName, a non-positiveSignInTimeout, a negativeRequestTimeout, or an unknown mock preset.ArgumentNullException:optionsisnull.
Properties#
| Member | Description |
|---|---|
Targets | The canonical target strings, for example github:octocat. |
TargetsHash | The targets_hash that keys this target set's local storage. |
IsDisabled | Whether Sponsorkit is disabled through SPONSORKIT_DISABLE. |
IsMock | Whether mock mode is active. |
ApiBaseUrl | The Sponsorkit API origin in use (normalized, no trailing slash). |
SiteBaseUrl | The 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#
| Member | Description |
|---|---|
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
cancellationTokenCancellationToken: Cancels the request. Optional (defaultdefault).
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
SponsorkitError:invalid_configfrom the dev-time target existence check.OperationCanceledException: ThecancellationTokenwas cancelled.
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
cacheTtlTimeSpan: The maximum age of a cached result to serve.cancellationTokenCancellationToken: Cancels the request. Optional (defaultdefault).
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
optionsSignInOptions?: Provider and sign-in method overrides. Optional (defaultnull).cancellationTokenCancellationToken: Cancels the sign-in; it then throwscancelled. Optional (defaultdefault).
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_stateorprovider_errorfrom the provider;invalid_code(the API rejected the code);cancelled(thecancellationTokenfired or no paste-back code was entered);timeout(not finished withinSponsorkitOptions.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
optionsCheckoutUrlOptions?: Preferred provider, amount (minor units), currency and frequency. Optional (defaultnull).
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
optionsCheckoutOptions?: Checkout preferences and the polling time limit (default 10 minutes). Optional (defaultnull).cancellationTokenCancellationToken: Stops signing in or waiting, in either phase; the result is thenCheckoutStatus.Unknown. Optional (defaultdefault).
Returns Task<CheckoutResult> (CheckoutResult): CheckoutStatus.Contributed with the contribution, or CheckoutStatus.Unknown when polling times out or on cancellation.
Exceptions
SponsorkitError:disabled,invalid_config, or a sign-in error (includingtimeoutwhen the sign-in phase exceedsSponsorkitOptions.SignInTimeout).
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
cancellationTokenCancellationToken: Checked once on entry: an already-cancelled token throwsOperationCanceledException. The call is otherwise synchronous. Optional (defaultdefault).
Returns Task: A task that has already completed when the method returns.
Exceptions
SponsorkitError:invalid_config: the storage could not be written.OperationCanceledException: ThecancellationTokenwas already cancelled when the method was called.
Nag(NagOptions)#
public SponsorkitNag Nag(NagOptions options)Creates a nag helper. See SponsorkitNag.
Parameters
optionsNagOptions: The nag id and how often it may be shown.
Returns SponsorkitNag: The helper.