Sort order for pagination

749 views
Skip to first unread message

Michaël Gallego

unread,
Oct 25, 2014, 9:26:00 AM10/25/14
to api-d...@lists.stripe.com
Hi,

Currently, the biggest limitation in pagination is that everything is 
returned from newest to oldest. Sometimes, it makes much more sense to have 
the opposite order. However, I could not find any way to do it using the 
API. Is there some kind of hidden parameter to achieve this? Currently, the 
only way is to do a first API call to get total_count, then using the old 
offset parameter to get the ID of the last element, and then using 
ending_before, but kind of hacky ;).

Thanks!

Jim Danz

unread,
Oct 30, 2014, 7:03:33 PM10/30/14
to api-d...@lists.stripe.com
Hi Michaël,

This is a reasonable ask, but there is not currently a hidden
parameter to achieve it or anything like that.

You may have to forgive me for asking, but would you be up for
providing any more color around what use case you have for going
backwards like this? Is it related to building a model / timeline of
what has happened to a customer, or such?

We definitely want to be able to support people incrementally pulling
in recently-created objects, which ending_before endeavors to support.
But we don't currently have support for starting at the actual end of
a collection.

Cheers,
Jim
> --
> You received this message because you are subscribed to the Google Groups
> "Stripe API Discussion" group.
> To post to this group, send email to api-d...@lists.stripe.com.
> Visit this group at
> http://groups.google.com/a/lists.stripe.com/group/api-discuss/.
>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to api-discuss...@lists.stripe.com.

Michaël Gallego

unread,
Oct 31, 2014, 4:42:48 AM10/31/14
to api-d...@lists.stripe.com
Hi Jim,

As you have guessed, the use case is to be able to incrementally pulling objects and recreating things like subscription history. As this is done from invoices, it's actually much simpler to start from oldest to newest than the opposite order.

Jim Danz

unread,
Oct 31, 2014, 11:54:44 PM10/31/14
to api-d...@lists.stripe.com
Makes sense. Definitely a valid suggestion that we will keep in mind
and consider! Thanks for providing the further color.

Michaël Gallego

unread,
Nov 6, 2014, 1:20:18 PM11/6/14
to api-d...@lists.stripe.com
Would you have an approximate date for this feature (is it more like in days, weeks or months?). Just to know if I try to hack around the offset parameter to get the oldest invoice (can you please confirm me that "offset" parameter will still be supported in the meantime?)

Jim Danz

unread,
Nov 7, 2014, 7:50:21 PM11/7/14
to api-d...@lists.stripe.com
I can confirm that offset will be supported forever or for some
approximation of forever.

As for the reverse sorting order, the frank answer is that this is
still floating at the level of "reasonable idea that we think could be
worth exploring further" and not something that we have scheduled. I
realize that that's the Wrong Answer but hope that it helps you think
about it.

Jim

Michaël Gallego

unread,
Nov 8, 2014, 5:07:02 AM11/8/14
to api-d...@lists.stripe.com
Hi Jim,

Thank you. I've been able to workaround this, and I'm sorting them server side because processing. Require a bit more API call but that's not that dramatic. Thanks!

Oliver Nassar

unread,
Jan 7, 2015, 1:29:48 AM1/7/15
to api-d...@lists.stripe.com
Hi Michaël,
Any chance you could point me to the old "offset" param?
Wondering if/how I can use that to perform something similar to what you are with the newest PHP SDK

Michaël Gallego

unread,
Jan 7, 2015, 6:25:38 AM1/7/15
to api-d...@lists.stripe.com
Hi Oliver,

I'm not using the offset param. But with the starting_after. I'd suggest you using my own library instead of official PHP SDK (https://github.com/zf-fr/zfr-stripe). With it, you'll have an automatic iterator:

$customers = $stripeClient->getCustomersIterator()->toArray(); // This will automatically do all the needed call and handle the starting_after automatically for you
$customers  = array_reverse($customers);

This will therefore give you a resource in opposite order. The issue is that it forces you to fetch ALL the resources. If you can use the offset parameter, maybe what you can do is:

1. Do a first API call with include[]=total_count, and a limit of 1.
2. Once you have the total_count, do an offset of the total_count - 1. This will gives you the latest resource id.
3. Iterate backward using the ending_before.

That's obviously a bit complicated, but those are the only oslution for now.

Oliver Nassar

unread,
Jan 7, 2015, 11:15:57 AM1/7/15
to api-d...@lists.stripe.com
Thanks for your help Michaël
I'm more curious: is the param you're passing in <offset> with a value that defines where to start the query?
I can't find any documentation on that



--
You received this message because you are subscribed to a topic in the Google Groups "Stripe API Discussion" group.

Michaël Gallego

unread,
Jan 7, 2015, 11:51:19 AM1/7/15
to api-d...@lists.stripe.com
Offset is actually not supported anymore and does not appear neither on Stripe API. I've also removed it from my library. Stripe team told us that offset should still be supported for backward-compatibility reason, but I'd suggest you to not use it, although offset still allows use case that are not possible with offset.

Oliver Nassar

unread,
Jan 8, 2015, 1:14:21 AM1/8/15
to api-d...@lists.stripe.com
Thanks for your help Michaël :)

On Wed, Jan 7, 2015 at 11:51 AM, Michaël Gallego <mic.g...@gmail.com> wrote:
Offset is actually not supported anymore and does not appear neither on Stripe API. I've also removed it from my library. Stripe team told us that offset should still be supported for backward-compatibility reason, but I'd suggest you to not use it, although offset still allows use case that are not possible with offset.

--
Reply all
Reply to author
Forward
0 new messages