List Users
One script reply has been approved by the moderators Verified

Get a list of users for a site Required scope | users:read

Created by hugo697 514 days ago
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
	site_id: string,
9
	offset: string | undefined,
10
	limit: string | undefined,
11
	sort:
12
		| 'CreatedOn'
13
		| '-CreatedOn'
14
		| 'Email'
15
		| '-Email'
16
		| 'Status'
17
		| '-Status'
18
		| 'LastLogin'
19
		| '-LastLogin'
20
		| 'UpdatedOn'
21
		| '-UpdatedOn'
22
		| undefined
23
) {
24
	const url = new URL(`https://api.webflow.com/v2/sites/${site_id}/users`)
25

26
	for (const [k, v] of [
27
		['offset', offset],
28
		['limit', limit],
29
		['sort', sort]
30
	]) {
31
		if (v !== undefined && v !== '' && k !== undefined) {
32
			url.searchParams.append(k, v)
33
		}
34
	}
35

36
	const response = await fetch(url, {
37
		method: 'GET',
38
		headers: {
39
			Authorization: 'Bearer ' + auth.token
40
		},
41
		body: undefined
42
	})
43

44
	if (!response.ok) {
45
		const text = await response.text()
46
		throw new Error(`${response.status} ${text}`)
47
	}
48

49
	return await response.json()
50
}
51