Configuring the Go SDK

Every sponsorkit.Options field with its default, the per-call options, the environment variables and the exported constants.

On this page

All configuration goes into sponsorkit.Options, passed to sponsorkit.New. New validates everything up front and returns a *sponsorkit.Error with code invalid_config for anything wrong. It does no I/O: nothing touches the network or the disk until you call a method.

main.go
package main

import (
	"log"
	"time"

	sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)

func main() {
	sk, err := sponsorkit.New(sponsorkit.Options{
		GitHub:         "octocat",
		OpenCollective: "my-collective",
		AppName:        "my-cli",
		CacheTTL:       30 * time.Minute,
	})
	if err != nil {
		log.Fatal(err) // e.g. sponsorkit: invalid_config: invalid github account "-bad-"
	}
	log.Println(sk.Targets()) // [github:octocat open-collective:my-collective]
}

Zero values mean "use the default" throughout, so you only set what you need.

Options#

Targets and identity#

FieldTypeDefaultMeaning
GitHubstring–The GitHub user or organization login that receives sponsorships.
OpenCollectivestring–The Open Collective slug that receives contributions.
AppNamestring–Shown on the consent page as "AppName says it is…". At most 64 characters.

At least one of GitHub and OpenCollective is required. Accounts are trimmed and lowercased, and must be valid logins or slugs; see targets. The target set is sealed into every session, so changing it later signs users out of your app (why).

Caching, storage and network#

FieldTypeDefaultMeaning
CacheTTLtime.Duration1 hourHow long a result is served from the local cache without calling the API. Values below 1 minute are raised to 1 minute; negative values are invalid_config. Per call, WithCacheTTL overrides it without the minimum.
StorageDirstringDefaultStorageDir()Directory of the state file. Ignored when Storage is set. If there is no per-user config directory (for example HOME and XDG_CONFIG_HOME are unset), state is kept in process memory only.
Storagesponsorkit.Storage&DirStorage{Dir: StorageDir}Replaces file storage entirely, for example with a keychain-backed implementation.
APIBaseURLstringhttps://api.sponsorkit.ioThe API origin, used for every API call, for self-hosting and tests. Must be an http or https URL.
SiteBaseURLstringhttps://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 http or https URL; trailing slashes are trimmed.
HTTPClient*http.Client&http.Client{}Used for every API call. Its transport, proxy settings and TLS configuration apply.
RequestTimeouttime.Duration30 sBounds each HTTP request, including reading the response. sponsorkit.NoRequestTimeout (any negative value) disables it. Applies whichever HTTPClient you pass.

The zero value of RequestTimeout means the 30 second default. Other SDKs disable the timeout with requestTimeout: 0; in Go that is sponsorkit.NoRequestTimeout.

Sign-in and checkout#

FieldTypeDefaultMeaning
SignInMethodsponsorkit.SignInMethodSignInAutoSignInAuto, SignInLoopback or SignInPaste. See choosing a flow. Any other value is invalid_config.
SignInTimeouttime.Duration5 minutesHow long a sign-in waits for the user (also the sign-in phase of OpenCheckout). Exceeding it returns timeout. Negative is invalid_config.
OpenBrowserfunc(ctx context.Context, url string) errorsponsorkit.OpenSystemBrowserOpens a URL for the user. An error makes SignInAuto fall back to paste-back.
PromptForCodefunc(ctx context.Context, url string) (string, error)sponsorkit.PromptStdinAsks for the paste-back code; receives the URL the user must open.
CheckoutPollIntervaltime.Duration60 sHow often OpenCheckout polls for the new contribution. Negative is invalid_config.
Stderrio.Writeros.StderrReceives user-facing messages, such as the paste-back URL.

Testing and environment#

