0
List Subscribers
One script reply has been approved by the moderators Verified

Returns a list of all subscribers

Created by hugo697 76 days ago Viewed 29 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 76 days ago
1
type Convertkit = {
2
	apiSecret: string
3
}
4

5
export async function main(
6
	resource: Convertkit,
7
	filter?: {
8
		page?: number
9
		sortOrder?: 'asc' | 'desc'
10
		sortField?: 'cancelled_at'
11
		from?: string // Format: yyyy-mm-dd
12
		to?: string // Format: yyyy-mm-dd
13
		updatedFrom?: string // Format: yyyy-mm-dd
14
		updatedTo?: string // Format: yyyy-mm-dd
15
		emailAddress?: string
16
	}
17
) {
18
	// Construct the query parameters.
19
	const queryParams = new URLSearchParams({
20
		api_secret: resource.apiSecret
21
	})
22

23
	// Add optional filters to the query parameters.
24
	if (filter?.page) queryParams.append('page', filter.page.toString())
25
	if (filter?.sortOrder) queryParams.append('sort_order', filter.sortOrder)
26
	if (filter?.sortField) queryParams.append('sort_field', filter.sortField)
27
	if (filter?.from) queryParams.append('from', filter.from)
28
	if (filter?.to) queryParams.append('to', filter.to)
29
	if (filter?.updatedFrom) queryParams.append('updated_from', filter.updatedFrom)
30
	if (filter?.updatedTo) queryParams.append('updated_to', filter.updatedTo)
31
	if (filter?.emailAddress) queryParams.append('email_address', filter.emailAddress)
32

33
	const endpoint = `https://api.convertkit.com/v3/subscribers?${queryParams.toString()}`
34

35
	const response = await fetch(endpoint, {
36
		method: 'GET',
37
		headers: {
38
			'Content-Type': 'application/json'
39
		}
40
	})
41

42
	if (!response.ok) {
43
		throw new Error(`HTTP error! status: ${response.status}`)
44
	}
45

46
	const data = await response.json()
47

48
	return data
49
}
50