1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Create Campaign |
7 | * Creates a campaign given a set of parameters, then returns it.*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"; |
16 | attributes: { |
17 | name: string; |
18 | audiences: { included?: string[]; excluded?: string[] }; |
19 | send_strategy?: { |
20 | method: string; |
21 | options_static?: { |
22 | datetime: string; |
23 | is_local?: false | true; |
24 | send_past_recipients_immediately?: false | true; |
25 | }; |
26 | options_throttled?: { datetime: string; throttle_percentage: number }; |
27 | options_sto?: { date: string }; |
28 | }; |
29 | send_options?: { use_smart_sending?: false | true }; |
30 | tracking_options?: |
31 | | { |
32 | add_tracking_params?: false | true; |
33 | custom_tracking_params?: |
34 | | { |
35 | type: "dynamic"; |
36 | value: |
37 | | "campaign_id" |
38 | | "campaign_name" |
39 | | "campaign_name_id" |
40 | | "campaign_name_send_day" |
41 | | "email_subject" |
42 | | "group_id" |
43 | | "group_name" |
44 | | "group_name_id" |
45 | | "link_alt_text" |
46 | | "message_type" |
47 | | "profile_external_id" |
48 | | "profile_id"; |
49 | name: string; |
50 | } |
51 | | { type: "static"; value: string; name: string }[]; |
52 | is_tracking_clicks?: false | true; |
53 | is_tracking_opens?: false | true; |
54 | } |
55 | | { |
56 | add_tracking_params?: false | true; |
57 | custom_tracking_params?: |
58 | | { |
59 | type: "dynamic"; |
60 | value: |
61 | | "campaign_id" |
62 | | "campaign_name" |
63 | | "campaign_name_id" |
64 | | "campaign_name_send_day" |
65 | | "email_subject" |
66 | | "group_id" |
67 | | "group_name" |
68 | | "group_name_id" |
69 | | "link_alt_text" |
70 | | "message_type" |
71 | | "profile_external_id" |
72 | | "profile_id"; |
73 | name: string; |
74 | } |
75 | | { type: "static"; value: string; name: string }[]; |
76 | }; |
77 | "campaign-messages": { |
78 | data: { |
79 | type: "campaign-message"; |
80 | attributes: { |
81 | channel: string; |
82 | label?: string; |
83 | content?: |
84 | | { |
85 | subject?: string; |
86 | preview_text?: string; |
87 | from_email?: string; |
88 | from_label?: string; |
89 | reply_to_email?: string; |
90 | cc_email?: string; |
91 | bcc_email?: string; |
92 | } |
93 | | { body?: string }; |
94 | render_options?: { |
95 | shorten_links?: false | true; |
96 | add_org_prefix?: false | true; |
97 | add_info_link?: false | true; |
98 | add_opt_out_language?: false | true; |
99 | }; |
100 | }; |
101 | }[]; |
102 | }; |
103 | }; |
104 | }; |
105 | }, |
106 | ) { |
107 | const url = new URL(`https://a.klaviyo.com/api/campaigns`); |
108 |
|
109 | const response = await fetch(url, { |
110 | method: "POST", |
111 | headers: { |
112 | revision: revision, |
113 | "Accept": "application/vnd.api+json", |
114 | "Content-Type": "application/vnd.api+json", |
115 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
116 | }, |
117 | body: JSON.stringify(body), |
118 | }); |
119 | if (!response.ok) { |
120 | const text = await response.text(); |
121 | throw new Error(`${response.status} ${text}`); |
122 | } |
123 | return await response.json(); |
124 | } |
125 |
|