0

List Products

by
Published May 26, 2023

List products, newest first. `active` filters on active or archived products. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true.

Script stripe Verified

The script

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

3
/**
4
 * List Products
5
 * List products, newest first. `active` filters on active or archived products. Returns one page (`limit` 1-100, default 10); pass the last object's id as `starting_after` for the next page while `has_more` is true.
6
 */
7
export async function main(
8
  auth: RT.Stripe,
9
  active: "true" | "false" | undefined,
10
  limit: number | undefined,
11
  starting_after: string | undefined,
12
  ending_before: string | undefined
13
) {
14
  const url = new URL(`https://api.stripe.com/v1/products`)
15
  url.search = toForm({
16
    active,
17
    limit,
18
    starting_after,
19
    ending_before,
20
  }).toString()
21

22
  const response = await fetch(url, {
23
    headers: {
24
      Authorization: `Bearer ${auth.token}`,
25
      Accept: "application/json",
26
      "Stripe-Version": "2025-03-31.basil",
27
    },
28
  })
29

30
  if (!response.ok) {
31
    throw new Error(`${response.status} ${await response.text()}`)
32
  }
33

34
  return await response.json()
35
}
36

37
// Stripe takes form-encoded params with bracketed nesting: metadata[key]=v, items[0][price]=p
38
function toForm(
39
  params: { [key: string]: any },
40
  prefix = "",
41
  out = new URLSearchParams()
42
) {
43
  for (const [key, value] of Object.entries(params)) {
44
    if (value == null || value === "") continue
45
    const name = prefix ? `${prefix}[${key}]` : key
46
    if (typeof value === "object") toForm(value, name, out)
47
    else out.append(name, String(value))
48
  }
49
  return out
50
}
51

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