Edits history of script submission #9899 for ' Send a event to Klaviyo with a payload (klaviyo)'

  • bun
    import * as wmill from "windmill-client"
    
    export async function main(
      event_name: string,
      email: string,
      first_name: string,
      last_name: string,
      properties: {
        [key: string]: string;
      }
    ) {
      const apiKey = await wmill.getVariable('f/Klaviyo/api_key');
    
      const response = await fetch("https://a.klaviyo.com/api/events/", {
          method: "POST",
          headers: {
            Accept: "application/json",
            "Content-Type": "application/json",
            revision: "2024-07-15",
            Authorization: `Klaviyo-API-Key ${apiKey}`,
          },
          body: JSON.stringify({
            data: {
              type: "event",
              attributes: {
                metric: {
                  data: { type: "metric", attributes: { name: event_name } },
                },
                profile: {
                  data: {
                    type: "profile",
                    attributes: {
                      email: email,
                      first_name: first_name,
                      last_name: last_name,
                    },
                  },
                },
                properties,
              },
            },
          }),
        });
    
      if (!response.ok) {
        throw new Error(`Failed to send event: ${response.statusText}`);
      }
    
      return `Event sent successfully: ${properties.type ? properties.type : "No type specified"}`;
    }
    

    Submitted by rafael gonzález387 566 days ago