Edits history of script submission #12289 for ' Create or Update Push Token (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Create or Update Push Token
     * 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`
    
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "push-token";
          attributes: {
            token: string;
            platform: "android" | "ios";
            enablement_status?:
              | "AUTHORIZED"
              | "DENIED"
              | "NOT_DETERMINED"
              | "PROVISIONAL"
              | "UNAUTHORIZED";
            vendor: "apns" | "fcm";
            background?: "DENIED" | "AVAILABLE" | "RESTRICTED";
            device_metadata?: {
              device_id?: string;
              klaviyo_sdk?:
                | "android"
                | "flutter_community"
                | "react_native"
                | "swift";
              sdk_version?: string;
              device_model?: string;
              os_name?: "android" | "ios" | "ipados" | "macos" | "tvos";
              os_version?: string;
              manufacturer?: string;
              app_name?: string;
              app_version?: string;
              app_build?: string;
              app_id?: string;
              environment?: "debug" | "release";
            };
            profile: {
              data: {
                type: "profile";
                id?: string;
                attributes: {
                  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[];
                    };
                  };
                  email?: string;
                };
              };
            };
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/push-tokens`);
    
      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