0

Bulk Create Events

by
Published Apr 8, 2025

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` **Scopes:** `events:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Bulk Create Events
7
 * Create a batch of events for one or more profiles.
8

9
Note that this endpoint allows you to create new profiles or update existing profile properties.
10

11
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.
12

13
Accepts up to 1,000 events per request. The maximum allowed payload size is 5MB.*Rate limits*:Burst: `10/s`Steady: `150/m`
14

15
 */
16
export async function main(
17
  auth: Klaviyo,
18
  revision: string,
19
  body: {
20
    data: {
21
      type: "event-bulk-create-job";
22
      attributes: {
23
        "events-bulk-create": {
24
          data: {
25
            type: "event-bulk-create";
26
            attributes: {
27
              profile: {
28
                data: {
29
                  type: "profile";
30
                  id?: string;
31
                  attributes: {
32
                    email?: string;
33
                    phone_number?: string;
34
                    external_id?: string;
35
                    anonymous_id?: string;
36
                    _kx?: string;
37
                    first_name?: string;
38
                    last_name?: string;
39
                    organization?: string;
40
                    locale?: string;
41
                    title?: string;
42
                    image?: string;
43
                    location?: {
44
                      address1?: string;
45
                      address2?: string;
46
                      city?: string;
47
                      country?: string;
48
                      latitude?: string | number;
49
                      longitude?: string | number;
50
                      region?: string;
51
                      zip?: string;
52
                      timezone?: string;
53
                      ip?: string;
54
                    };
55
                    properties?: {};
56
                  };
57
                  meta?: {
58
                    patch_properties?: {
59
                      append?: {};
60
                      unappend?: {};
61
                      unset?: string | string[];
62
                    };
63
                  };
64
                };
65
              };
66
              events: {
67
                data: {
68
                  type: "event";
69
                  attributes: {
70
                    properties: {};
71
                    time?: string;
72
                    value?: number;
73
                    value_currency?: string;
74
                    unique_id?: string;
75
                    metric: {
76
                      data: {
77
                        type: "metric";
78
                        attributes: { name: string; service?: string };
79
                      };
80
                    };
81
                  };
82
                }[];
83
              };
84
            };
85
          }[];
86
        };
87
      };
88
    };
89
  },
90
) {
91
  const url = new URL(`https://a.klaviyo.com/api/event-bulk-create-jobs`);
92

93
  const response = await fetch(url, {
94
    method: "POST",
95
    headers: {
96
      revision: revision,
97
      "Accept": "application/vnd.api+json",
98
      "Content-Type": "application/vnd.api+json",
99
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
100
    },
101
    body: JSON.stringify(body),
102
  });
103
  if (!response.ok) {
104
    const text = await response.text();
105
    throw new Error(`${response.status} ${text}`);
106
  }
107
  return await response.json();
108
}
109