0

Create Workflow

by
Published today

Create a contact-based workflow with the legacy v3 API.

Script hubspot Verified

The script

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

3
/**
4
 * Create Workflow
5
 * Create a contact-based workflow with the legacy v3 API. `actions` is a list of workflow action objects, e.g. [{"type": "DELAY", "delayMillis": 3600000}]. The workflow is created turned off unless `enabled` is set. Requires the automation scope and a Professional or Enterprise subscription.
6
 */
7
export async function main(
8
  auth: RT.Hubspot,
9
  name: string,
10
  type: "DRIP_DELAY" | "STATIC_ANCHOR" | "PROPERTY_ANCHOR",
11
  actions: { [key: string]: any }[] | undefined,
12
  enabled: boolean | undefined
13
) {
14
  const url = new URL(`https://api.hubapi.com/automation/v3/workflows`)
15

16
  const body: { [key: string]: any } = {}
17
  body.name = name
18
  body.type = type
19
  if (actions?.length) body.actions = actions
20
  if (enabled) body.enabled = enabled
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      Authorization: `Bearer ${auth.token}`,
26
      "Content-Type": "application/json",
27
      Accept: "application/json",
28
    },
29
    body: JSON.stringify(body),
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38