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

5
export async function main(
6
	resource: Baremetrics,
7
	sourceId: string,
8
	oid: string,
9
	body: {
10
		canceled_at: number
11
	}
12
) {
13
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/subscriptions/${oid}/cancel`
14

15
	const response = await fetch(endpoint, {
16
		method: 'PUT',
17
		headers: {
18
			'Content-Type': 'application/json',
19
			Accept: 'application/json',
20
			Authorization: `Bearer ${resource.apiKey}`
21
		},
22
		body: JSON.stringify(body)
23
	})
24

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

29
	const data = await response.json()
30

31
	return data.event
32
}
33