0

Update Campaign Message

by
Published Apr 8, 2025

Update a campaign 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
 * Update Campaign Message
7
 * Update a campaign message*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  id: string,
13
  revision: string,
14
  body: {
15
    data: {
16
      type: "campaign-message";
17
      id: string;
18
      attributes: {
19
        label?: string;
20
        content?:
21
          | {
22
              subject?: string;
23
              preview_text?: string;
24
              from_email?: string;
25
              from_label?: string;
26
              reply_to_email?: string;
27
              cc_email?: string;
28
              bcc_email?: string;
29
            }
30
          | { body?: string };
31
        render_options?: {
32
          shorten_links?: false | true;
33
          add_org_prefix?: false | true;
34
          add_info_link?: false | true;
35
          add_opt_out_language?: false | true;
36
        };
37
      };
38
    };
39
  },
40
) {
41
  const url = new URL(`https://a.klaviyo.com/api/campaign-messages/${id}`);
42

43
  const response = await fetch(url, {
44
    method: "PATCH",
45
    headers: {
46
      revision: revision,
47
      "Accept": "application/vnd.api+json",
48
      "Content-Type": "application/vnd.api+json",
49
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
50
    },
51
    body: JSON.stringify(body),
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59