Get Collection Item

Get details of a selected Collection Item. Required scope | `CMS:read`

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
	item_id: string,
10
	cmsLocaleId: string | undefined
11
) {
12
	const url = new URL(`https://api.webflow.com/v2/collections/${collection_id}/items/${item_id}`)
13

14
	for (const [k, v] of [['cmsLocaleId', cmsLocaleId]]) {
15
		if (v !== undefined && v !== '' && k !== undefined) {
16
			url.searchParams.append(k, v)
17
		}
18
	}
19

20
	const response = await fetch(url, {
21
		method: 'GET',
22
		headers: {
23
			Authorization: 'Bearer ' + auth.token
24
		},
25
		body: undefined
26
	})
27

28
	if (!response.ok) {
29
		const text = await response.text()
30
		throw new Error(`${response.status} ${text}`)
31
	}
32

33
	return await response.json()
34
}
35