0
Unsubscribe Contact
One script reply has been approved by the moderators Verified

Unsubscribes a contact from a list.

Created by hugo697 118 days ago Viewed 203 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 118 days ago
1
type Campayn = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Campayn,
7
	listId: string,
8
	body: {
9
		id?: string
10
		email?: string
11
	}
12
) {
13
	const endpoint = `https://campayn.com/api/v1/lists/${listId}/unsubscribe.json`
14

15
	if (!body.id && !body.email) {
16
		throw new Error('Please provide either an id or an email inside the body!')
17
	}
18

19
	const response = await fetch(endpoint, {
20
		method: 'POST',
21
		headers: {
22
			Authorization: `TRUEREST apikey=${resource.apiKey}`
23
		},
24
		body: JSON.stringify(body)
25
	})
26

27
	if (!response.ok) {
28
		throw new Error(`HTTP error! status: ${response.status}`)
29
	}
30

31
	const data = await response.json()
32

33
	return data
34
}
35