0

Get Images

by
Published Apr 8, 2025

Get all images in an account.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `images:read`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get Images
7
 * Get all images in an account.*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  fields_image_: string | undefined,
13
  filter: string | undefined,
14
  page_cursor_: string | undefined,
15
  page_size_: string | undefined,
16
  sort:
17
    | "format"
18
    | "-format"
19
    | "id"
20
    | "-id"
21
    | "name"
22
    | "-name"
23
    | "size"
24
    | "-size"
25
    | "updated_at"
26
    | "-updated_at"
27
    | undefined,
28
  revision: string,
29
) {
30
  const url = new URL(`https://a.klaviyo.com/api/images`);
31
  for (const [k, v] of [
32
    ["fields[image]", fields_image_],
33
    ["filter", filter],
34
    ["page[cursor]", page_cursor_],
35
    ["page[size]", page_size_],
36
    ["sort", sort],
37
  ]) {
38
    if (v !== undefined && v !== "" && k !== undefined) {
39
      url.searchParams.append(k, v);
40
    }
41
  }
42
  const response = await fetch(url, {
43
    method: "GET",
44
    headers: {
45
      revision: revision,
46
      "Accept": "application/vnd.api+json",
47
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
48
    },
49
    body: undefined,
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57