0

Get Messages for Campaign

by
Published Apr 8, 2025

Return all messages that belong to the given campaign.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `campaigns: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 Messages for Campaign
7
 * Return all messages that belong to the given campaign.*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  id: string,
13
  fields_campaign_message_: string | undefined,
14
  fields_campaign_: string | undefined,
15
  fields_template_: string | undefined,
16
  include: string | undefined,
17
  revision: string,
18
) {
19
  const url = new URL(
20
    `https://a.klaviyo.com/api/campaigns/${id}/campaign-messages`,
21
  );
22
  for (const [k, v] of [
23
    ["fields[campaign-message]", fields_campaign_message_],
24
    ["fields[campaign]", fields_campaign_],
25
    ["fields[template]", fields_template_],
26
    ["include", include],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      revision: revision,
36
      "Accept": "application/vnd.api+json",
37
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47