0

Marine Weather

by
Published Dec 20, 2024

Marine weather API method returns upto next 7 day (depending upon your price plan level) marine and sailing weather forecast and tide data (depending upon your price plan level)

Script weatherapi Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Weatherapi = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Weatherapi,
8
	params: {
9
		q: string
10
		days: number
11
		dt?: string
12
		unixdt?: number
13
		hour?: number
14
		lang?:
15
			| 'ar'
16
			| 'bn'
17
			| 'bg'
18
			| 'zh'
19
			| 'zh_tw'
20
			| 'cs'
21
			| 'da'
22
			| 'nl'
23
			| 'fi'
24
			| 'fr'
25
			| 'de'
26
			| 'el'
27
			| 'hi'
28
			| 'hu'
29
			| 'it'
30
			| 'ja'
31
			| 'jv'
32
			| 'ko'
33
			| 'zh_cmn'
34
			| 'mr'
35
			| 'pl'
36
			| 'pt'
37
			| 'pa'
38
			| 'ro'
39
			| 'ru'
40
			| 'sr'
41
			| 'si'
42
			| 'sk'
43
			| 'es'
44
			| 'sv'
45
			| 'ta'
46
			| 'te'
47
			| 'tr'
48
			| 'uk'
49
			| 'ur'
50
			| 'vi'
51
			| 'zh_wuu'
52
			| 'zh_hsn'
53
			| 'zh_yue'
54
			| 'zu'
55
	}
56
) {
57
	const url = new URL(`http://api.weatherapi.com/v1/marine.json`)
58

59
	// Append the API key for authentication
60
	url.searchParams.append('key', auth.apiKey)
61

62
	// Append the query parameters if provided
63
	for (const [key, value] of Object.entries(params)) {
64
		if (value !== undefined && value !== '') {
65
			url.searchParams.append(key, value.toString())
66
		}
67
	}
68

69
	const response = await fetch(url.toString(), {
70
		method: 'GET'
71
	})
72

73
	if (!response.ok) {
74
		const errorText = await response.text()
75
		throw new Error(
76
			`WeatherAPI request failed: ${response.status} ${response.statusText} - ${errorText}`
77
		)
78
	}
79

80
	return await response.json()
81
}
82