0

Assign Template to Campaign Message

by
Published Apr 8, 2025

Creates a non-reusable version of the template and assigns it to the message.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `campaigns: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
 * Assign Template to Campaign Message
7
 * Creates a non-reusable version of the template and assigns it to the message.*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  revision: string,
13
  body: {
14
    data: {
15
      type: "campaign-message";
16
      id: string;
17
      relationships?: {
18
        template?: { data?: { type: "template"; id: string } };
19
      };
20
    };
21
  },
22
) {
23
  const url = new URL(
24
    `https://a.klaviyo.com/api/campaign-message-assign-template`,
25
  );
26

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