Edits history of script submission #23428 for ' Enroll Contact in Sequence (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Enroll Contact in Sequence
     * Enroll a contact in a sales sequence, sent from a connected inbox. `user_id` is the HubSpot user who owns the enrollment, `sender_email` one of their connected inboxes. Requires automation.sequences.enrollments.write (Sales or Service Hub Professional or Enterprise).
     */
    export async function main(
      auth: RT.Hubspot,
      user_id: string,
      sequence_id: string,
      contact_id: string,
      sender_email: string,
      sender_alias_address: string | undefined
    ) {
      const url = new URL(
        `https://api.hubapi.com/automation/v4/sequences/enrollments`
      )
      url.searchParams.append("userId", user_id)
    
      const body: { [key: string]: any } = {}
      body.sequenceId = sequence_id
      body.contactId = contact_id
      body.senderEmail = sender_email
      if (sender_alias_address != null && sender_alias_address !== "")
        body.senderAliasAddress = sender_alias_address
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago