Edits history of script submission #12272 for ' Create Campaign (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Create Campaign
     * Creates a campaign given a set of parameters, then returns it.*Rate limits*:Burst: `10/s`Steady: `150/m`
    
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "campaign";
          attributes: {
            name: string;
            audiences: { included?: string[]; excluded?: string[] };
            send_strategy?: {
              method: string;
              options_static?: {
                datetime: string;
                is_local?: false | true;
                send_past_recipients_immediately?: false | true;
              };
              options_throttled?: { datetime: string; throttle_percentage: number };
              options_sto?: { date: string };
            };
            send_options?: { use_smart_sending?: false | true };
            tracking_options?:
              | {
                  add_tracking_params?: false | true;
                  custom_tracking_params?:
                    | {
                        type: "dynamic";
                        value:
                          | "campaign_id"
                          | "campaign_name"
                          | "campaign_name_id"
                          | "campaign_name_send_day"
                          | "email_subject"
                          | "group_id"
                          | "group_name"
                          | "group_name_id"
                          | "link_alt_text"
                          | "message_type"
                          | "profile_external_id"
                          | "profile_id";
                        name: string;
                      }
                    | { type: "static"; value: string; name: string }[];
                  is_tracking_clicks?: false | true;
                  is_tracking_opens?: false | true;
                }
              | {
                  add_tracking_params?: false | true;
                  custom_tracking_params?:
                    | {
                        type: "dynamic";
                        value:
                          | "campaign_id"
                          | "campaign_name"
                          | "campaign_name_id"
                          | "campaign_name_send_day"
                          | "email_subject"
                          | "group_id"
                          | "group_name"
                          | "group_name_id"
                          | "link_alt_text"
                          | "message_type"
                          | "profile_external_id"
                          | "profile_id";
                        name: string;
                      }
                    | { type: "static"; value: string; name: string }[];
                };
            "campaign-messages": {
              data: {
                type: "campaign-message";
                attributes: {
                  channel: string;
                  label?: string;
                  content?:
                    | {
                        subject?: string;
                        preview_text?: string;
                        from_email?: string;
                        from_label?: string;
                        reply_to_email?: string;
                        cc_email?: string;
                        bcc_email?: string;
                      }
                    | { body?: string };
                  render_options?: {
                    shorten_links?: false | true;
                    add_org_prefix?: false | true;
                    add_info_link?: false | true;
                    add_opt_out_language?: false | true;
                  };
                };
              }[];
            };
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/campaigns`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          revision: revision,
          "Accept": "application/vnd.api+json",
          "Content-Type": "application/vnd.api+json",
          Authorization: "Klaviyo-API-Key " + auth.apiKey,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 428 days ago