How to retrieve your Patreons automatically, using Java October 14, 2021


Patreon

By this point, most people know of the crowdfunding platform Patreon. To thank their supporters for their funding, most campains offer some sort of "Patreon recognition", my own included. Although a nice gesture that doesn't cost much, keeping track of your supporters manually can quickly become busywork. The solution is obvious: let's automate!

On that front, there is some good news and there is some bad news. The good news is that Patreon does offer a fairly extensive API. The bad news is that it is not actively supported at the moment and the support is relegated to the community. This means that while there is a way to automatically get your patreons using java, there is some updates to do to be able to do it on the most recent JDK.

Setup

In order to use the official Java client for the Patreon API, you will require two things: an access token and your campaign id. To get both, just follow these steps:

  1. Generate a Patreon Access Token, aka your API key
    1. Login to Patreon with your creator account
    2. Navigate to the "My Clients" configuration page
    3. Click the "Create client" button
    4. Fill-in the form. You only need to provide values for the required fields:
      • App Name: The name of the application that will connect to Patreon
      • Description: A short description of the application
      • App Cateogry: Reason why the application connects to Patreon
      • Redirect URIs: Whilst these will not be used as part of this tutorial, they would be the return URIs your users would get redirected to after authenticating
      • Client API Version: must be set to 1
    5. Click the "Create Client" button
    6. Copy the value under "Creator's Access Token": this is your API key
  2. Get your campaign id
    1. Navigate to your campain's Patreon page, for example https://www.patreon.com/sc_trade_tools
    2. Open the developper's tools (usually done by pressing F12)
    3. In the javascript console tab, paste the following and hit Enter: javascript:prompt('Campaign ID', window.patreon.bootstrap.creator.data.id);
    4. The numerical value appearing in the pop-up is your campaign id

Client code

Example

build.gradle

...
dependencies {
    ...
    implementation ('com.patreon:patreon:0.4.3') {
        // custom built from https://github.com/EtienneLamoureux/patreon-java/tree/url-fix due to https://github.com/Patreon/patreon-java/issues/40
        exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    }
    ...
}
...

PatreonClient.java

import com.patreon.PatreonAPI;
import com.patreon.resources.Pledge;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class PatreonClient {
  private PatreonAPI patreonApi;
  private String campaign;

  public PatreonClient(String campaign, String apiKey) {
    this.patreonApi = new PatreonAPI(apiKey);
    this.campaign = campaign;
  }

  public Collection<String> getPatreonNames() {
    try {
      List<Pledge> pledges = patreonApi.fetchAllPledges(campaign);
      return pledges.parallelStream().filter(n -> n.getDeclinedSince() == null) // only paying Patreons
          .filter(n -> n.getReward().getAmountCents() >= 300) // only $3.00+ tiers
          .sorted((n, m) -> n.getCreatedAt().compareTo(m.getCreatedAt())) // sort by oldest first
          .map(n -> n.getPatron().getFullName()).collect(Collectors.toList());
    } catch (Exception e) {
      return Collections.emptyList();
    }
  }
}

Of course the above example code is provided as a starting point only. Depending on the framework you use, you could implement the following enhancements:

And that's it! Hopefully this has made automating the retrieval of your patreons easier that it was for me. Now we can only hope for the official API support to resume soon so community fixes like these are not required going forward.


Java Tutorial
Last updated on October 14, 2021

— Etienne Lamoureux