Edits history of script submission #23403 for ' Add Contact to List (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_list_id = string
    
    // Dropdown of the account's lists.
    export async function list_id(auth: RT.Hubspot) {
      const response = await fetch("https://api.hubapi.com/crm/v3/lists/search", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ count: 500 }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { lists } = (await response.json()) as {
        lists: { listId: string; name: string; processingType: string }[]
      }
      return lists.map((l) => ({
        value: l.listId,
        label: `${l.name} (${l.processingType})`,
      }))
    }
    
    /**
     * Add Contact to List
     * Add one or more contacts (record IDs) to a list. Only MANUAL and SNAPSHOT lists accept manual additions; DYNAMIC lists are filled by their filters. Requires crm.lists.write.
     */
    export async function main(
      auth: RT.Hubspot,
      list_id: DynSelect_list_id,
      contact_ids: string[]
    ) {
      const url = new URL(
        `https://api.hubapi.com/crm/v3/lists/${list_id}/memberships/add`
      )
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(contact_ids),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago