0
Update Subscription
One script reply has been approved by the moderators Verified
Created by hugo697 118 days ago Viewed 169 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 118 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
		plan_oid: string
11
		occurred_at?: number
12
		quantity?: number
13
		discount?: number
14
		addons?: {
15
			oid: string
16
			amount: number
17
			quantity: number
18
		}[]
19
	}
20
) {
21
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/subscriptions/${oid}`
22

23
	const response = await fetch(endpoint, {
24
		method: 'PUT',
25
		headers: {
26
			'Content-Type': 'application/json',
27
			Accept: 'application/json',
28
			Authorization: `Bearer ${resource.apiKey}`
29
		},
30
		body: JSON.stringify(body)
31
	})
32

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

37
	const data = await response.json()
38

39
	return data.event
40
}
41