0

Render Template

by
Published Apr 8, 2025

Render a template with the given template ID and context attribute.

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Render Template
7
 * Render a template with the given template ID and context attribute.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  revision: string,
12
  body: { data: { type: "template"; id: string; attributes: { context: {} } } },
13
) {
14
  const url = new URL(`https://a.klaviyo.com/api/template-render`);
15

16
  const response = await fetch(url, {
17
    method: "POST",
18
    headers: {
19
      revision: revision,
20
      "Accept": "application/vnd.api+json",
21
      "Content-Type": "application/vnd.api+json",
22
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
23
    },
24
    body: JSON.stringify(body),
25
  });
26
  if (!response.ok) {
27
    const text = await response.text();
28
    throw new Error(`${response.status} ${text}`);
29
  }
30
  return await response.json();
31
}
32