0

Upload a logo for the organization

by
Published Apr 8, 2025

Set or replace an organization's logo, by uploading an image file. This endpoint uses the `multipart/form-data` request content type and accepts a file of image type. The file size cannot exceed 10MB. Only the following file content types are supported: `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/x-icon`, `image/vnd.microsoft.icon`.

Script clerk Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Clerk = {
3
  apiKey: string;
4
};
5
type Base64 = string;
6
/**
7
 * Upload a logo for the organization
8
 * Set or replace an organization's logo, by uploading an image file.
9
This endpoint uses the `multipart/form-data` request content type and accepts a file of image type.
10
The file size cannot exceed 10MB.
11
Only the following file content types are supported: `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/x-icon`, `image/vnd.microsoft.icon`.
12
 */
13
export async function main(
14
  auth: Clerk,
15
  organization_id: string,
16
  body: {
17
    uploader_user_id?: string;
18
    file: {
19
      base64: Base64;
20
      type:
21
        | "image/png"
22
        | "image/jpeg"
23
        | "image/gif"
24
        | "application/pdf"
25
        | "appication/json"
26
        | "text/csv"
27
        | "text/plain"
28
        | "audio/mpeg"
29
        | "audio/wav"
30
        | "video/mp4";
31
      name: string;
32
    };
33
  },
34
) {
35
  const url = new URL(
36
    `https://api.clerk.com/v1/organizations/${organization_id}/logo`,
37
  );
38

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