0

Retrieve Balance

Published Oct 30, 2023

Retrieve the account's available and pending balance, per currency, in the smallest currency unit.

Script stripe Verified

The script

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

3
/**
4
 * Retrieve Balance
5
 * Retrieve the account's available and pending balance, per currency, in the smallest currency unit.
6
 */
7
export async function main(auth: RT.Stripe) {
8
  const response = await fetch(`https://api.stripe.com/v1/balance`, {
9
    headers: {
10
      Authorization: `Bearer ${auth.token}`,
11
      Accept: "application/json",
12
      "Stripe-Version": "2025-03-31.basil",
13
    },
14
  })
15

16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19

20
  return await response.json()
21
}
22

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 554 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get balance
    6
     * Retrieves the current account balance, based on the authentication that was used to make the request.
    7
     For a sample request, see Accounting for negative balances.
    8
     */
    9
    export async function main(auth: Stripe, expand: any) {
    10
      const url = new URL(`https://api.stripe.com/v1/balance`);
    11
      encodeParams({ expand }).forEach((v, k) => {
    12
        if (v !== undefined && v !== "") {
    13
          url.searchParams.append(k, v);
    14
        }
    15
      });
    16
      const response = await fetch(url, {
    17
        method: "GET",
    18
        headers: {
    19
          "Content-Type": "application/x-www-form-urlencoded",
    20
          Authorization: "Bearer " + auth.token,
    21
        },
    22
        body: undefined,
    23
      });
    24
      if (!response.ok) {
    25
        const text = await response.text();
    26
        throw new Error(`${response.status} ${text}`);
    27
      }
    28
      return await response.json();
    29
    }
    30
    
    
    31
    function encodeParams(o: any) {
    32
      function iter(o: any, path: string) {
    33
        if (Array.isArray(o)) {
    34
          o.forEach(function (a) {
    35
            iter(a, path + "[]");
    36
          });
    37
          return;
    38
        }
    39
        if (o !== null && typeof o === "object") {
    40
          Object.keys(o).forEach(function (k) {
    41
            iter(o[k], path + "[" + k + "]");
    42
          });
    43
          return;
    44
        }
    45
        data.push(path + "=" + o);
    46
      }
    47
      const data: string[] = [];
    48
      Object.keys(o).forEach(function (k) {
    49
        if (o[k] !== undefined) {
    50
          iter(o[k], k);
    51
        }
    52
      });
    53
      return new URLSearchParams(data.join("&"));
    54
    }
    55