Post accounts account login links

Creates a single-use login link for an Express account to access their Stripe dashboard. You may only create login links for Express accounts connected to your platform.

Script stripe Verified

by hugo697 ยท 10/30/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 368 days ago
1
type Stripe = {
2
  token: string;
3
};
4
/**
5
 * Post accounts account login links
6
 * Creates a single-use login link for an Express account to access their Stripe dashboard.
7

8
You may only create login links for Express accounts connected to your platform.
9
 */
10
export async function main(
11
  auth: Stripe,
12
  account: string,
13
  body: { expand?: string[] }
14
) {
15
  const url = new URL(
16
    `https://api.stripe.com/v1/accounts/${account}/login_links`
17
  );
18

19
  const response = await fetch(url, {
20
    method: "POST",
21
    headers: {
22
      "Content-Type": "application/x-www-form-urlencoded",
23
      Authorization: "Bearer " + auth.token,
24
    },
25
    body: encodeParams(body),
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33

34
function encodeParams(o: any) {
35
  function iter(o: any, path: string) {
36
    if (Array.isArray(o)) {
37
      o.forEach(function (a) {
38
        iter(a, path + "[]");
39
      });
40
      return;
41
    }
42
    if (o !== null && typeof o === "object") {
43
      Object.keys(o).forEach(function (k) {
44
        iter(o[k], path + "[" + k + "]");
45
      });
46
      return;
47
    }
48
    data.push(path + "=" + o);
49
  }
50
  const data: string[] = [];
51
  Object.keys(o).forEach(function (k) {
52
    if (o[k] !== undefined) {
53
      iter(o[k], k);
54
    }
55
  });
56
  return new URLSearchParams(data.join("&"));
57
}
58