How to fix the Patreon API only returning the 20 oldest Patrons

How to fix the Patreon API only returning the 20 oldest Patrons

Recently, we’ve been hooking the Patreon API to our blog, so that we can set up a page for our Patrons properly. Along the way, we ran into a snag that has not been properly-documented online. If you’ve found this article, I hope this helps save you some time.

The problem

What we were trying to do is retrieve our Patron list. To do so, we queried the following URL:

https://www.patreon.com/api/oauth2/v2/campaigns/1231234/members?fields[member]=full_name,patron_status

This was supposed to return all of our Patrons. Instead, it only returned our oldest 20 Patrons. To help give us an idea of what’s going on, we dumped the returned data using var_dump() in PHP:

array(3) {
  ["data"]=>
  array(20) {
    [PATRON DATA REDACTED]
  }
  ["links"]=>
  array(1) {
    ["next"]=>
    string(152) "https://www.patreon.com/api/oauth2/v2/campaigns/3080605/members?fields%5Bmember%5D=full_name%2Cpatron_status&page%5Bcursor%5D=01XVLl9422U226PB6AedxoyFlv"
  }
  ["meta"]=>
  array(1) {
    ["pagination"]=>
    array(2) {
      ["cursors"]=>
      array(1) {
        ["next"]=>
        string(26) "01XVLl9422U226PB6AedxoyFlv"
      }
      ["total"]=>
      int(27)
    }
  }
}

Notice a few things in the returned data above:

  1. Only 20 Patrons are returned, even though the data tells us that there are 27 Patrons.
  2. There is a URL returned in $array['links']['next'], and a cursor hash returned in $array['meta']['pagination']['cursors']['next'].

Article continues after the advertisement:


The solution

After looking at the data dump, the solution became obvious. We have to make multiple queries!

Making multiple queries

Basically, instead of making just a single-query to our URL. We have to continue making queries to the URL given in $array['links']['next'] until the Patreon API stops returning URLs to us.

In our case above, making another query to the returned URL above will retrieve the remaining 7 Patrons. Once there is no more data, the data returned will not contain a $array['links']['next'] item. Here is the data dump for our last 7 Patrons.

array(2) {
  ["data"]=>
  array(7) {
    [PATRON DATA REDACTED]
  }
  ["meta"]=>
  array(1) {
    ["pagination"]=>
    array(1) {
      ["total"]=>
      int(27)
    }
  }
}

If you prefer to build your own “next” URLs, you can also use the cursor hash returned in $array['meta']['pagination']['cursors']['next'] to form your own query. You just need to add an additional page[cursor] parameter to your original query:

https://www.patreon.com/api/oauth2/v2/campaigns/1231234/members?fields[member]=full_name,patron_status&page[cursor]=YOUR_NEXT_CURSOR

Increasing the query limit

If you are rushing and want a quick, dirty fix, you can simply just set the number of results to return.

https://www.patreon.com/api/oauth2/v2/campaigns/1231234/members?fields[member]=full_name,patron_status&page[count]=100

This is not a suitable long-term solution though, for obvious reasons.

Conclusion

I hope this short article helped you! If you’ve got any feedback, feel free to leave us a comment right below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: You can use Markdown to format your comments.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.

This site uses Akismet to reduce spam. Learn how your comment data is processed.