type Convertkit = {
apiSecret: string
}
export async function main(
resource: Convertkit,
tagId: string,
subscriberData: {
email: string
firstName?: string
fields?: { [key: string]: string }
}
) {
const endpoint = `https://api.convertkit.com/v3/tags/${tagId}/subscribe`
const body = {
api_secret: resource.apiSecret,
email: subscriberData.email,
...(subscriberData.firstName && { first_name: subscriberData.firstName }),
...(subscriberData.fields && { fields: subscriberData.fields })
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(body)
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data.subscription
}
Submitted by hugo697 81 days ago