0
Create Subscription
One script reply has been approved by the moderators Verified
Created by hugo697 118 days ago Viewed 201 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
	body: {
9
		oid: string
10
		plan_oid: string
11
		customer_oid: string
12
		started_at: number
13
		canceled_at?: number
14
		quantity?: number
15
		discount?: number
16
		addons?: {
17
			oid: string
18
			amount: number
19
			quantity: number
20
		}[]
21
	}
22
) {
23
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/subscriptions`
24

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

35
	if (!response.ok) {
36
		const error = await response.json()
37
		console.error(error)
38
		throw new Error(`HTTP error! status: ${response.status}`)
39
	}
40

41
	const data = await response.json()
42

43
	return data.event
44
}
45