Edits history of script submission #12252 for ' Assign Template to Campaign Message (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Assign Template to Campaign Message
     * Creates a non-reusable version of the template and assigns it to the message.*Rate limits*:Burst: `10/s`Steady: `150/m`
    
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "campaign-message";
          id: string;
          relationships?: {
            template?: { data?: { type: "template"; id: string } };
          };
        };
      },
    ) {
      const url = new URL(
        `https://a.klaviyo.com/api/campaign-message-assign-template`,
      );
    
      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