0

List Webhook Endpoints

by
Published Oct 30, 2023

List the account's webhook endpoints. 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 Webhook Endpoints
5
 * List the account's webhook endpoints. 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
  limit: number | undefined,
10
  starting_after: string | undefined,
11
  ending_before: string | undefined
12
) {
13
  const url = new URL(`https://api.stripe.com/v1/webhook_endpoints`)
14
  url.search = toForm({ limit, starting_after, ending_before }).toString()
15

16
  const response = await fetch(url, {
17
    headers: {
18
      Authorization: `Bearer ${auth.token}`,
19
      Accept: "application/json",
20
      "Stripe-Version": "2025-03-31.basil",
21
    },
22
  })
23

24
  if (!response.ok) {
25
    throw new Error(`${response.status} ${await response.text()}`)
26
  }
27

28
  return await response.json()
29
}
30

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

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get webhook endpoints
    6
     * Returns a list of your webhook endpoints.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      ending_before: string | undefined,
    11
      expand: any,
    12
      limit: string | undefined,
    13
      starting_after: string | undefined
    14
    ) {
    15
      const url = new URL(`https://api.stripe.com/v1/webhook_endpoints`);
    16
      for (const [k, v] of [
    17
        ["ending_before", ending_before],
    18
        ["limit", limit],
    19
        ["starting_after", starting_after],
    20
      ]) {
    21
        if (v !== undefined && v !== "") {
    22
          url.searchParams.append(k, v);
    23
        }
    24
      }
    25
      encodeParams({ expand }).forEach((v, k) => {
    26
        if (v !== undefined && v !== "") {
    27
          url.searchParams.append(k, v);
    28
        }
    29
      });
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          "Content-Type": "application/x-www-form-urlencoded",
    34
          Authorization: "Bearer " + auth.token,
    35
        },
    36
        body: undefined,
    37
      });
    38
      if (!response.ok) {
    39
        const text = await response.text();
    40
        throw new Error(`${response.status} ${text}`);
    41
      }
    42
      return await response.json();
    43
    }
    44
    
    
    45
    function encodeParams(o: any) {
    46
      function iter(o: any, path: string) {
    47
        if (Array.isArray(o)) {
    48
          o.forEach(function (a) {
    49
            iter(a, path + "[]");
    50
          });
    51
          return;
    52
        }
    53
        if (o !== null && typeof o === "object") {
    54
          Object.keys(o).forEach(function (k) {
    55
            iter(o[k], path + "[" + k + "]");
    56
          });
    57
          return;
    58
        }
    59
        data.push(path + "=" + o);
    60
      }
    61
      const data: string[] = [];
    62
      Object.keys(o).forEach(function (k) {
    63
        if (o[k] !== undefined) {
    64
          iter(o[k], k);
    65
        }
    66
      });
    67
      return new URLSearchParams(data.join("&"));
    68
    }
    69