FieldTypeDefaultMeaning
Mock*sponsorkit.MocknilMock mode: sponsorkit.MockPreset(name) or sponsorkit.MockContributions(...). Takes precedence over SPONSORKIT_MOCK. An unknown preset is invalid_config. See Testing.
Envmap[string]stringnil (process environment)Replaces the process environment for the variables below. It is not merged: a variable missing from the map counts as unset.
DevChecks*boolon only when SPONSORKIT_DEV_CHECKS is setThe dev-time check that your targets exist.
Clockfunc() time.Timetime.NowThe current time, for the cache TTL and nag timing.
Interactive*boolTTY detectionOverrides the check that stdout and stderr are terminals, used by Nag.Due.

Per-call options#

Some methods take their own options, which override the client's for that call:

MethodOptions
GetFinancialContribution(ctx, opts ...GetOption)sponsorkit.WithCacheTTL(d): 0 always refreshes (unless a Retry-After is still in force).
SignIn(ctx, opts ...SignInOptions)SignInOptions{Provider, Method}: skip the provider picker, or force a flow. Pass at most one.
OpenCheckout(ctx, CheckoutOptions), CheckoutURL(CheckoutOptions)Provider, Amount (minor units), Currency, Frequency, and for OpenCheckout also Timeout (10 minutes), PollInterval and SignInMethod. See Checkout.
Nag.Due(ctx, predicate, opts ...DueOption)sponsorkit.Force(): skip the TTY, CI and SPONSORKIT_NO_NAG checks.
sponsorkit.VerifySession(ctx, token, targets, opts ...VerifyOption)WithAPIBaseURL, WithHTTPClient, WithRequestTimeout. See Backend verification.

Environment variables#

A variable counts as set when it is non-empty and not 0 or false (in any case). With Options.Env, the map is read instead of the process environment.

VariableEffect
SPONSORKIT_DISABLETurns the SDK off: no network, no disk, Result.Disabled is true, Nag.Due is always false, and SignIn/OpenCheckout return disabled. Wins over mock mode.
SPONSORKIT_MOCKMock mode with the named preset, when Options.Mock is nil.
SPONSORKIT_NO_NAGNag.Due is always false (unless Force()).
SPONSORKIT_DEV_CHECKSTurns on the dev-time target check when Options.DevChecks is nil.
CINag.Due is always false (unless Force()).
SSH_CONNECTIONSignInAuto uses paste-back.
DISPLAY, WAYLAND_DISPLAYOn Linux and the BSDs, when both are empty and OpenBrowser is the default, SignInAuto uses paste-back.

Tell your users about SPONSORKIT_DISABLE and SPONSORKIT_NO_NAG; see turning Sponsorkit off.

The dev-time target check#

Go has no "development build" signal, so the check that your targets exist is off unless you turn it on with SPONSORKIT_DEV_CHECKS=1 or DevChecks. When on, the first GetFinancialContribution, SignIn or OpenCheckout in a process calls GET /api/v1/targets once. A target that doesn't exist (or has no sponsorship listing) makes those methods return invalid_config for the rest of the process; if the API can't be reached, the check is skipped.

main.go
package main

import (
	"log"

	sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)

func main() {
	on := true
	sk, err := sponsorkit.New(sponsorkit.Options{GitHub: "octocat", DevChecks: &on})
	if err != nil {
		log.Fatal(err)
	}
	_ = sk
}

Constants and helpers#

NameValue
sponsorkit.VersionThe SDK version, "1.0.0". Sent in the User-Agent header as sponsorkit-go/1.0.0.
sponsorkit.DefaultAPIBaseURL"https://api.sponsorkit.io".
sponsorkit.DefaultSiteBaseURL"https://ffmathy.github.io/sponsorkit".
sponsorkit.NoRequestTimeout-1: disables RequestTimeout and WithRequestTimeout.
sponsorkit.MockPresetNamesThe mock preset names.
sponsorkit.DefaultStorageDir()The default StorageDir; see platform notes.
sponsorkit.TargetsParam(t), sponsorkit.TargetsHash(t)The canonical target string (github:octocat,open-collective:webpack) and its SHA-256, which names the state file.
sponsorkit.ParseISODuration(s)Parses "P7D"-style durations, for configuring nags from a file.

The client also reports its configuration: Targets(), TargetsParam(), Disabled() and IsMock().