I am trying to get all the charge data from stripe api. By default, stripe doesn't allow fetching more than 100 data and I have thousands of data there. There's a field in stripe called has_more, which provides info about if there's more data through it's true/false value. Now. I'm trying to automate the process in node js so that, first of all, an API call to fetch 100 data, then it'll check if has_more is true. If true, then it'll take the id of the last object in data and start fetching data after that id and keep doing it untill has_more returns false.
I've done this till now, which gets only the first 100 data. Can anyone give any idea on the next process?
async function getData() {
const data = await axios.get(
`${URL}?created%5Bgte%5D=${created}&created%5Blte%5D=${createdEnd}&limit=${limit}`,
{
headers: {
Authorization: `Bearer ${bearer}`,
"Stripe-Account": `${stripe_account}`,
},
}
);
return data;
}
I'd do:
async function getData(startingAfter) {
let url = `${URL}?created%5Bgte%5D=${created}&created%5Blte%5D=${createdEnd}&limit=${limit}`
// if they pass in a startingAfter param, use it
if (typeof startingAfter !== 'undefined') {
url += `&starting_after=${startingAfter}`
}
const data = await axios.get(url
,
{
headers: {
Authorization: `Bearer ${bearer}`,
"Stripe-Account": `${stripe_account}`,
},
}
);
// if there is more, add the additional items to this list, and
// then set hasMore to false (since we fetched them all)
if (data.hasMore) {
const more = await getData(data[data.length - 1].id);
data.data.push(...more.data)
data.hasMore = false;
}
return data;
}
This will get the first 100 items, see it has more, and then call getData(100) (well, getData('ch_whatever_the_id_is_for_#_100')), which will fetch the next 100 items, then call getData(200), etc. etc. etc., and then your initial call will end up with the full list.
You should install the official stripe-node library and use the library's in-built auto-pagination.
https://stripe.com/docs/api/pagination/auto?lang=node
Also are you using axios on the frontend? You can't do that since that would mean you're exposting your secret Stripe API key on the frontend, listing charges is a backend API call (so you should be able to install and use stripe-node https://github.com/stripe/stripe-node)