0

Get Form

by
Published today

Retrieve a marketing form definition, including its field groups.

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
 * Get Form
27
 * Retrieve a marketing form definition, including its field groups. Requires the forms scope.
28
 */
29
export async function main(auth: RT.Hubspot, form_id: DynSelect_form_id) {
30
  const url = new URL(`https://api.hubapi.com/marketing/v3/forms/${form_id}`)
31

32
  const response = await fetch(url, {
33
    headers: {
34
      Authorization: `Bearer ${auth.token}`,
35
      Accept: "application/json",
36
    },
37
  })
38

39
  if (!response.ok) {
40
    throw new Error(`${response.status} ${await response.text()}`)
41
  }
42

43
  return await response.json()
44
}
45