Is it possible to query for datetime ranges with the Stripe API?
-
10-06-2021 - |
Question
Using the Stripe API, I'd like to be able to query for date ranges or, failing that, dates that are greater than or less than some arbitrary date.
I know that I can query for something based on an exact date, e.g.,:
https://api.stripe.com/v1/events?created=1336503409
But I want something along the lines of...
# Search for events where `created` is greater than the epoch time: 1336503400
https://api.stripe.com/v1/events?created__gt=1336503400
Solution
Yes. From https://stripe.com/docs/api?lang=curl#list_events
created: A filter on the list based on the events created date. The value can be a string with an exact UTC timestamp, or it can be a dictionary with the following options:
gt (optional) Return values should have been created after this timestamp.
gte (optional) Return values should have been created after or equal to this timestamp.
lt (optional) Return values should have been created before this timestamp.
lte (optional) Return values should have been created before or equal to this timestamp.
So with curl, you can build a request like this:
curl "https://api.stripe.com/v1/events?created%5Blt%5D=1337923293" \
-u ${YOUR_API_KEY}:
The unescaped query parameter is created[lt]=1337923293
.
OTHER TIPS
If you're looking how to do so with the ruby client, here it is:
Stripe::Charge.all(limit: 100, 'created[lt]' => timestamps })
Along the same lines. You can achieve the same thing with the Python client like so:
import stripe
from datetime import datetime, timedelta
my_date = '1/31/2011'
my_timestamp = datetime.strptime(my_date, "%m/%d/%Y").timestamp()
stripe.Charge.all(created={'lt': my_timestamp})
For those of us using the Go Api, both formats below works for selecting a range:
params := &stripe.ChargeListParams{}
params.Filters.AddFilter("created", "lt", strconv.Itoa(1592217599))
params.Filters.AddFilter("created", "gt", strconv.Itoa(1591612124))
i := charge.List(params)
params := &stripe.ChargeListParams{}
params.Filters.AddFilter("created[lt]", "", strconv.Itoa(1592217599))
params.Filters.AddFilter("created[gt]", "", strconv.Itoa(1591612124))
i := charge.List(params)