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

List form subscriptions

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

5
export async function main(
6
	resource: Convertkit,
7
	formId: string,
8
	filter?: {
9
		page?: number
10
		sortOrder?: 'asc' | 'desc'
11
		subscriberState?: 'active' | 'cancelled'
12
	}
13
) {
14
	// Construct the query parameters.
15
	const queryParams = new URLSearchParams({
16
		api_secret: resource.apiSecret
17
	})
18

19
	// Add optional filters to the query parameters.
20
	if (filter?.page) queryParams.append('page', filter.page.toString())
21
	if (filter?.sortOrder) queryParams.append('sort_order', filter.sortOrder)
22

23
	const endpoint = `https://api.convertkit.com/v3/forms/${formId}/subscriptions?${queryParams.toString()}`
24

25
	const response = await fetch(endpoint, {
26
		method: 'GET',
27
		headers: {
28
			'Content-Type': 'application/json'
29
		}
30
	})
31

32
	if (!response.ok) {
33
		throw new Error(`HTTP error! status: ${response.status}`)
34
	}
35

36
	const data = await response.json()
37

38
	return data
39
}
40