Edits history of script submission #12257 for ' Bulk Create Events (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Bulk Create Events
     * Create a batch of events for one or more profiles.
    
    Note that this endpoint allows you to create new profiles or update existing profile properties.
    
    At a minimum, profile and metric objects should include at least one profile identifier (e.g., `id`, `email`, or `phone_number`) and the metric `name`, respectively.
    
    Accepts up to 1,000 events per request. The maximum allowed payload size is 5MB.*Rate limits*:Burst: `10/s`Steady: `150/m`
    
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "event-bulk-create-job";
          attributes: {
            "events-bulk-create": {
              data: {
                type: "event-bulk-create";
                attributes: {
                  profile: {
                    data: {
                      type: "profile";
                      id?: string;
                      attributes: {
                        email?: string;
                        phone_number?: string;
                        external_id?: string;
                        anonymous_id?: string;
                        _kx?: string;
                        first_name?: string;
                        last_name?: string;
                        organization?: string;
                        locale?: string;
                        title?: string;
                        image?: string;
                        location?: {
                          address1?: string;
                          address2?: string;
                          city?: string;
                          country?: string;
                          latitude?: string | number;
                          longitude?: string | number;
                          region?: string;
                          zip?: string;
                          timezone?: string;
                          ip?: string;
                        };
                        properties?: {};
                      };
                      meta?: {
                        patch_properties?: {
                          append?: {};
                          unappend?: {};
                          unset?: string | string[];
                        };
                      };
                    };
                  };
                  events: {
                    data: {
                      type: "event";
                      attributes: {
                        properties: {};
                        time?: string;
                        value?: number;
                        value_currency?: string;
                        unique_id?: string;
                        metric: {
                          data: {
                            type: "metric";
                            attributes: { name: string; service?: string };
                          };
                        };
                      };
                    }[];
                  };
                };
              }[];
            };
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/event-bulk-create-jobs`);
    
      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