0
Create Customer
One script reply has been approved by the moderators Verified
Created by hugo697 127 days ago Viewed 994 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 127 days ago
1
type Baremetrics = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Baremetrics,
7
	sourceId: string,
8
	body: {
9
		oid: string
10
		name?: string
11
		email?: string
12
		notes?: string
13
	}
14
) {
15
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/customers`
16

17
	const response = await fetch(endpoint, {
18
		method: 'POST',
19
		headers: {
20
			'Content-Type': 'application/json',
21
			Accept: 'application/json',
22
			Authorization: `Bearer ${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.customer
34
}
35