0
Get all active stripe products from your account
One script reply has been approved by the moderators Verified

returns all your active stripe products. Docs: https://stripe.com/docs/api/products/list?lang=curl

Created by sindre svendby964 325 days ago Viewed 3830 times
0
Submitted by sindre svendby964 Deno
Verified 325 days ago
1
type Stripe = {
2
  token: string;
3
};
4
export async function main(stripeAuth: Stripe) {
5
  let has_more = true;
6
  let starting_after = "";
7
  const products = [];
8

9
  // if you have a lot of products (> 200), you may want to consider to throttle.
10
  while (has_more) {
11
    const res = await getStripeProductsIds(stripeAuth.token, starting_after);
12
    has_more = res.has_more;
13
    starting_after = res.data[res.data.length - 1].id;
14
    products.push(...res.data);
15
  }
16

17
  return products;
18
}
19

20
async function getStripeProductsIds(stripeKey: string, starting_after = "") {
21
  let url = "https://api.stripe.com/v1/products?limit=10";
22
  if (starting_after) {
23
    url += `&starting_after=${starting_after}`;
24
  }
25
  const products = await fetch(url, {
26
    headers: {
27
      Authorization: `Bearer ${stripeKey}`,
28
    },
29
  }).then((res) => res.json());
30

31
  return products;
32
}
33