Edits history of script submission #11835 for ' Update a contact (brevo)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Brevo = {
      apiKey: string;
    };
    /**
     * Update a contact
     * There are 2 ways to update a contact  Option 1- https://api.
     */
    export async function main(
      auth: Brevo,
      identifier: string,
      identifierType:
        | "email_id"
        | "contact_id"
        | "ext_id"
        | "phone_id"
        | "whatsapp_id"
        | "landline_number_id"
        | undefined,
      body: {
        attributes?: {};
        ext_id?: string;
        emailBlacklisted?: false | true;
        smsBlacklisted?: false | true;
        listIds?: number[];
        unlinkListIds?: number[];
        smtpBlacklistSender?: string[];
      },
    ) {
      const url = new URL(`https://api.brevo.com/v3/contacts/${identifier}`);
      for (const [k, v] of [["identifierType", identifierType]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "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.text();
    }
    

    Submitted by hugo697 428 days ago