1

Send a event to Klaviyo with a payload

by
Published Nov 20, 2024

Send a event to Klaviyo with a payload, you can use the event as a trigger and the event data as data for the campaign

Script klaviyo
  • Submitted by rafael gonzález387 Bun
    Created 566 days ago
    1
    import * as wmill from "windmill-client"
    2
    
    
    3
    export async function main(
    4
      event_name: string,
    5
      email: string,
    6
      first_name: string,
    7
      last_name: string,
    8
      properties: {
    9
        [key: string]: string;
    10
      }
    11
    ) {
    12
      const apiKey = await wmill.getVariable('f/Klaviyo/api_key');
    13
    
    
    14
      const response = await fetch("https://a.klaviyo.com/api/events/", {
    15
          method: "POST",
    16
          headers: {
    17
            Accept: "application/json",
    18
            "Content-Type": "application/json",
    19
            revision: "2024-07-15",
    20
            Authorization: `Klaviyo-API-Key ${apiKey}`,
    21
          },
    22
          body: JSON.stringify({
    23
            data: {
    24
              type: "event",
    25
              attributes: {
    26
                metric: {
    27
                  data: { type: "metric", attributes: { name: event_name } },
    28
                },
    29
                profile: {
    30
                  data: {
    31
                    type: "profile",
    32
                    attributes: {
    33
                      email: email,
    34
                      first_name: first_name,
    35
                      last_name: last_name,
    36
                    },
    37
                  },
    38
                },
    39
                properties,
    40
              },
    41
            },
    42
          }),
    43
        });
    44
    
    
    45
      if (!response.ok) {
    46
        throw new Error(`Failed to send event: ${response.statusText}`);
    47
      }
    48
    
    
    49
      return `Event sent successfully: ${properties.type ? properties.type : "No type specified"}`;
    50
    }
    51