0
Create Plan
One script reply has been approved by the moderators Verified
Created by hugo697 111 days ago Viewed 33 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 111 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
		currency: string
12
		amount: number
13
		interval: 'day' | 'month' | 'year'
14
		interval_count: number
15
		trial_duration?: number
16
		trial_duration_unit?: 'day' | 'month' | 'year'
17
	}
18
) {
19
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/plans`
20

21
	const response = await fetch(endpoint, {
22
		method: 'POST',
23
		headers: {
24
			'Content-Type': 'application/json',
25
			Accept: 'application/json',
26
			Authorization: `Bearer ${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.plan
38
}
39