0

Create or Update Push Token

by
Published Apr 8, 2025

Create or update a push token. This endpoint can be used to migrate push tokens from another platform to Klaviyo. Please use our mobile SDKs ([iOS](https://github.com/klaviyo/klaviyo-swift-sdk) and [Android](https://github.com/klaviyo/klaviyo-android-sdk)) to create push tokens from users' devices.*Rate limits*:Burst: `75/s`Steady: `700/m` **Scopes:** `profiles:write` `push-tokens: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
 * Create or Update Push Token
7
 * Create or update a push token.
8

9
This endpoint can be used to migrate push tokens from another platform to Klaviyo. Please use our mobile SDKs ([iOS](https://github.com/klaviyo/klaviyo-swift-sdk) and [Android](https://github.com/klaviyo/klaviyo-android-sdk)) to create push tokens from users' devices.*Rate limits*:Burst: `75/s`Steady: `700/m`
10

11
 */
12
export async function main(
13
  auth: Klaviyo,
14
  revision: string,
15
  body: {
16
    data: {
17
      type: "push-token";
18
      attributes: {
19
        token: string;
20
        platform: "android" | "ios";
21
        enablement_status?:
22
          | "AUTHORIZED"
23
          | "DENIED"
24
          | "NOT_DETERMINED"
25
          | "PROVISIONAL"
26
          | "UNAUTHORIZED";
27
        vendor: "apns" | "fcm";
28
        background?: "DENIED" | "AVAILABLE" | "RESTRICTED";
29
        device_metadata?: {
30
          device_id?: string;
31
          klaviyo_sdk?:
32
            | "android"
33
            | "flutter_community"
34
            | "react_native"
35
            | "swift";
36
          sdk_version?: string;
37
          device_model?: string;
38
          os_name?: "android" | "ios" | "ipados" | "macos" | "tvos";
39
          os_version?: string;
40
          manufacturer?: string;
41
          app_name?: string;
42
          app_version?: string;
43
          app_build?: string;
44
          app_id?: string;
45
          environment?: "debug" | "release";
46
        };
47
        profile: {
48
          data: {
49
            type: "profile";
50
            id?: string;
51
            attributes: {
52
              phone_number?: string;
53
              external_id?: string;
54
              anonymous_id?: string;
55
              _kx?: string;
56
              first_name?: string;
57
              last_name?: string;
58
              organization?: string;
59
              locale?: string;
60
              title?: string;
61
              image?: string;
62
              location?: {
63
                address1?: string;
64
                address2?: string;
65
                city?: string;
66
                country?: string;
67
                latitude?: string | number;
68
                longitude?: string | number;
69
                region?: string;
70
                zip?: string;
71
                timezone?: string;
72
                ip?: string;
73
              };
74
              properties?: {};
75
              meta?: {
76
                patch_properties?: {
77
                  append?: {};
78
                  unappend?: {};
79
                  unset?: string | string[];
80
                };
81
              };
82
              email?: string;
83
            };
84
          };
85
        };
86
      };
87
    };
88
  },
89
) {
90
  const url = new URL(`https://a.klaviyo.com/api/push-tokens`);
91

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