Creators Access Token

Hello!

I’m using my creator access token to check if a user of my app is subscribed to my patreon. I noticed that this token expires monthly (I think). My currently workflow involves manually updating my app token every month.

  1. Is this typically automated with the refresh token somehow? Seems like a lot of work for me to set up another service to do this, but I guess I could do it.
  2. Is there some other token I should be using that doesn’t expire monthly?

Thanks! Looking forward to hearing about any recommendations or best practices.

Here is how I’m checking for active patrons (another concern is how I’m paging through all users and then filtering after). Does the API have a better way to do this? I see a lot of posts about it in the forum but not any clear recommendations.

export async function getActivePatreons(
  token: string,
  campaignId: string
): Promise<PatreonUser[]> {
  const headers = new Headers({
    Authorization: `Bearer ${token}`,
    "User-Agent": "Asset-Bae",
    "Content-Type": "application/json",
  });
  const init = {
    method: "GET",
    headers: headers,
  };
  const baseUrl = "https://www.patreon.com";

  const url = `${baseUrl}/api/oauth2/v2/campaigns/${campaignId}/members?fields%5Bmember%5D=email,patron_status&page%5Bsize%5D=1000`;

  let activePatrons: PatreonUser[] = [];
  let nextUrl: string | null = url;

  while (nextUrl) {
    const response = await fetch(nextUrl, init);

    if (!response.ok) {
      throw new Error(
        `Failed to fetch patrons: ${response.status} ${response.statusText}`
      );
    }

    const data: ApiResponse = await response.json();

    const currentPatrons = data.data
      .map((member: PatronData) => ({
        name: `${member.attributes.first_name ?? ""} ${
          member.attributes.last_name ?? ""
        }`.trim(),
        email: member.attributes.email,
        patron_status: member.attributes.patron_status,
      }))
      .filter(
        (
          member
        ): member is {
          name: string;
          email: string;
          patron_status: "active_patron";
        } => member.email !== null && member.patron_status === "active_patron"
      );

    activePatrons = activePatrons.concat(currentPatrons);
    nextUrl = data.links?.next || null;
  }

  return activePatrons;
}

Yes, you’ll need to set up automated service to refresh the token at least monthly. You could get a new one every day, or any other period < 30 days.

I run a daily cronjob that gets all tokens <=7 days to expiry and refresh them.

An alternative to getting all your campaign members and looping through the pagination is setting up a webhook to update this info in your database (although based on above I can’t tell if you’re using this dynamically in your nextjs app or storing in database). Then fetch the data from the DB