Sponsorkit for Rust

Install the sponsorkit crate and use it from Rust CLIs, desktop apps and Tokio backends.

On this page

The sponsorkit crate tells your Rust program what the current user has contributed to your GitHub Sponsors and Open Collective accounts, signs them in, and sends them to contribute. It's written for command-line tools and desktop apps that one person runs, and it has a helper for servers that need to check a supporter.

There's no Sponsorkit account or API key. You name the accounts you receive money on, and the SDK does the rest.

Requirements#

Rust1.85 or later (the crate uses the 2024 edition)
Async runtimeTokio 1.x, current-thread or multi-thread. The SDK has no blocking API; see platform notes.
Dependenciesreqwest (with rustls, so no OpenSSL), serde, serde_json, tokio, tokio-util, sha2, base64, getrandom
Cargo featuresNone. There's nothing to turn on or off.
Operating systemsLinux, macOS and Windows for sign-in and checkout (they open the system browser). server::verify_session and mock mode run anywhere Tokio and reqwest do.

Install#

The crate is on crates.io as sponsorkit. You also need Tokio in your own app to run async code; #[tokio::main] needs the macros feature, plus rt-multi-thread for its default multi-thread flavour.

Terminal
cargo add sponsorkit
cargo add tokio --features macros,rt-multi-thread

A first look#

src/main.rs
use sponsorkit::{CheckoutOptions, Frequency, GetOptions, Options, Sponsorkit};

#[tokio::main]
async fn main() -> Result<(), sponsorkit::Error> {
    let sk = Sponsorkit::new(Options {
        github: Some("octocat".into()), // the account you receive sponsorships on
        app_name: Some("my-cli".into()), // shown on the consent page
        ..Default::default()
    })?; // invalid_config: a malformed target or option

    // Never fails because of the network: it falls back to the cached result.
    let fc = sk.get_financial_contribution(GetOptions::default()).await?;
    if fc.signed_in && !fc.active().is_empty() {
        println!("Thank you for sponsoring my-cli!");
        return Ok(());
    }
    let link = sk.checkout_url(&CheckoutOptions { frequency: Some(Frequency::Monthly), ..Default::default() })?;
    eprintln!("Enjoying my-cli? Support it: {link}");
    Ok(())
}

A few things hold everywhere in the crate:

  • All configuration is a plain struct with public fields. Set what you need and fill the rest with ..Default::default().
  • Every fallible call returns sponsorkit::Result<T>, and every error is a sponsorkit::Error with a machine-readable code().
  • Sponsorkit is cheap to clone (an Arc inside), Send and Sync, and its futures are Send, so you can share one client across tasks.

In this section#

PageWhat it covers
QuickstartA complete CLI with login, sponsor and logout commands and a nag.
ConfigurationEvery Options field with its default, GetOptions, environment variables and constants.
Sign-inLoopback and paste-back sign-in, choosing a flow, second identities, hooks, cancelling.
Contributionsget_financial_contribution, ContributionResult, its filter helpers, and caching.
Checkoutopen_checkout and checkout_url.
NagAsking for support every N runs or N days.
TestingMock mode, deterministic nags, and test hooks.
Backend verificationChecking a supporter on your server with server::verify_session, with an axum example.
Errors and cancellationsponsorkit::Error, ErrorCode, CancellationToken and dropping futures.
Platform notesTokio, synchronous apps, where state is kept, browsers, stdin, TLS and proxies.

The API reference lists every public type, function, method, field and constant, generated from the crate's source and doc comments. cargo doc --open builds the same documentation locally; it will also be on docs.rs once the crate is published.