0

Upload Image From URL

by
Published Apr 8, 2025

Import an image from a url or data uri. If you want to upload an image from a file, use the Upload Image From File endpoint instead.*Rate limits*:Burst: `3/s`Steady: `100/m`Daily: `100/d` **Scopes:** `images:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Upload Image From URL
7
 * Import an image from a url or data uri.
8

9
If you want to upload an image from a file, use the Upload Image From File endpoint instead.*Rate limits*:Burst: `3/s`Steady: `100/m`Daily: `100/d`
10

11
 */
12
export async function main(
13
  auth: Klaviyo,
14
  revision: string,
15
  body: {
16
    data: {
17
      type: "image";
18
      attributes: {
19
        name?: string;
20
        import_from_url: string;
21
        hidden?: false | true;
22
      };
23
    };
24
  },
25
) {
26
  const url = new URL(`https://a.klaviyo.com/api/images`);
27

28
  const response = await fetch(url, {
29
    method: "POST",
30
    headers: {
31
      revision: revision,
32
      "Accept": "application/vnd.api+json",
33
      "Content-Type": "application/vnd.api+json",
34
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44