Get a card expense
One script reply has been approved by the moderators Verified

Get a card expense by its ID.

Created by hugo697 170 days ago
Submitted by hugo697 Bun
Verified 170 days ago
1
//native
2
type Brex = {
3
  token: string;
4
};
5
/**
6
 * Get a card expense
7
 * Get a card expense by its ID.
8
 */
9
export async function main(
10
  auth: Brex,
11
  expense_id: string,
12
  expand__: string | undefined,
13
  load_custom_fields: string | undefined,
14
) {
15
  const url = new URL(
16
    `https://platform.brexapis.com/v1/expenses/card/${expense_id}`,
17
  );
18
  for (const [k, v] of [
19
    ["expand[]", expand__],
20
    ["load_custom_fields", load_custom_fields],
21
  ]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39