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#
| Rust | 1.85 or later (the crate uses the 2024 edition) |
| Async runtime | Tokio 1.x, current-thread or multi-thread. The SDK has no blocking API; see platform notes. |
| Dependencies | reqwest (with rustls, so no OpenSSL), serde, serde_json, tokio, tokio-util, sha2, base64, getrandom |
| Cargo features | None. There's nothing to turn on or off. |
| Operating systems | Linux, 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.
cargo add sponsorkit
cargo add tokio --features macros,rt-multi-thread[dependencies]
sponsorkit = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }"1" accepts any 1.x release. Pin an exact version with "=1.0.0"; see
versions & pinning.
To try an unreleased change, depend on the repository. Cargo finds the crate inside it by its name, so you can depend on a branch, tag or commit:
[dependencies]
sponsorkit = { git = "https://github.com/ffMathy/sponsorkit", branch = "main" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }Cargo.lock records the exact commit. Use rev = "<commit>" or tag = "sdks/rust/v1.0.0" instead of
branch for a fixed version.
To work against a clone of the repository:
git clone https://github.com/ffMathy/sponsorkit.git ../sponsorkit[dependencies]
sponsorkit = { path = "../sponsorkit/sdks/rust" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }See build from source for running the SDK's own tests.
A first look#
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 asponsorkit::Errorwith a machine-readablecode(). Sponsorkitis cheap to clone (anArcinside),SendandSync, and its futures areSend, so you can share one client across tasks.
In this section#
| Page | What it covers |
|---|---|
| Quickstart | A complete CLI with login, sponsor and logout commands and a nag. |
| Configuration | Every Options field with its default, GetOptions, environment variables and constants. |
| Sign-in | Loopback and paste-back sign-in, choosing a flow, second identities, hooks, cancelling. |
| Contributions | get_financial_contribution, ContributionResult, its filter helpers, and caching. |
| Checkout | open_checkout and checkout_url. |
| Nag | Asking for support every N runs or N days. |
| Testing | Mock mode, deterministic nags, and test hooks. |
| Backend verification | Checking a supporter on your server with server::verify_session, with an axum example. |
| Errors and cancellation | sponsorkit::Error, ErrorCode, CancellationToken and dropping futures. |
| Platform notes | Tokio, 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.