type Convertkit = {
apiSecret: string
}
export async function main(
resource: Convertkit,
subscriberId: string,
payload: {
firstName?: string
email?: string
fields?: { [key: string]: string }
}
) {
if (!Object.keys(payload).length) {
throw new Error(`Provide at least one key-value pair in the payload!`)
}
const endpoint = `https://api.convertkit.com/v3/subscribers/${subscriberId}`
const body = {
api_secret: resource.apiSecret,
...(payload.firstName && { first_name: payload.firstName }),
...(payload.email && { email_address: payload.email }),
...(payload.fields && { fields: payload.fields })
}
const response = await fetch(endpoint, {
method: 'PUT',
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.subscriber
}
Submitted by hugo697 81 days ago