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

List all subscriptions to a sequence

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

5
export async function main(
6
	resource: Convertkit,
7
	sequenceId: 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
	if (filter?.subscriberState) queryParams.append('subscriber_state', filter.subscriberState)
23

24
	const endpoint = `https://api.convertkit.com/v3/sequences/${sequenceId}/subscriptions?${queryParams.toString()}`
25

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

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

37
	const data = await response.json()
38

39
	return data
40
}
41