Configuring the JavaScript / TypeScript SDK

Every Sponsorkit constructor option with its default, the environment variables, and the exported defaults.

On this page

All configuration goes into the constructor. The constructor validates everything up front and throws a SponsorkitError with code invalid_config for a missing target or a malformed option, so mistakes surface on the first line that uses the SDK rather than at sign-in time.

import { Sponsorkit } from "@sponsorkit/sdk";

const sk = new Sponsorkit({
  github: "octocat",               // GitHub user or organization login
  openCollective: "my-collective", // Open Collective collective slug
  cacheTtl: 3600,                  // seconds a cached result is served without calling the API
  requestTimeout: 30,              // seconds before an API request is abandoned
});

Durations are in seconds and amounts in minor units (cents) everywhere in the SDK.

Options for every client#

These come from SponsorkitCoreOptions and apply to both @sponsorkit/sdk and @sponsorkit/sdk/node.

OptionTypeDefaultWhat it does
githubstringnoneGitHub user or organization login that receives sponsorships.
openCollectivestringnoneOpen Collective collective slug that receives contributions. At least one of github and openCollective is required.
cacheTtlnumber3600Seconds a cached result is served without calling the API. Values below 60 are raised to 60; negative values throw invalid_config.
mockMockOptionnoneMock mode: a preset name or { contributions }. Overrides SPONSORKIT_MOCK. See Testing.
storageSponsorkitStorage or Web Storage objectplatform defaultWhere the session token, cache and nag state live. See Platform notes.
apiBaseUrlstring"https://api.sponsorkit.io"API origin, used for every API call (/api/v1/…), for self-hosting and tests. Must be http or https; only the origin is used. An empty string counts as unset.
siteBaseUrlstring"https://ffmathy.github.io/sponsorkit", or apiBaseUrl when only that is setSite base URL (origin plus optional path), used for the /connect/ and /checkout/ pages, checkoutUrl() and the loopback checkout redirect. In the browser, sign-in messages are accepted only from its origin. An empty string counts as unset; trailing slashes are trimmed.
envRecord<string, string | undefined>process.envEnvironment used for the variables below. When given, it replaces the process environment for them; it is not merged.
devChecksbooleansee belowChecks once per process that the targets exist (GET /api/v1/targets), and throws invalid_config on the first async call if one doesn't.
signInTimeoutnumber300Seconds to wait for a sign-in (popup, loopback or paste-back) before rejecting with timeout. Must be positive.
checkoutPollIntervalnumber60Seconds between polls while openCheckout() waits for the new contribution. Must be positive.
clock() => numberDate.nowEpoch-millisecond clock for cache expiry, Retry-After and nag timing.
fetchFetchLikeglobal fetchCustom fetch implementation (proxies, logging, tests).
requestTimeoutnumber30Seconds before an API request (headers and body) is abandoned. getFinancialContribution() then serves the cache; the sign-in code exchange rejects with network_error. 0 disables the limit.
interactivebooleandetectedWhether the app is interactive, for nag().due(). Browsers count as interactive; on Node it is "stdout and stderr are both TTYs".

devChecks defaults to on when SPONSORKIT_DEV_CHECKS is set, or when process.env.NODE_ENV is "development" (most bundlers replace that expression at build time), and off otherwise. The check never runs in mock mode or when Sponsorkit is disabled, and it is skipped silently when the API can't be reached.

Browser options#

Only for Sponsorkit from @sponsorkit/sdk (SponsorkitOptions).

OptionTypeDefaultWhat it does
returnTostringnoneRedirect fallback when the popup is blocked: an https URL of your app (or http://localhost, 127.0.0.1, [::1]) to come back to with ?sponsorkit_code=…. Without it, a blocked popup rejects with popup_blocked.
openWindow(url: string) => Window | nullwindow.open(url, "sponsorkit", popupFeatures)Opens the popup. Must open the window synchronously.
popupFeaturesstring"popup,width=520,height=720"The features argument of window.open.

Node, Bun and Deno options#

Only for Sponsorkit from @sponsorkit/sdk/node (SponsorkitOptions).

OptionTypeDefaultWhat it does
appNamestringnoneShown on the consent page as the app that "says it is" asking. At most 64 characters.
storageDirstringper-user config directoryDirectory for the storage file. Ignored when storage is given.
signInMethod"auto" | "loopback" | "paste""auto"How sign-in delivers the code. See Sign-in.
openBrowser(url: string) => void | Promise<void>system browserOpens a URL. The default runs open (macOS), xdg-open (Linux) or rundll32 (Windows) without a shell.
promptForCode(url: string) => string | Promise<string>reads a line from stdinAsks the user for the paste-back code; receives the URL they need to open.
output{ write(text: string): unknown }process.stderrWhere messages for the user (sign-in links) are printed.

Environment variables#

A variable counts as set when it is non-empty and not 0 or false (case-insensitive). The SDK reads them from process.env where it exists, or from the env option.

VariableEffect
SPONSORKIT_DISABLETurns Sponsorkit off: no network or disk access, results have disabled: true, signIn() and openCheckout() reject with disabled, nag().due() returns false. Beats mock.
SPONSORKIT_MOCKA mock preset name, used when the mock option isn't given.
SPONSORKIT_NO_NAGnag().due() returns false (unless force).
SPONSORKIT_DEV_CHECKSTurns the dev-time target check on, unless devChecks: false.
CInag().due() returns false (unless force).

SPONSORKIT_DISABLE is how users of your app opt out; mention it in your README.

In browsers there is usually no process.env, so the variables only apply if your bundler defines them. Pass env explicitly when you want them in a web app:

import { Sponsorkit } from "@sponsorkit/sdk";

const sk = new Sponsorkit({
  github: "octocat",
  env: { SPONSORKIT_MOCK: new URLSearchParams(location.search).get("mock") ?? undefined },
});

Exported defaults#

The defaults are exported as constants, in case you want to show them or build on them:

ConstantValue
DEFAULT_API_BASE_URL"https://api.sponsorkit.io"
DEFAULT_SITE_BASE_URL"https://ffmathy.github.io/sponsorkit"
DEFAULT_CACHE_TTL3600
DEFAULT_CHECKOUT_POLL_INTERVAL60
DEFAULT_CHECKOUT_TIMEOUT600
DEFAULT_SIGN_IN_TIMEOUT300

Instance properties#

After construction a client exposes what it resolved:

PropertyTypeMeaning
targetsreadonly string[]Canonical target strings, e.g. ["github:octocat", "open-collective:my-collective"].
targetsParamstringThe same, comma-separated.
apiBaseUrlstringThe API origin in use.
siteBaseUrlstringThe site base URL in use (pages and checkout links).
disabledbooleanSPONSORKIT_DISABLE is set.
isMockbooleanMock mode is on.
appNamestring | undefinedNode client only.

Targets are normalized (trimmed and lowercased) and validated: a GitHub login must be a valid GitHub login and an Open Collective slug a valid slug, otherwise the constructor throws invalid_config. See Targets for why the target set matters.