Go API reference
Every exported type, function, method, field and constant of package sponsorkit (github.com/ffMathy/sponsorkit/sdks/go), generated from the source.
On this page
import sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"Package sponsorkit is the Go SDK, Go 1.23 or later with no dependencies outside the standard library. Version 1.0.0. This reference is generated from the doc comments in sdks/go with go/doc; for guides see Sponsorkit for Go.
Overview#
Package sponsorkit builds financial contributions (GitHub Sponsors and Open Collective) directly into Go programs. It needs no account, API key or setup: name the account(s) you receive sponsorships on and write your own rule.
sk, err := sponsorkit.New(sponsorkit.Options{GitHub: "octocat", AppName: "my-cli"})
if err != nil {
log.Fatal(err) // invalid_config: malformed targets or options
}
fc, _ := sk.GetFinancialContribution(ctx)
if fc.SignedIn && len(fc.Active()) == 0 {
link, _ := sk.CheckoutURL(sponsorkit.CheckoutOptions{Frequency: sponsorkit.Monthly})
fmt.Fprintln(os.Stderr, "Support my-cli:", link)
}What the client does#
GetFinancialContributionreports what the signed-in user contributed to your targets. Results are cached locally (CacheTTL, default one hour).SignInsigns the user in with the loopback flow (a local server on 127.0.0.1 plus the system browser) or the paste-back flow for headless machines. Calling it again with another provider adds a second identity.OpenCheckoutsends the user to contribute and polls until the new contribution shows up;CheckoutURLis an offline link to the same page.Nagdecides when to show a "please support" message.VerifySessionlets a backend check a session token forwarded by a client.
Error contract#
Every error is an *Error with a Code (see ErrorCode; the codes are those of PROTOCOL §7). GetFinancialContribution never returns errors for network or server problems: it serves the cache (FromCache and Stale set) or a signed-in empty result with Stale set, honours Retry-After, and treats session_invalid as a sign-out. SignIn reports them as network_error (with Error.RetryAfter when the API sent Retry-After), and reports cancellation of its context.Context as cancelled and Options.SignInTimeout as timeout. With SPONSORKIT_DISABLE set there is no network or disk access at all.
Testing#
Options.Mock (MockPreset, MockContributions) or the SPONSORKIT_MOCK environment variable switch to mock mode: no network, no disk, and Result.Mock is true. Options also exposes test hooks: Options.APIBaseURL, Options.SiteBaseURL, Options.StorageDir, Options.OpenBrowser, Options.PromptForCode, Options.SignInMethod, Options.SignInTimeout, Options.RequestTimeout, Options.Env, Options.DevChecks, Options.Interactive, Options.CheckoutPollInterval and Options.Clock. Options.Env replaces the process environment for the SPONSORKIT_* variables and CI; the dev-time target check is off unless Options.DevChecks or SPONSORKIT_DEV_CHECKS turns it on. ResetProcessState simulates a new process.
The wire protocol is specified in docs/PROTOCOL.md of https://github.com/ffMathy/sponsorkit; this package passes the language-neutral conformance suite in sdks/conformance.
Example#
package main
import (
"context"
"fmt"
"log"
sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)
func main() {
ctx := context.Background()
sk, err := sponsorkit.New(sponsorkit.Options{
GitHub: "octocat",
OpenCollective: "webpack",
AppName: "my-cli",
Mock: sponsorkit.MockPreset("active-monthly"), // remove in production
})
if err != nil {
log.Fatal(err)
}
fc, err := sk.GetFinancialContribution(ctx)
if err != nil {
log.Fatal(err) // only invalid_config from the dev-time target check
}
for _, c := range fc.Active() {
fmt.Printf("%s %s %d %s\n", c.Provider, c.Frequency, c.Amount.Value, c.Amount.Currency)
}
}Output:
github monthly 500 USDIndex#
Types#
CheckoutOptions: CheckoutOptions describes the contribution to suggest.CheckoutResult: CheckoutResult is returned by OpenCheckout.CheckoutStatus: CheckoutStatus is the outcome of OpenCheckout.Client: Client is a Sponsorkit client for one target set.func New(opts Options) (*Client, error)func (c *Client) CheckoutURL(opts CheckoutOptions) (string, error)func (c *Client) Disabled() boolfunc (c *Client) GetFinancialContribution(ctx context.Context, opts ...GetOption) (*Result, error)func (c *Client) IsMock() boolfunc (c *Client) IsSignedIn() boolfunc (c *Client) Nag(opts NagOptions) *Nagfunc (c *Client) OpenCheckout(ctx context.Context, opts CheckoutOptions) (*CheckoutResult, error)func (c *Client) SessionToken() stringfunc (c *Client) SignIn(ctx context.Context, opts ...SignInOptions) (*Result, error)func (c *Client) SignOut(_ context.Context) errorfunc (c *Client) Targets() []stringfunc (c *Client) TargetsParam() string
Contribution: Contribution is one sponsorship (GitHub) or order (Open Collective), normalized to one shape across providers.DirStorage: DirStorage is the default Storage: one file named <key>.json per target set in Dir (PROTOCOL §8).DueOption: DueOption customizes Nag.Due.Error: Error is the error type returned by New, the Client methods, VerifySession, TargetsParam, TargetsHash and ParseISODuration.ErrorCode: ErrorCode is the machine-readable code carried by every *Error (PROTOCOL §7).Frequency: Frequency is how often a contribution is charged.GetOption: GetOption customizes one GetFinancialContribution call.Identity: Identity is one provider account of the signed-in supporter.Kind: Kind distinguishes recurring from one-time contributions.Mock: Mock configures mock mode: no network and no disk access, results carry mock: true.Money: Money is an amount in minor units (cents) of an ISO 4217 currency.Nag: Nag decides when to show a "please support" message.NagOptions: NagOptions configures a nag.Options: Options configures a Client.Provider: Provider identifies a sponsorship platform.Result: Result is what GetFinancialContribution returns.func VerifySession(ctx context.Context, token string, targets Targets, opts ...VerifyOption) (*Result, error)func (r *Result) Active() []Contributionfunc (r *Result) ByProvider(p Provider) []Contributionfunc (r *Result) Direct() []Contributionfunc (r Result) MarshalJSON() ([]byte, error)func (r *Result) OneTime() []Contributionfunc (r *Result) Recurring() []Contribution
SignInMethod: SignInMethod selects the native sign-in flow (PROTOCOL §7).SignInOptions: SignInOptions customizes SignIn.SponsorEntity: SponsorEntity is who actually pays: the user or an organization they belong to.Status: Status is a contribution status.Storage: Storage persists the per-target-set state (session token, cache and nag state).Supporter: Supporter is the signed-in user.Targets: Targets names the accounts a maintainer receives sponsorships on.Tier: Tier is the provider tier of a contribution.VerifyOption: VerifyOption customizes VerifySession.Via: Via says whether the supporter paid directly or through an organization.
Functions#
func CodeChallenge(verifier string) stringfunc DefaultStorageDir() (string, error)func NewCodeVerifier() stringfunc OpenSystemBrowser(ctx context.Context, url string) errorfunc ParseISODuration(s string) (time.Duration, error)func PromptStdin(ctx context.Context, _ string) (string, error)func ResetProcessState()func TargetsHash(t Targets) (string, error)func TargetsParam(t Targets) (string, error)
Constants and variables#
Typed constants (error codes, statuses, providers, frequencies, sign-in methods) are listed on the page of their type.