0
Calculate VAT Price
One script reply has been approved by the moderators Verified

Calculate a VAT compliant price given a country and price.

Created by hugo697 56 days ago Viewed 15 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 56 days ago
1
type Abstractapi = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Abstractapi,
7
	amount: number,
8
	countryCode: string,
9
	vatCategory?: string
10
) {
11
	const queryParams = new URLSearchParams({
12
		api_key: resource.apiKey,
13
		amount: amount.toString(),
14
		country_code: countryCode
15
	})
16

17
	if (vatCategory) {
18
		queryParams.append('vat_category', vatCategory)
19
	}
20

21
	const endpoint = `https://vat.abstractapi.com/v1/calculate?${queryParams.toString()}`
22

23
	const response = await fetch(endpoint, {
24
		method: 'GET'
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
34
}
35