0

Import deals(creation and updation)

by
Published Apr 8, 2025

Import deals from a CSV file with mapping options.

Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
type Base64 = string;
6
/**
7
 * Import deals(creation and updation)
8
 * Import deals from a CSV file with mapping options.
9
 */
10
export async function main(
11
  auth: Brevo,
12
  body: {
13
    file?: {
14
      base64: Base64;
15
      type:
16
        | "image/png"
17
        | "image/jpeg"
18
        | "image/gif"
19
        | "application/pdf"
20
        | "appication/json"
21
        | "text/csv"
22
        | "text/plain"
23
        | "audio/mpeg"
24
        | "audio/wav"
25
        | "video/mp4";
26
      name: string;
27
    };
28
    mapping?: {};
29
  },
30
) {
31
  const url = new URL(`https://api.brevo.com/v3/crm/deals/import`);
32

33
  const formData = new FormData();
34
  for (const [k, v] of Object.entries(body)) {
35
    if (v !== undefined && v !== "") {
36
      if (["file"].includes(k)) {
37
        const { base64, type, name } = v as {
38
          base64: Base64;
39
          type: string;
40
          name: string;
41
        };
42
        formData.append(
43
          k,
44
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
45
            type,
46
          }),
47
          name,
48
        );
49
      } else {
50
        formData.append(k, String(v));
51
      }
52
    }
53
  }
54
  const response = await fetch(url, {
55
    method: "POST",
56
    headers: {
57
      "api-key": auth.apiKey,
58
    },
59
    body: formData,
60
  });
61
  if (!response.ok) {
62
    const text = await response.text();
63
    throw new Error(`${response.status} ${text}`);
64
  }
65
  return await response.json();
66
}
67