0

Create Template

by
Published Apr 8, 2025

Create a new custom HTML template. If there are 1,000 or more templates in an account, creation will fail as there is a limit of 1,000 templates that can be created via the API. Request specific fields using [sparse fieldsets](https://developers.klaviyo.com/en/reference/api_overview#sparse-fieldsets).*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `templates: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
 * Create Template
7
 * Create a new custom HTML template.
8

9
If there are 1,000 or more templates in an account, creation will fail as there is a limit of 1,000 templates
10
that can be created via the API.
11

12
Request specific fields using [sparse fieldsets](https://developers.klaviyo.com/en/reference/api_overview#sparse-fieldsets).*Rate limits*:Burst: `10/s`Steady: `150/m`
13

14
 */
15
export async function main(
16
  auth: Klaviyo,
17
  revision: string,
18
  body: {
19
    data: {
20
      type: "template";
21
      attributes: {
22
        name: string;
23
        editor_type: string;
24
        html?: string;
25
        text?: string;
26
      };
27
    };
28
  },
29
) {
30
  const url = new URL(`https://a.klaviyo.com/api/templates`);
31

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