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

List all subscriptions to a tag

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
	tagId: 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/tags/${tagId}/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