0

Enroll Contact in Workflow

by
Published today

Enroll a contact, by email address, into a workflow.

Script hubspot Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Enroll Contact in Workflow
5
 * Enroll a contact, by email address, into a workflow. The workflow must be turned on. Requires the automation scope and a Professional or Enterprise subscription.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  workflow_id: string,
10
  contact_email: string
11
) {
12
  const response = await fetch(
13
    `https://api.hubapi.com/automation/v2/workflows/${workflow_id}/enrollments/contacts/${encodeURIComponent(contact_email)}`,
14
    {
15
      method: "POST",
16
      headers: {
17
        Authorization: `Bearer ${auth.token}`,
18
        Accept: "application/json",
19
      },
20
    }
21
  )
22

23
  if (!response.ok) {
24
    throw new Error(`${response.status} ${await response.text()}`)
25
  }
26
  if (response.status === 204) return { success: true }
27

28
  return await response.json()
29
}
30