Edits history of script submission #12288 for ' Create or Update Profile (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Create or Update Profile
     * Given a set of profile attributes and optionally an ID, create or update a profile.
    
    Returns 201 if a new profile was created, 200 if an existing profile was updated.
    
    Use the `additional-fields` parameter to include subscriptions and predictive analytics data in your response.
    
    Note that setting a field to `null` will clear out the field, whereas not including a field in your request will leave it unchanged.*Rate limits*:Burst: `75/s`Steady: `700/m`
    
     */
    export async function main(
      auth: Klaviyo,
      additional_fields_profile_: string | undefined,
      revision: string,
      body: {
        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[];
            };
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/profile-import`);
      for (const [k, v] of [
        ["additional-fields[profile]", additional_fields_profile_],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      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