Edits history of script submission #12464 for ' Query Segment Series (klaviyo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Klaviyo = {
      apiKey: string;
    };
    /**
     * Query Segment Series
     * Returns the requested segment analytics series data.*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d`
    
     */
    export async function main(
      auth: Klaviyo,
      revision: string,
      body: {
        data: {
          type: "segment-series-report";
          attributes: {
            statistics:
              | "members_added"
              | "members_removed"
              | "net_members_changed"
              | "total_members"[];
            timeframe:
              | {
                  key:
                    | "last_12_months"
                    | "last_30_days"
                    | "last_365_days"
                    | "last_3_months"
                    | "last_7_days"
                    | "last_90_days"
                    | "last_month"
                    | "last_week"
                    | "last_year"
                    | "this_month"
                    | "this_week"
                    | "this_year"
                    | "today"
                    | "yesterday";
                }
              | { start: string; end: string };
            interval: "daily" | "hourly" | "monthly" | "weekly";
            filter?: string;
          };
        };
      },
    ) {
      const url = new URL(`https://a.klaviyo.com/api/segment-series-reports`);
    
      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