Sponsorkit for Go

Install and use the Go SDK (github.com/ffMathy/sponsorkit/sdks/go) in CLIs, desktop tools and servers.

On this page

Package sponsorkit tells your Go program what the current user has contributed to your GitHub Sponsors and Open Collective accounts, signs them in, and sends them to contribute. It is written for command-line tools and other programs one person runs, and has a helper for servers that need to check a supporter. It uses only the standard library.

Requirements#

Go1.23 or later (the module's go.mod says go 1.23)
DependenciesNone outside the standard library, and no cgo
Operating systemsLinux, macOS, Windows and the BSDs for sign-in and checkout (they open the system browser). VerifySession and mock mode run anywhere Go does.
Module pathgithub.com/ffMathy/sponsorkit/sdks/go
Package namesponsorkit

Install#

Go modules are fetched straight from the Git repository through the module proxy, so there is no separate registry and no package manager besides the go command. The current release is v1.0.0.

Pick the way that suits your project:

From your module's directory:

Terminal
go get github.com/ffMathy/sponsorkit/sdks/go@latest

go get adds a require line to go.mod and the checksums to go.sum. Ask for a branch or commit with @main or @<commit> (Go records a pseudo-version), or for an exact version with @v1.0.0.

Then import it. The package is already named sponsorkit; the alias only makes the import read clearly:

main.go
import sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"

Releases

Releases are tags named sdks/go/vX.Y.Z, because the module lives in a subdirectory of the repository; you ask for @v1.0.0. For an unreleased change, use @main or a commit; see build from source and versions & pinning.

A first look#

main.go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

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

func main() {
	ctx := context.Background()
	sk, err := sponsorkit.New(sponsorkit.Options{
		GitHub:  "octocat", // the account you receive sponsorships on
		AppName: "my-cli",  // shown on the consent page
	})
	if err != nil {
		log.Fatal(err) // invalid_config: a malformed target or option
	}

	// Never fails because of the network: it falls back to the cached result.
	fc, err := sk.GetFinancialContribution(ctx)
	if err != nil {
		log.Fatal(err) // only invalid_config, from the opt-in dev-time target check
	}
	if fc.SignedIn && len(fc.Active()) > 0 {
		fmt.Println("Thank you for sponsoring my-cli!")
		return
	}
	link, err := sk.CheckoutURL(sponsorkit.CheckoutOptions{Frequency: sponsorkit.Monthly})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintln(os.Stderr, "Enjoying my-cli? Support it:", link)
}

Every method that can do network I/O or block takes a context.Context, every error from New, the Client methods and VerifySession is a *sponsorkit.Error with a Code, and a *Client is safe for concurrent use.

In this section#

PageWhat it covers
QuickstartA complete CLI with a status check, a sponsor command and a nag.
ConfigurationEvery Options field with its default, per-call options and environment variables.
Sign-inLoopback and paste-back sign-in, choosing a flow, second identities, cancelling.
ContributionsGetFinancialContribution, the Result, its filter helpers, and caching.
CheckoutOpenCheckout and CheckoutURL.
NagAsking for support every N runs or N days.
TestingMock mode, test hooks, and a fake API with net/http/httptest.
Backend verificationChecking a supporter on your server with VerifySession.
Errors and cancellation*sponsorkit.Error, its codes, errors.As, and context.Context.
Platform notesContexts and goroutines, where state is stored, browsers, stdin, proxies.

The API reference lists every type, function, method, field and constant, generated from the package's doc comments. The same documentation is on pkg.go.dev and in your terminal with go doc github.com/ffMathy/sponsorkit/sdks/go.