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

List all broadcasts

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
	filter?: {
8
		page?: number
9
		sortOrder?: 'asc' | 'desc'
10
	}
11
) {
12
	// Construct the query parameters.
13
	const queryParams = new URLSearchParams({
14
		api_secret: resource.apiSecret
15
	})
16

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

21
	const endpoint = `https://api.convertkit.com/v3/broadcasts?${queryParams.toString()}`
22

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

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

34
	const data = await response.json()
35

36
	return data.broadcasts
37
}
38