Verify supporters on your backend

Get a trustworthy answer on your server by forwarding the supporter's session token and verifying it, with no API key.

On this page

Anything your app checks on the user's machine can be patched out. When a feature costs you something (a hosted service, storage, API quota), decide on your server instead. Your backend doesn't need an API key or a webhook: the client forwards its session token, and the backend asks Sponsorkit about it.

How it works#

  1. The supporter signs in through your app as usual. The SDK stores a session token (sks_…).
  2. Your app sends that token to your backend, for example in a request header.
  3. Your backend calls verifySession(token, targets). It sends the token to GET /api/v1/contributions and gets the live contributions back.
  4. verifySession checks that the session was issued for your targets, then returns the same result type as getFinancialContribution().

The token can't be forged or edited: it's encrypted and authenticated with a key only Sponsorkit has, and it contains the targets it was issued for. Step 4 matters because a token from a different app (with different targets) is just as genuine; it just isn't about you.

Verify on the server#

server.ts
import { isSponsorkitError, verifySession } from "@sponsorkit/sdk/server";

// A fetch-style handler: works in Node 20+, Bun, Deno and Cloudflare Workers.
export async function handleDownload(request: Request): Promise<Response> {
  const token = request.headers.get("X-Sponsorkit-Session") ?? "";
  try {
    const fc = await verifySession(token, { github: "octocat", openCollective: "my-project" });
    if (fc.active().length === 0) return new Response("Sponsors only", { status: 403 });
    return new Response(`Here you go, ${fc.supporter?.id}`);
  } catch (e) {
    if (isSponsorkitError(e, "session_invalid") || isSponsorkitError(e, "targets_mismatch")) {
      return new Response("Please sign in again", { status: 401 });
    }
    if (isSponsorkitError(e, "network_error")) {
      return new Response("Try again later", { status: 503, headers: { "Retry-After": String(e.retryAfter ?? 60) } });
    }
    throw e;
  }
}

verifySession touches no local storage and keeps no cache: every call asks Sponsorkit live. That's what makes it trustworthy, and also why you should cache the answer yourself (see below).

Errors#

CodeMeaningSuggested response
session_invalidThe token is missing, malformed, expired, tampered with or sealed with a retired key.401: ask the user to sign in again.
targets_mismatchThe token is genuine but was issued for a different set of targets.401 or 403.
network_errorSponsorkit couldn't be reached, timed out, or answered 5xx, 429 or something unexpected. retryAfter holds the seconds from Retry-After when the API sent one.503, and retry later.
invalid_configYour targets argument is missing or malformed.Fix your code.

Unlike getFinancialContribution(), verifySession does throw on network problems. On a client, a stale answer is better than a crash; on a server deciding access, "I don't know" must be visible so you can choose whether to fail open or closed.

Options#

Every SDK accepts the API origin (default https://api.sponsorkit.io; change it when you self-host) and a request timeout (30 seconds by default; 0 disables it). Most also take your own HTTP client:

LanguageCall
JavaScriptverifySession(token, targets, { apiBaseUrl, fetch, signal, requestTimeout })
.NETSponsorkitServer.VerifySessionAsync(token, new VerifySessionOptions { GitHub, OpenCollective, ApiBaseUrl, HttpClient, RequestTimeout }, ct)
Pythonverify_session(token, targets, api_base_url=…, request_timeout=…, http_client=…), or await averify_session(…)
Gosponsorkit.VerifySession(ctx, token, targets, sponsorkit.WithAPIBaseURL(…), sponsorkit.WithHTTPClient(…), sponsorkit.WithRequestTimeout(…))
Rustserver::verify_session_with(token, &targets, VerifyOptions { api_base_url, http_client, request_timeout })

In JavaScript, targets can also be a canonical target string such as "github:octocat,open-collective:my-project".

Send the token from the client#

The token is a bearer credential for the supporter's contribution data on your targets, so send it only to your own backend, over HTTPS.

client.ts
import { Sponsorkit } from "@sponsorkit/sdk";

const sk = new Sponsorkit({ github: "octocat", openCollective: "my-project" });

export async function download(): Promise<Response> {
  const token = await sk.getSessionToken(); // null when signed out
  return fetch("/download", { headers: token ? { "X-Sponsorkit-Session": token } : {} });
}

The JavaScript (getSessionToken()) and Go (SessionToken()) clients expose the stored token. The .NET, Python and Rust clients don't have an accessor yet; the token is the sessionToken field of the storage file. The client keeps the token fresh on its own: sessions last 180 days and are renewed while they're being used.

Key users on supporter.id#

supporter.id (sup_…) is stable for the same person and the same set of targets, so you can use it as the key for a user record or an entitlement in your database. It's derived from the person's first sign-in identity, so connecting a second provider later doesn't change it.

It's also pairwise: apps with different targets get different ids for the same person, so ids can't be used to track people across apps. It isn't their GitHub or Open Collective user id.

Cache the answer#

Every verifySession call is a live request to GitHub or Open Collective, and the API allows one refresh per minute per session (more requests get 429 and a network_error). That limit is shared with the client's own refreshes, because they use the same session. Verify when something changes (a sign-in, the first request of a session, a purchase) and keep the answer for a while, keyed on supporter.id, instead of verifying on every request.

Without an SDK#

verifySession is a thin wrapper around one HTTP call. In another language, do it yourself:

curl https://api.sponsorkit.io/api/v1/contributions \
  -H "Authorization: Bearer sks_…"

Then compare the targets array in the response with your own canonical targets (lowercased, sorted, for example ["github:octocat", "open-collective:my-project"]), and treat 401 as session_invalid. The response is described in the HTTP API reference.