0

Create image item using file from device

by
Published Oct 17, 2025

Adds an image item to a board by specifying a file from device.Required scope boards:write Rate limiting Level 2

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 image item using file from device
8
 * Adds an image item to a board by specifying a file from device.Required scope boards:write Rate limiting Level 2
9
 */
10
export async function main(
11
  auth: Miro,
12
  board_id_PlatformFileUpload: string,
13
  body: {
14
    data?: {
15
      title?: string;
16
      altText?: string;
17
      position?: { x?: number; y?: number };
18
      geometry?: { height?: number; width?: number; rotation?: number };
19
      parent?: { id?: string };
20
    };
21
    resource: {
22
      base64: Base64;
23
      type:
24
        | "image/png"
25
        | "image/jpeg"
26
        | "image/gif"
27
        | "application/pdf"
28
        | "appication/json"
29
        | "text/csv"
30
        | "text/plain"
31
        | "audio/mpeg"
32
        | "audio/wav"
33
        | "video/mp4";
34
      name: string;
35
    };
36
  },
37
) {
38
  const url = new URL(
39
    `https://api.miro.com//v2/boards/${board_id_PlatformFileUpload}/images`,
40
  );
41

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