type Convertkit = {
apiSecret: string
}
const fetchSubscriberByEmail = async (resource: Convertkit, email: string) => {
const queryParams = new URLSearchParams({
api_secret: resource.apiSecret,
email_address: email
})
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(
`Failed to fetch subscriber that matches the provided email. Response code: ${response.status}`
)
}
const data = await response.json()
if (data.subscribers && data.subscribers.length > 0) {
return data.subscribers[0]
}
throw new Error(`No subscriber found with the provided email`)
}
const fetchSubscribedTags = async (resource: Convertkit, subscriberId: string) => {
const queryParams = new URLSearchParams({
api_secret: resource.apiSecret
})
const endpoint = `https://api.convertkit.com/v3/subscribers/${subscriberId}/tags?${queryParams.toString()}`
const response = await fetch(endpoint, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
if (!response.ok) {
throw new Error(`Failed to fetch tags. Response code: ${response.status}`)
}
const data = await response.json()
if (data.tags) {
return data.tags
}
throw new Error(`No tags found for the subscriber`)
}
export async function main(resource: Convertkit, email: string) {
const subscriber = await fetchSubscriberByEmail(resource, email)
const tags = await fetchSubscribedTags(resource, subscriber.id)
return tags
}
Submitted by hugo697 25 days ago