type Convertkit = {
apiSecret: string
}
export async function main(
resource: Convertkit,
filter?: {
page?: number
sortOrder?: 'asc' | 'desc'
sortField?: 'cancelled_at'
from?: string // Format: yyyy-mm-dd
to?: string // Format: yyyy-mm-dd
updatedFrom?: string // Format: yyyy-mm-dd
updatedTo?: string // Format: yyyy-mm-dd
emailAddress?: string
}
) {
// Construct the query parameters.
const queryParams = new URLSearchParams({
api_secret: resource.apiSecret
})
// Add optional filters to the query parameters.
if (filter?.page) queryParams.append('page', filter.page.toString())
if (filter?.sortOrder) queryParams.append('sort_order', filter.sortOrder)
if (filter?.sortField) queryParams.append('sort_field', filter.sortField)
if (filter?.from) queryParams.append('from', filter.from)
if (filter?.to) queryParams.append('to', filter.to)
if (filter?.updatedFrom) queryParams.append('updated_from', filter.updatedFrom)
if (filter?.updatedTo) queryParams.append('updated_to', filter.updatedTo)
if (filter?.emailAddress) queryParams.append('email_address', filter.emailAddress)
const endpoint = `https://api.convertkit.com/v3/subscribers?${queryParams.toString()}`
const response = await fetch(endpoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data
}
Submitted by hugo697 79 days ago