0

Weather Future

by
Published Dec 20, 2024

Future weather API method returns weather in a 3 hourly interval in future for a date between 14 days and 365 days from today in the future

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
		dt?: string
11
		lang?:
12
			| 'ar'
13
			| 'bn'
14
			| 'bg'
15
			| 'zh'
16
			| 'zh_tw'
17
			| 'cs'
18
			| 'da'
19
			| 'nl'
20
			| 'fi'
21
			| 'fr'
22
			| 'de'
23
			| 'el'
24
			| 'hi'
25
			| 'hu'
26
			| 'it'
27
			| 'ja'
28
			| 'jv'
29
			| 'ko'
30
			| 'zh_cmn'
31
			| 'mr'
32
			| 'pl'
33
			| 'pt'
34
			| 'pa'
35
			| 'ro'
36
			| 'ru'
37
			| 'sr'
38
			| 'si'
39
			| 'sk'
40
			| 'es'
41
			| 'sv'
42
			| 'ta'
43
			| 'te'
44
			| 'tr'
45
			| 'uk'
46
			| 'ur'
47
			| 'vi'
48
			| 'zh_wuu'
49
			| 'zh_hsn'
50
			| 'zh_yue'
51
			| 'zu'
52
	}
53
) {
54
	const url = new URL(`http://api.weatherapi.com/v1/future.json`)
55

56
	// Append the API key for authentication
57
	url.searchParams.append('key', auth.apiKey)
58

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

66
	const response = await fetch(url.toString(), {
67
		method: 'GET'
68
	})
69

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

77
	return await response.json()
78
}
79