0

Get all active stripe products from your account

by
Published May 26, 2023

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

Script stripe Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
type Stripe = {
4
  token: string;
5
};
6
export async function main(stripeAuth: Stripe) {
7
  let has_more = true;
8
  let starting_after = "";
9
  const products = [];
10

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

19
  return products;
20
}
21

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

33
  return products;
34
}
35

Other submissions
  • Submitted by sindre svendby964 Deno
    Created 398 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