Weather
The Weather API in Scripting provides access to real-time and forecast weather data, including current conditions, hourly forecasts, and daily forecasts. This API allows users to fetch weather details such as temperature, wind speed, humidity, and precipitation for a specified location.
Types
UnitType
Represents a unit of measurement with its value, symbol, and formatted string.
1type UnitType = {
2 value: number
3 symbol: string
4 formatted: string
5}
UnitTemperature
, UnitSpeed
, UnitLength
, UnitAngle
, UnitPressure
These types extend UnitType
to represent temperature, speed, length, angle, and pressure.
WeatherCondition
A string enum describing various weather conditions, including:
-
clear
-
rain
-
snow
-
thunderstorms
-
cloudy
-
windy
-
...
Functions
Weather.requestCurrent(location: LocationInfo): Promise<CurrentWeather>
Retrieves the current weather conditions for a given location.
Parameters
location: LocationInfo
– The location for which weather data is requested.
Returns
A Promise
resolving to a CurrentWeather
object.
Example
1const location = { latitude: 37.7749, longitude: -122.4194 }
2const weather = await Weather.requestCurrent(location)
3console.log(`Current temperature: ${weather.temperature.formatted}`)
Weather.requestDailyForecast(location: LocationInfo, options?: { startDate: Date, endDate: Date }): Promise<WeatherDailyForecast>
Retrieves the daily weather forecast for the specified location. You can optionally provide a start and end date to specify the forecast range.
Parameters
location: LocationInfo
– The location to query.
options.startDate
– The start date for the forecast.
options.endDate
– The end date for the forecast.
Returns
A Promise
resolving to a WeatherDailyForecast
object.
Example
1const forecast = await Weather.requestDailyForecast(location, {
2 startDate: new Date(),
3 endDate: new Date(Date.now() + 5 * 24 * 60 * 60 * 1000)
4})
5console.log(`Tomorrow's weather: ${forecast.forecast[1].condition}`)
Weather.requestHourlyForecast(location: LocationInfo, options?: { startDate: Date, endDate: Date }): Promise<WeatherHourlyForecast>
Retrieves the hourly weather forecast for the specified location. You can optionally provide a start and end date to specify the forecast range.
Parameters
location: LocationInfo
– The location to query.
options.startDate
– The start date for the forecast.
options.endDate
– The end date for the forecast.
Returns
A Promise
resolving to a WeatherHourlyForecast
object.
Example
1const hourlyForecast = await Weather.requestHourlyForecast(location, {
2 startDate: new Date(),
3 endDate: new Date(Date.now() + 3 * 60 * 60 * 1000)
4})
5console.log(`Next hour's temperature: ${hourlyForecast.forecast[0].temperature.formatted}`)
CurrentWeather
Represents the current weather conditions.
1type CurrentWeather = {
2 temperature: UnitTemperature
3 apparentTemperature: UnitTemperature
4 humidity: number
5 wind: WeatherWind
6 condition: WeatherCondition
7 date: number
8 symbolName: string
9}
WeatherDailyForecast
Represents the daily forecast.
1type WeatherDailyForecast = {
2 metadata: WeatherMetadata
3 forecast: DayWeather[]
4}
WeatherHourlyForecast
Represents the hourly forecast.
1type WeatherHourlyForecast = {
2 metadata: WeatherMetadata
3 forecast: HourWeather[]
4}
DayWeather
Represents daily weather details.
1type DayWeather = {
2 highTemperature: UnitTemperature
3 lowTemperature: UnitTemperature
4 wind: WeatherWind
5 condition: WeatherCondition
6 date: number
7 symbolName: string
8}
HourWeather
Represents hourly weather details.
1type HourWeather = {
2 temperature: UnitTemperature
3 humidity: number
4 wind: WeatherWind
5 condition: WeatherCondition
6 date: number
7 symbolName: string
8}
Example Usage
Fetch and Display Current Weather
1async function displayCurrentWeather() {
2 const location = { latitude: 37.7749, longitude: -122.4194 }
3 const weather = await Weather.requestCurrent(location)
4 console.log(`The temperature is ${weather.temperature.formatted} with ${weather.condition}`)
5}
6
7displayCurrentWeather()
Fetch and Display Daily Forecast
1async function displayDailyForecast() {
2 const location = { latitude: 37.7749, longitude: -122.4194 }
3 const forecast = await Weather.requestDailyForecast(location, {
4 startDate: new Date(),
5 endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
6 })
7 forecast.forecast.forEach(day => {
8 console.log(`Date: ${new Date(day.date).toDateString()}, Condition: ${day.condition}`)
9 })
10}
11
12displayDailyForecast()
Fetch and Display Hourly Forecast
1async function displayHourlyForecast() {
2 const location = { latitude: 37.7749, longitude: -122.4194 }
3 const hourlyForecast = await Weather.requestHourlyForecast(location, {
4 startDate: new Date(),
5 endDate: new Date(Date.now() + 5 * 60 * 60 * 1000)
6 })
7 hourlyForecast.forecast.forEach(hour => {
8 console.log(`Time: ${new Date(hour.date).toLocaleTimeString()}, Temp: ${hour.temperature.formatted}`)
9 })
10}
11
12displayHourlyForecast()