Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get a count of subscribers for a plan from the Stripe API?

I am using the Stripe.net library to make calls against the Stripe API.

I would like to get a total count of subscribers for various Plans but I am not sure if this is possible with the current API and/or the Stripe.NET Library at all.

Can anyone provide any insight as to whether or not this is possible?

like image 718
stephen776 Avatar asked Sep 16 '25 19:09

stephen776


2 Answers

I found that this works: (sorry, this is PHP)

$subscriptions = \Stripe\Subscription::all(array('limit' => 1, 'plan' => 'plan-name-here', 'status' => 'trialing|active|past_due|unpaid|all', 'include[]' => 'total_count'));
echo $subscriptions->total_count;
like image 150
RayJ Avatar answered Sep 19 '25 09:09

RayJ


There's no direct call for this, but it's easy enough to accomplish.

The "List all customers" API call (StripeCustomerService's List() method in Stripe.Net) returns the full JSON object for each customer, including their subscription and plan information. You can easily iterate through that and build your list of subscriber counts.

Note that if you have a lot of users, you'll have to retrieve the customer list in chunks. The API call is capped at 100 records (with a default of 10) and accepts an offset. For easy traversal of the list, the count property in Stripe's JSON response is the total number of customer records.

So for a basic outline, your strategy would be:

  1. Request 100 records via List()
  2. Calculate the number of additional requests required
  3. Process the initial 100 records
  4. Request 100 records via List(), offset by 100 * iteration
  5. Process the current 100 records
  6. Repeat 4 & 5 until records are exhausted
like image 41
colinm Avatar answered Sep 19 '25 09:09

colinm