0

Create items in bulk using file from device

by
Published Oct 17, 2025

Adds different types of items to a board using files from a device.

Script miro Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Miro = {
3
  token: string;
4
};
5
type Base64 = string;
6
/**
7
 * Create items in bulk using file from device
8
 * Adds different types of items to a board using files from a device.
9
 */
10
export async function main(
11
  auth: Miro,
12
  board_id: string,
13
  body: {
14
    data: {
15
      base64: Base64;
16
      type:
17
        | "image/png"
18
        | "image/jpeg"
19
        | "image/gif"
20
        | "application/pdf"
21
        | "appication/json"
22
        | "text/csv"
23
        | "text/plain"
24
        | "audio/mpeg"
25
        | "audio/wav"
26
        | "video/mp4";
27
      name: string;
28
    };
29
    resources: string[];
30
  },
31
) {
32
  const url = new URL(
33
    `https://api.miro.com//v2/boards/${board_id}/items/bulk`,
34
  );
35

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