SponsorKit

repository·main·Indexed 21 days ago

https://github.com/antfu-collective/sponsorkit

A toolkit for fetching sponsor information from platforms such as GitHub Sponsors, Patreon, OpenCollective, Afdian, Polar, Liberapay, and Ko-fi to generate visual sponsor images in SVG, PNG, WebP, and JSON formats. It features a CLI for quick generation, a programmatic API via fetchSponsors, and advanced configuration options for defining sponsorship tiers, merging identities across platforms, and customizing SVG rendering via CSS.

Tokens
12.2K
Snippets
46
Records
55
Agent score
74%

What's inside sponsorkit

  1. Quickstart with SponsorKit via CLI

    main

    To use SponsorKit without writing code, you can use the CLI. First, create a .env file in your project root and configure at least one provider (e.g., GitHub, Patreon, etc.).

    Supported providers include:

    • GitHub Sponsors
    • Patreon
    • OpenCollective
    • Afdian
    • Polar
    • Liberapay
    • Ko-fi

    Once configured, run the following command to fetch data and generate sponsor images:

    npx sponsorkit
  2. Configure SponsorKit via .env

    main

    SponsorKit uses environment variables for provider authentication. You only need to configure one provider to get started.

    GitHub

    • SPONSORKIT_GITHUB_TOKEN: Requires read:user and read:org scopes.
    • SPONSORKIT_GITHUB_LOGIN: Your GitHub username.
    • SPONSORKIT_MODE: Set to sponsors (people sponsoring you) or sponsees (people you have sponsored).

    Patreon

    • SPONSORKIT_PATREON_TOKEN: Use the "Creator’s Access Token" from the Patreon portal.

    OpenCollective

    • SPONSORKIT_OPENCOLLECTIVE_KEY: Your API key.
    • SPONSORKIT_OPENCOLLECTIVE_ID, SPONSORKIT_OPENCOLLECTIVE_SLUG, or SPONSORKIT_OPENCOLLECTIVE_GH_HANDLE: Your account identifier.
    • SPONSORKIT_OPENCOLLECTIVE_TYPE: Set to person for personal accounts, or collective (default).

    Afdian

    • SPONSORKIT_AFDIAN_USER_ID: Your user ID.
    • SPONSORKIT_AFDIAN_TOKEN: Your token.

    Polar

    • SPONSORKIT_POLAR_TOKEN: Your token from Polar settings.
    • SPONSORKIT_POLAR_ORGANIZATION: The name of your organization.

    Liberapay

    • SPONSORKIT_LIBERAPAY_LOGIN: Your profile name.

    Ko-fi

    • SPONSORKIT_KOFI_VERIFICATION_TOKEN: Verification token from Ko-fi webhooks.
    • SPONSORKIT_KOFI_DATA_FILE: Path to the event store (default: ./sponsorkit/kofi-events.json).
  3. Set up Ko-fi Webhooks

    main

    Ko-fi uses webhooks instead of a direct API for sponsor lists. To use Ko-fi:

    1. Start the SponsorKit receiver:
      npx sponsorkit kofi-webhook
    2. Expose the local server http://127.0.0.1:3456/kofi via an HTTPS tunnel (e.g., using ngrok).
    3. Set that public URL in your Ko-fi webhooks page.
    4. Once events are received, regenerate your sponsor output using:
      npx sponsorkit --force

    Note: Ko-fi does not send events when a membership ends. SponsorKit treats subscription payments as active for 35 days by default. You can adjust this using kofi.subscriptionEffectivity in your config.

    npx sponsorkit kofi-webhook
  4. Configure SponsorKit via sponsorkit.config.js

    main

    For advanced configuration, create a sponsorkit.config.js file. You can use defineConfig and tierPresets to manage rendering logic and tiers.

    Key configuration options:

    • mode: 'sponsors' (default) or 'sponsees'.
    • renderer: 'tiers' or 'circles'.
    • width: Image width in pixels.
    • formats: Array of output formats (e.g., ['json', 'svg', 'png', 'webp']).
    • tiers: An array of tier objects defining how sponsors are grouped by monthlyDollars and their visual preset.
    import { defineConfig, tierPresets } from 'sponsorkit'
    
    export default defineConfig({
      mode: 'sponsors',
      github: {
        login: 'antfu',
        type: 'user',
      },
      width: 800,
      renderer: 'tiers',
      formats: ['json', 'svg', 'png', 'webp'],
      tiers: [
        {
          title: 'Backers',
          preset: tierPresets.base,
        },
        {
          title: 'Sponsors',
          monthlyDollars: 10,
          preset: tierPresets.medium,
        },
      ],
    })
  5. Configure multiple renders in SponsorKit

    main

    You can generate multiple images with different settings (e.g., different sizes or renderers) in a single run using the renders field in your configuration. Each render inherits the top-level configuration settings unless overridden.

    import { defineConfig } from 'sponsorkit'
    
    export default defineConfig({
      github: { /* ... */ },
      width: 800,
      renderer: 'tiers',
    
      renders: [
        {
          name: 'sponsors.tiers',
          formats: ['svg'],
        },
        {
          name: 'sponsors.wide',
          width: 1200,
        },
        {
          name: 'sponsors.circles',
          renderer: 'circles',
          width: 600,
        },
      ],
    })
  6. Configure SponsorKit using defineConfig

    main

    Use defineConfig from sponsorkit to create your configuration object. This object allows you to define sponsorship tiers, handle sponsor merging across platforms, customize rendering outputs, and manage how links and avatars are replaced.

    import { defineConfig, tierPresets } from 'sponsorkit'
    
    export default defineConfig({
      tiers: [
        {
          title: 'Sponsors',
          monthlyDollars: 10,
          preset: tierPresets.medium,
        },
      ],
    })
  7. Set up a Ko-fi webhook server

    main

    To automatically capture Ko-fi sponsorship events, you can start a dedicated HTTP server using startKofiWebhookServer. This server listens for POST requests from Ko-fi and stores the events in a local JSON file.

    Required configuration:

    • verificationToken: The token provided by Ko-fi to validate that incoming requests are authentic.
    • port: The port to listen on (defaults to 3456).
    • host: The host address (defaults to 127.0.0.1).
    • path: The endpoint path where the webhook will be received (defaults to /kofi).
    • dataFile: The path to the JSON file where events will be stored (defaults to ./sponsorkit/kofi-events.json).
    import { startKofiWebhookServer } from './src/providers/kofi';
    
    const server = await startKofiWebhookServer({
      verificationToken: 'YOUR_KOFI_VERIFICATION_TOKEN',
      port: 3456,
      path: '/kofi-webhook',
      dataFile: './data/kofi-events.json'
    });
    
    console.log('Ko-fi webhook server is running');
  8. Use fetchSponsors programmatically

    main

    You can import fetchSponsors to integrate SponsorKit directly into your own TypeScript/JavaScript applications.

    import { fetchSponsors } from 'sponsorkit'
    
    const sponsors = await fetchSponsors({
      github: {
        token: 'YOUR_TOKEN',
        login: 'YOUR_LOGIN',
      },
      // ... other providers
    })
  9. Customize SVG inline CSS

    main

    You can customize the appearance of generated SVG files using the svgInlineCSS configuration key. The default CSS targets the text element and specific SponsorKit classes:

    text {
      font-weight: 300;
      font-size: 14px;
      fill: #777777;
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    }
    .sponsorkit-link {
      cursor: pointer;
    }
    .sponsorkit-tier-title {
      font-weight: 500;
      font-size: 20px;
    }
  10. Configure multiple renders

    main

    The renders array allows you to generate multiple versions of your sponsor graphics in a single run. Each render object can specify:

    • name: A unique identifier for the render.
    • width: The width in pixels.
    • formats: An array of output formats (e.g., ['svg', 'png']).
    • renderer: The specific rendering engine to use (e.g., 'circles').
    • includePastSponsors: Boolean to include or exclude past sponsors in this specific render.
    renders: [
      {
        name: 'sponsors',
        width: 800,
        formats: ['svg', 'png'],
      },
      {
        renderer: 'circles',
        name: 'sponsors-circles',
        width: 1000,
        includePastSponsors: true,
      },
    ],
  11. Replace links and avatars

    main

    You can customize the metadata displayed for sponsors using:

    • replaceLinks: An object mapping original URLs to new URLs (e.g., mapping a GitHub profile to a personal website).
    • replaceAvatars: An object to provide custom avatar mappings.
    replaceLinks: {
      'https://github.com/antfu': 'https://antfu.me',
    },
  12. Configure Tier-based rendering

    main

    When using the 'tiers' renderer, you can define custom tiers using the tiers option in SponsorkitRenderOptions. Each tier defines a minimum monthly dollar amount and can have its own visual styling and composition hooks.

    tiers: [
      {
        monthlyDollars: 100,
        title: 'Gold Tier',
        preset: {
          boxWidth: 200,
          boxHeight: 50,
          avatar: { size: 40 }
        }
      }
    ]