Update Page Metadata

Update Page-level metadata, including SEO and Open Graph fields. Required scope | `pages: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
	page_id: string,
9
	locale: string | undefined,
10
	body: {
11
		id: string
12
		siteId?: string
13
		title?: string
14
		slug?: string
15
		parentId?: string
16
		collectionId?: string
17
		createdOn?: string
18
		lastUpdated?: string
19
		archived?: false | true
20
		draft?: false | true
21
		canBranch?: false | true
22
		isMembersOnly?: false | true
23
		seo?: { title?: string; description?: string }
24
		openGraph?: {
25
			title?: string
26
			titleCopied?: false | true
27
			description?: string
28
			descriptionCopied?: false | true
29
		}
30
		localeId?: string
31
		publishedPath?: string
32
	}
33
) {
34
	const url = new URL(`https://api.webflow.com/v2/pages/${page_id}`)
35

36
	for (const [k, v] of [['locale', locale]]) {
37
		if (v !== undefined && v !== '' && k !== undefined) {
38
			url.searchParams.append(k, v)
39
		}
40
	}
41

42
	const response = await fetch(url, {
43
		method: 'PUT',
44
		headers: {
45
			'Content-Type': 'application/json',
46
			Authorization: 'Bearer ' + auth.token
47
		},
48
		body: JSON.stringify(body)
49
	})
50

51
	if (!response.ok) {
52
		const text = await response.text()
53
		throw new Error(`${response.status} ${text}`)
54
	}
55

56
	return await response.json()
57
}
58