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#
| Go | 1.23 or later (the module's go.mod says go 1.23) |
| Dependencies | None outside the standard library, and no cgo |
| Operating systems | Linux, macOS, Windows and the BSDs for sign-in and checkout (they open the system browser). VerifySession and mock mode run anywhere Go does. |
| Module path | github.com/ffMathy/sponsorkit/sdks/go |
| Package name | sponsorkit |
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:
go get github.com/ffMathy/sponsorkit/sdks/go@latestgo 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.
Add the requirement by hand, then let Go fill in go.sum.
require github.com/ffMathy/sponsorkit/sdks/go v1.0.0go mod tidyTo build without network access, vendor your dependencies after adding the requirement:
go get github.com/ffMathy/sponsorkit/sdks/go@latest
go mod vendorgo build uses the vendor/ folder automatically when it exists.
To work against a clone of the repository, use a workspace (nothing to undo in go.mod):
git clone https://github.com/ffMathy/sponsorkit.git ../sponsorkit
go work init . ../sponsorkit/sdks/goOr add a replace directive to go.mod:
require github.com/ffMathy/sponsorkit/sdks/go v0.0.0
replace github.com/ffMathy/sponsorkit/sdks/go => ../sponsorkit/sdks/goThen import it. The package is already named sponsorkit; the alias only makes the import read clearly:
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#
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#
| Page | What it covers |
|---|---|
| Quickstart | A complete CLI with a status check, a sponsor command and a nag. |
| Configuration | Every Options field with its default, per-call options and environment variables. |
| Sign-in | Loopback and paste-back sign-in, choosing a flow, second identities, cancelling. |
| Contributions | GetFinancialContribution, the Result, its filter helpers, and caching. |
| Checkout | OpenCheckout and CheckoutURL. |
| Nag | Asking for support every N runs or N days. |
| Testing | Mock mode, test hooks, and a fake API with net/http/httptest. |
| Backend verification | Checking a supporter on your server with VerifySession. |
| Errors and cancellation | *sponsorkit.Error, its codes, errors.As, and context.Context. |
| Platform notes | Contexts 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.