Edits history of script submission #12271 for ' Create Back In Stock Subscription (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Create Back In Stock Subscription
     * Subscribe a profile to receive back in stock notifications.
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "back-in-stock-subscription";
          attributes: {
            channels: "EMAIL" | "PUSH" | "SMS"[];
            profile?: {
              data: {
                type: "profile";
                id?: string;
                attributes: {
                  email?: string;
                  phone_number?: string;
                  external_id?: string;
                  anonymous_id?: string;
                };
              };
            };
          };
          relationships?: {
            variant?: { data?: { type: "catalog-variant"; id: string } };
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/back-in-stock-subscriptions`);
    
      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