0
Add/Update Custom Code
One script reply has been approved by the moderators Verified

Add a registered script to a Site. In order to use the Custom Code APIs for Sites and Pages, Custom Code Scripts must first be registered to a Site via the registered_scripts endpoints, and then applied to a Site or Page using the appropriate custom_code endpoints. Access to this endpoint requires a bearer token from a Data Client App. Required scope | custom_code:write

Created by hugo697 245 days ago Viewed 13502 times
0
Submitted by hugo697 Bun
Verified 245 days ago
1
//native
2
type Webflow = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Webflow,
8
	site_id: string,
9
	body: {
10
		scripts?: {
11
			id: string
12
			location: 'header' | 'footer'
13
			version: string
14
			attributes?: {}
15
		}[]
16
		lastUpdated?: string
17
		createdOn?: string
18
	}
19
) {
20
	const url = new URL(`https://api.webflow.com/v2/sites/${site_id}/custom_code`)
21

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

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

36
	return await response.json()
37
}
38