Configuring the .NET SDK

Every SponsorkitOptions property with its default, how the constructor validates them, and the environment variables the SDK reads.

On this page

All configuration goes into a SponsorkitOptions object passed to the SponsorkitClient constructor. The constructor validates everything synchronously, does no I/O, and throws SponsorkitError with code invalid_config when something is wrong, so configuration mistakes show up the first time your app starts.

Program.cs
using Sponsorkit;

var sk = new SponsorkitClient(new SponsorkitOptions
{
    GitHub = "octocat",
    OpenCollective = "my-collective",
    AppName = "my-cli",
    CacheTtl = TimeSpan.FromHours(6),            // fewer requests; contributions rarely change
    SignInMethod = SignInMethod.Auto,            // loopback, falling back to paste-back
    SignInTimeout = TimeSpan.FromMinutes(3),
    RequestTimeout = TimeSpan.FromSeconds(10),
});

Console.WriteLine(string.Join(", ", sk.Targets)); // github:octocat, open-collective:my-collective

Options#

PropertyTypeDefaultWhat it does
GitHubstring?nullYour GitHub Sponsors login (user or organization).
OpenCollectivestring?nullYour Open Collective slug. At least one of GitHub and OpenCollective is required.
AppNamestring?nullShown on the consent page as "says it is". 1–64 characters after trimming.
CacheTtlTimeSpan1 hourHow long a result is served from the local cache. Values below 60 seconds are raised to 60 seconds.
MockSponsorkitMock?nullMock mode: a preset name such as "active-monthly" or SponsorkitMock.WithContributions(...). Wins over SPONSORKIT_MOCK. See testing.
StorageDirectorystring?per-user config folderWhere the state file lives. See platform notes.
StorageISponsorkitStorage?nullA custom storage backend. Takes precedence over StorageDirectory.
ApiBaseUrlstring?https://api.sponsorkit.ioThe API origin, used for every API call, for self-hosting and tests. Must be an absolute http(s) URL; trailing slashes are removed. An empty string counts as unset.
SiteBaseUrlstring?https://ffmathy.github.io/sponsorkit, or ApiBaseUrl when only that is setThe site base URL (origin plus optional path), used for the /connect/ and /checkout/ pages, CheckoutUrl and the loopback checkout redirect. Must be an absolute http(s) URL; trailing slashes are removed. An empty string counts as unset.
SignInMethodSignInMethodAutoAuto, Loopback or Paste. See sign-in.
OpenBrowserFunc<string, CancellationToken, Task>?system browserOpens the sign-in or checkout URL. Throwing counts as "the browser could not open".
PromptForCodeFunc<string, CancellationToken, Task<string>>?reads a line from stdinAsks for the paste-back code; receives the URL. An empty answer throws cancelled.
SignInTimeoutTimeSpan5 minutesHow long a sign-in may take before it throws timeout. Must be positive or Timeout.InfiniteTimeSpan.
Interactivebool?null (detect)Overrides the nag's terminal check. By default the terminal is interactive when neither stdout nor stderr is redirected.
EnvironmentIReadOnlyDictionary<string, string>?null (process environment)Replaces the environment variables below for this instance. It is not merged: a missing key counts as unset.
DevChecksbool?nullChecks once per process that your targets exist. null means "only when SPONSORKIT_DEV_CHECKS is set".
CheckoutPollIntervalTimeSpan60 secondsHow often OpenCheckoutAsync polls for the new contribution.
ClockFunc<DateTimeOffset>?DateTimeOffset.UtcNowThe current time, for cache expiry and nag timers. Handy in tests.
HttpClientHttpClient?a shared internal clientBring your own, for example to add a proxy or a logging handler.
RequestTimeoutTimeSpan30 secondsTimeout of each HTTP request. TimeSpan.Zero or Timeout.InfiniteTimeSpan disables it; negative values throw invalid_config.

The per-property details, with links between related types, are in the SponsorkitOptions reference.

Targets#

GitHub and OpenCollective are trimmed and lowercased, then validated: a GitHub login is 1–39 letters, digits or single hyphens; an Open Collective slug is letters, digits and hyphens. The canonical form, such as github:octocat, is what sk.Targets returns and what every result carries in Targets. The SHA-256 of the target list (sk.TargetsHash) names the state file, so two apps with the same targets share a session. See targets.

What the constructor rejects#

Each of these throws SponsorkitError with Code == "invalid_config":

  • no target, or a malformed login or slug;
  • an AppName that is only whitespace or longer than 64 characters (an empty string counts as not set);
  • an ApiBaseUrl or SiteBaseUrl that isn't an absolute http or https URL;
  • a SignInTimeout of zero or less (other than Timeout.InfiniteTimeSpan);
  • a negative RequestTimeout;
  • an unknown mock preset, from Mock or SPONSORKIT_MOCK.

ArgumentNullException is thrown only for a null options object.

Environment variables#

The SDK reads these from the process environment, or from the Environment option when you set one. A variable counts as set when it is non-empty and not 0 or false (case-insensitive).

VariableEffect
SPONSORKIT_DISABLETurns the SDK into a no-op: no network, no disk. Results have Disabled == true; SignInAsync and OpenCheckoutAsync throw disabled; nags never show. Wins over mock mode.
SPONSORKIT_MOCKSelects a mock preset, for example active-monthly. The Mock option wins over it.
SPONSORKIT_NO_NAGDueAsync returns false unless called with force: true.
CISame as SPONSORKIT_NO_NAG. Most CI systems set it.
SPONSORKIT_DEV_CHECKSTurns on the dev-time target check when DevChecks is null.

SPONSORKIT_DISABLE is the switch to document for your users: anyone who doesn't want your app to talk to Sponsorkit can set it.

The dev-time target check#

.NET has no reliable "this is a development build" signal, so the target check is off unless you ask for it. Turn it on while you integrate:

Terminal
SPONSORKIT_DEV_CHECKS=1 dotnet run

The first GetFinancialContributionAsync, SignInAsync or OpenCheckoutAsync in the process then asks Sponsorkit whether each target exists and has a sponsor listing, and throws invalid_config naming the missing ones. If Sponsorkit can't be reached, the check passes silently.

Client properties#

MemberDescription
TargetsThe canonical target strings, for example github:octocat.
TargetsHashThe hash that names this target set's state file.
IsDisabledWhether SPONSORKIT_DISABLE is set for this instance.
IsMockWhether mock mode is active.
ApiBaseUrlThe API origin in use, after normalization.
SiteBaseUrlThe site base URL in use (pages and checkout links), after normalization.
IsSignedIn()Whether a session token is stored. No network access.

With dependency injection#

A client is cheap to keep and safe to share between threads, so register one instance:

Program.cs
using Microsoft.Extensions.DependencyInjection;
using Sponsorkit;

var services = new ServiceCollection();
services.AddSingleton(new SponsorkitClient(new SponsorkitOptions { GitHub = "octocat", AppName = "my-app" }));

using var provider = services.BuildServiceProvider();
var sk = provider.GetRequiredService<SponsorkitClient>();
Console.WriteLine(sk.IsSignedIn() ? "Signed in" : "Signed out");

This needs the Microsoft.Extensions.DependencyInjection package; the generic host and ASP.NET Core include it.