0
Add/Update Subscriber
One script reply has been approved by the moderators Verified
Created by hugo697 55 days ago Viewed 14 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 55 days ago
1
type Acumbamail = {
2
	authToken: string
3
}
4

5
export async function main(
6
	resource: Acumbamail,
7
	body: {
8
		list_id: number
9
		merge_fields: { [key: string]: string }
10
		double_optin?: boolean
11
		update_subscriber?: boolean
12
	}
13
) {
14
	const endpoint = 'https://acumbamail.com/api/1/addSubscriber/'
15

16
	const formData = new FormData()
17

18
	Object.entries(body.merge_fields).forEach(([key, value]) => {
19
		formData.append(`merge_fields[${key}]`, value)
20
	})
21

22
	formData.append('auth_token', resource.authToken)
23
	formData.append('list_id', body.list_id.toString())
24
	formData.append('double_optin', body.double_optin ? '1' : '0')
25
	formData.append('update_subscriber', body.update_subscriber ? '1' : '0')
26
	formData.append('complete_json', '1')
27

28
	const response = await fetch(endpoint, {
29
		method: 'POST',
30
		headers: {
31
			'Cache-Control': 'no-cache'
32
		},
33
		body: formData
34
	})
35

36
	if (!response.ok) {
37
		throw new Error(`HTTP error! status: ${response.status}`)
38
	}
39

40
	const data = await response.json()
41

42
	return data
43
}
44