weatherstack: an Amazing Weather API (Sponsored)
Publikováno: 18.9.2019
One of my first tasks each day is checking the weather; it’s a necessity for knowing what my children and I should wear, if I’m going to need to water the lawn or need to shovel snow, and if I can take meetings out on my patio. It’s also been one of my worst web […]
The post weatherstack: an Amazing Weather API (Sponsored) appeared first on David Walsh Blog.
One of my first tasks each day is checking the weather; it’s a necessity for knowing what my children and I should wear, if I’m going to need to water the lawn or need to shovel snow, and if I can take meetings out on my patio. It’s also been one of my worst web experiences because Weather.com is slow, difficult to navigate, and plastered with dozens of trackers and advertisements. Luckily I recently discovered an API so I can create my own weather dashboard with the WeatherStack API.
Quick Hits
- Free to start!
- Global weather information with one easy to use API
- Get both realtime, historical, and forecast weather data
- Sample code for implementing the API in Ruby, Node.js, Go, Python, PHP, and even jQuery
- Trusted by 75,000 companies including Microsoft and Warner Brothers
- From the creators of currencylayer, ipstack, mailboxlayer, and more rock solid APIs
Start by signing up for free — you’ll immediately be provided an API token to use, as well as get detailed API instructions for usage.
Basic Usage
To get the realtime weather data for a given location, make a simple API call with the language and method of your choice:
// Getting weather by postal code in Fahrenheit http://api.weatherstack.com/current ?access_key=YOUR_API_KEY &query=53711 &units=f
You will receive a wealth of information in the response:
{ "request":{ "type":"Zipcode", "query":"53711", "language":"en", "unit":"f" }, "location":{ "name":"Madison", "country":"USA", "region":"Wisconsin", "lat":"43.031", "lon":"-89.444", "timezone_id":"America\/Chicago", "localtime":"2019-09-14 14:25", "localtime_epoch":1568471100, "utc_offset":"-5.0" }, "current":{ "observation_time":"07:25 PM", "temperature":72, "weather_code":116, "weather_icons":[ "https:\/\/assets.weatherstack.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" ], "weather_descriptions":[ "Partly cloudy" ], "wind_speed":8, "wind_degree":210, "wind_dir":"SSW", "pressure":1021, "precip":0, "humidity":57, "cloudcover":75, "feelslike":77, "uv_index":6, "visibility":10 } }
You can specify location by city name, latitude/longitude, postal code, IP address, or even Geolocation via the fetch:ip
option.
You can also get weather information for multiple locations with on request by separating separate locations with a ;
; you’ll receive an array of objects representing each location.
Units and Languages
weatherstack also allows developers to specify language and units. Imagine being French and arriving in the United States. Ideally you want the weather information in your local language (French) in and units you understand (Celcius, kilometers, etc.). Add the following parameters to your request:
// Getting weather in French and metric system http://api.weatherstack.com/current ?access_key=YOUR_API_KEY &query=New York &units=m &language=fr
The response payload will provide temperature in Celcius, wind in kilometers, precipitation in millimeters, and snow in centimeters. Letting weatherstack do the localization for you (and your users) is a huge task you don’t need to worry about!
Historical Weather
If you would like historical weather information for a given location and date, hit the /historical
endpoint:
// Weather from last July 5th in hour increments http://api.weatherstack.com/current ?access_key=YOUR_API_KEY &query=Madison &historical_date=2018-07-05 &units=f &hourly=1
The historical endpoint allows a single past date or multiple dates separated by ;
via the historical_date
method.
The historical endpoint also offers two new options: hourly
and interval
. hourly
values are 1
(on) or 0
(off) values which specify if you’d like hourly weather information. The interval
represents the hour increments you’d like values for (1
, 3
, 6
, 12
, and 24
).
Forecast
A very typical use case is wanting to know a weeks worth of weather outlook. For that you can hit the /forecast
API endpoint. The important parameter when using the forecast endpoint is forecast_days
, with the maximum value being 14
.
// Weather for the next 14 days http://api.weatherstack.com/current ?access_key=YOUR_API_KEY &query=Madison &forecast_days=14 &units=f &hourly=1
All of the previously mentioned parameters can be used for the forecast API!
weatherstack is just another amazing API from apilayer. You can always count on them to keep the API simple and reliable, and coupled with their numerous other APIs, you can create an insanely personalized experience for your users. If you want to provide an awesome weather experience for your users, try weatherstack!
The post weatherstack: an Amazing Weather API (Sponsored) appeared first on David Walsh Blog.