Plain Dev Blog
← Back to all posts

Retrieve more records than the limit Stripe API

·

Retrieve more records than the limit Stripe API

For every resource from the API you can query results using the list method for almost every resource you can get a list all resource…well not all…

When you are retrieving results with the list method the maximum results that can be returned from the request is 100:

Let’s take the prices resource as an example:

// GET /v1/prices

$stripe = new \Stripe\StripeClient('sk_test_51RRquARcy99RV4czPtcirlPrgNttCr026zIC57B3Y9ZsYW6LWUv0sZv2sOgizGtNGijy6sMFPubOpO1bqTuM2lRg00t5TbV7SG');

$prices = $stripe->prices->all(['limit' => 3]);

as seen in the example from the the Stripe documentation there is a limit parameter which is set to 3, which will retrieve only 3 results from the API.

You can only get 100, but what if you have a lot more results than that?

Well Stripe got you covered in this one: start_after :

A cursor for use in pagination. starting_after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include starting_after=obj_foo in order to fetch the next page of the list.

in the beginning I thought it should be a timestamp(another evidence that you should absolutely always read the docs!!!) for when the resource was created, but then I found out that it should be the ID of the last resource from the initial 100 results request you made.

// GET /v1/prices

$stripe = new \Stripe\StripeClient('sk_test_51RRquARcy99RV4czPtcirlPrgNttCr026zIC57B3Y9ZsYW6LWUv0sZv2sOgizGtNGijy6sMFPubOpO1bqTuM2lRg00t5TbV7SG');

$prices = $stripe->prices->all(['limit' => 100, 'start_after' => 'price_1MoBy5LkdIwHu7ixZhnattbh']);

now the results returned will be the 100 resources after the ID you provided, knowing the last ID you can query as much as you need regardless of the total count of the resources in your account.

Or because the Stripe team are very good people you can use:

autoPagingIterator

It does the pagination for you and you don't need to worry about it, you can read more about it here.

Conclusion

The Stripe API provides everything you need, you just need understand how to use it(By reading the documentation!). Experiment with it, you’d be surprised.