0

Get Templates

by
Published Apr 8, 2025

Get all templates in an account. Filter to request a subset of all templates. Templates can be sorted by the following fields, in ascending and descending order: `id`, `name`, `created`, `updated` Returns a maximum of 10 results per page.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `templates: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 Templates
7
 * Get all templates in an account.
8

9
Filter to request a subset of all templates. Templates can be sorted by the following fields, in ascending and descending order: `id`, `name`, `created`, `updated`
10

11
Returns a maximum of 10 results per page.*Rate limits*:Burst: `10/s`Steady: `150/m`
12

13
 */
14
export async function main(
15
  auth: Klaviyo,
16
  fields_template_: string | undefined,
17
  filter: string | undefined,
18
  page_cursor_: string | undefined,
19
  sort:
20
    | "created"
21
    | "-created"
22
    | "id"
23
    | "-id"
24
    | "name"
25
    | "-name"
26
    | "updated"
27
    | "-updated"
28
    | undefined,
29
  revision: string,
30
) {
31
  const url = new URL(`https://a.klaviyo.com/api/templates`);
32
  for (const [k, v] of [
33
    ["fields[template]", fields_template_],
34
    ["filter", filter],
35
    ["page[cursor]", page_cursor_],
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