Create Collection Field

Create a custom field in a collection. Slugs must be all lowercase letters without spaces. If you pass a string with uppercase letters and/or spaces to the "Slug" property, Webflow will convert the slug to lowercase and replace spaces with "-." Only some field types can be created through the API. This endpoint does not currently support bulk creation. Required scope | `cms:write`

Script webflow Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 563 days ago
1
//native
2
type Webflow = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Webflow,
8
	collection_id: string,
9
	body: {
10
		isRequired?: false | true
11
		type:
12
			| 'PlainText'
13
			| 'RichText'
14
			| 'Image'
15
			| 'MultiImage'
16
			| 'Video'
17
			| 'Link'
18
			| 'Email'
19
			| 'Phone'
20
			| 'Number'
21
			| 'DateTime'
22
			| 'Switch'
23
			| 'Color'
24
			| 'File'
25
		displayName: string
26
		helpText?: string
27
	}
28
) {
29
	const url = new URL(`https://api.webflow.com/v2/collections/${collection_id}/fields`)
30

31
	const response = await fetch(url, {
32
		method: 'POST',
33
		headers: {
34
			'Content-Type': 'application/json',
35
			Authorization: 'Bearer ' + auth.token
36
		},
37
		body: JSON.stringify(body)
38
	})
39

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

45
	return await response.json()
46
}
47