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

Creates a new contact.

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

5
export async function main(
6
	resource: Campayn,
7
	listId: string,
8
	body: {
9
		email: string
10
		first_name?: string
11
		last_name?: string
12
		title?: string
13
		address?: string
14
		city?: string
15
		state?: string
16
		zip?: string
17
		company?: string
18
		country?: string
19
	}
20
) {
21
	const endpoint = `https://campayn.com/api/v1/lists/${listId}/contacts.json`
22

23
	const response = await fetch(endpoint, {
24
		method: 'POST',
25
		headers: {
26
			Authorization: `TRUEREST apikey=${resource.apiKey}`
27
		},
28
		body: JSON.stringify(body)
29
	})
30

31
	if (!response.ok) {
32
		throw new Error(`HTTP error! status: ${response.status}`)
33
	}
34

35
	const data = await response.json()
36

37
	return data
38
}
39