0

Create form field

by
Published Oct 17, 2025

Create a new field for the specified form

Script formstack Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Formstack = {
3
  token: string;
4
};
5
/**
6
 * /form/:id/field
7
 * Create a new field for the specified form
8
 */
9
export async function main(
10
  auth: Formstack,
11
  id: string,
12
  body: {
13
    field_type: string;
14
    label: string;
15
    hide_label?: false | true;
16
    description?: string;
17
    description_callout?: false | true;
18
    default_value?: string;
19
    options?: string[];
20
    options_values?: string[];
21
    required?: false | true;
22
    readonly?: false | true;
23
    hidden?: false | true;
24
    uniq?: false | true;
25
    colspan?: number;
26
    sort?: number;
27
    attributes?: {};
28
    logic?: {};
29
    calculation?: string;
30
  },
31
) {
32
  const url = new URL(`https://www.formstack.com/api/v2/form/${id}/field.json`);
33

34
  const response = await fetch(url, {
35
    method: "POST",
36
    headers: {
37
      "Content-Type": "application/json",
38
      Authorization: "Bearer " + auth.token,
39
    },
40
    body: JSON.stringify(body),
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48