0

Update Form

by
Published today

Update parts of a marketing form (name, fieldGroups, configuration, displayOptions, legalConsentOptions).

Script hubspot Verified

The script

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

3
export type DynSelect_form_id = string
4

5
// Dropdown of the account's forms.
6
export async function form_id(auth: RT.Hubspot) {
7
  const response = await fetch(
8
    "https://api.hubapi.com/marketing/v3/forms?limit=100",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { results } = (await response.json()) as {
20
    results: { id: string; name: string }[]
21
  }
22
  return results.map((f) => ({ value: f.id, label: f.name }))
23
}
24

25
/**
26
 * Update Form
27
 * Update parts of a marketing form (name, fieldGroups, configuration, displayOptions, legalConsentOptions). Fields you omit keep their value; `fieldGroups` is replaced as a whole. Requires the forms scope.
28
 */
29
export async function main(
30
  auth: RT.Hubspot,
31
  form_id: DynSelect_form_id,
32
  body: { [key: string]: any }
33
) {
34
  const url = new URL(`https://api.hubapi.com/marketing/v3/forms/${form_id}`)
35

36
  const response = await fetch(url, {
37
    method: "PATCH",
38
    headers: {
39
      Authorization: `Bearer ${auth.token}`,
40
      "Content-Type": "application/json",
41
      Accept: "application/json",
42
    },
43
    body: JSON.stringify(body),
44
  })
45

46
  if (!response.ok) {
47
    throw new Error(`${response.status} ${await response.text()}`)
48
  }
49

50
  return await response.json()
51
}
52