0

Returns weather data for a single location and given day in the past

by
Published Oct 17, 2025

## Actual weather data for a single location and day in the past The output contains actual weather data for each day up to 20 years in the past, and long-term statistics of selected weather variables aggregated over 40 years.

Script meteosource Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Returns weather data for a single location and given day in the past
4
 * ## Actual weather data for a single location and day in the past
5

6
The output contains actual weather data for each day up to 20 years in the past, and long-term statistics of selected weather variables aggregated over 40 years.
7
 */
8
export async function main(
9
	auth: RT.Meteosource,
10
	date: string | undefined,
11
	place_id?: string | undefined,
12
	lat?: string | undefined,
13
	lon?: string | undefined,
14
	timezone?: string | undefined,
15
	units?: 'auto' | 'metric' | 'us' | 'uk' | 'ca' | undefined,
16
	language?: 'cs' | 'en' | 'de' | 'es' | 'fr' | 'pl' | 'pt' | undefined,
17
	key?: string | undefined
18
) {
19
	const url = new URL(`https://www.meteosource.com/api/v1/${auth.tier}/time_machine`)
20
	for (const [k, v] of [
21
		['place_id', place_id],
22
		['lat', lat],
23
		['lon', lon],
24
		['date', date],
25
		['timezone', timezone],
26
		['units', units],
27
		['language', language],
28
		['key', key]
29
	]) {
30
		if (v !== undefined && v !== '') {
31
			url.searchParams.append(k, v)
32
		}
33
	}
34
	const response = await fetch(url, {
35
		method: 'GET',
36
		headers: {
37
			'X-API-Key': auth.apiKey
38
		},
39
		body: undefined
40
	})
41
	if (!response.ok) {
42
		const text = await response.text()
43
		throw new Error(`${response.status} ${text}`)
44
	}
45
	return await response.json()
46
}
47