Update Collection Item
One script reply has been approved by the moderators Verified

Update a selected Item in a Collection. Required scope | CMS:write

Created by hugo697 514 days ago Picked 3 times
Submitted by hugo697 Bun
Verified 514 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
	item_id: string,
10
	body: {
11
		id: string
12
		cmsLocaleId?: string
13
		lastPublished?: string
14
		lastUpdated?: string
15
		createdOn?: string
16
		isArchived?: false | true
17
		isDraft?: false | true
18
		fieldData?: { name?: string; slug?: string }
19
	}
20
) {
21
	const url = new URL(`https://api.webflow.com/v2/collections/${collection_id}/items/${item_id}`)
22

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

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

37
	return await response.json()
38
}
39