The WeatherAPI Locationforecast/2.0 service gives you a short -and medium weather forecasts for the whole world, up to nine days into the future. It is by far the most popular product we provide, and should be of interest to everyone.
We assume you have already gone through the Getting Started tutorial, which explains the most important concepts of the API.
Locationforecast/2.0 comes in three flavours (endpoints):
compact
, but with more data (soon it will also include probabilities). It is quite large and should not be necessary for most users.From now on we assume you will be using the compact
endpoint, which you can find an example of here:
https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60&lon=11
As you can see you have to specify the location using geocoordinates. If you need to lookup these from place names we suggest using GeoNames or OpenStreetMap Nominatim. Do not use more than 4 decimals to avoid blocking (this is necessary for effective caching).
You should now read more about the structure of the JSON format we use, and the data model which explains what the various variables mean. There is also an official reference documentation available, where you can try out the various functions in the OpenAPI interface.
Since there are literally hundreds of different programming languages (791 on RosettaCode alone) it is impossible for us to write example code for even the most popular ones using the Weather API today. Instead we are going to give you just the basic information you will need to write a client in your chosen language:
You will be needing the following tools in your chosen programming language to use the Weather API :
A list of suggested libraries for various languages are listed in Appendix A.
Once you have the libraries installed and a basic website or mobile app running, you can start implementing the client interface code.
sitename
with the name of your website and some contact info (e.g. a GitHub URL, a non-personal email address, a working website or a mobile app store appname). Do not fake this or you are likely to be permanently blacklisted!User-Agent
request header using the sitename
variable as value. If this is missing or generic, you will get a 403 Forbidden
response.Expires
and Last-Modified
timestamp headers (in RFC 1123 format), parse and store them in variables for later use.expires
and last_modified
timestamp variables above.expires
value stored earlier; if not you must continue using the stored data. Do not send a new request every time the GPS position changes by a metre!expires
timestamp is in the past, you can repeat the request. However you should do this using the If-Modified-Since
HTTP request header with the stored last_modified
variable above as value. If the data has not been updated since your last request you will get a 304 Not Modified
status code back with no body; you should then continue using the stored data until you get a 200 OK
response.If you still want to see some example code in your chosen language we suggest taking a look at the following third-party libraries for Go, JavaScript, C#, Perl/Mojolicious or PHP.
When contacting the API directly from the browser using Javascript, you will run into CORS issues. For example it is not possible to add your own User-Agent
header; instead the browser will send an Origin
header identifying your website by its URL. If this is missing, is localhost
or a local IP address behind a firewall, you risk being throttled and/or blacklisted. Do not use this in production environments!
Please follow our mailing list or RSS feed to get important informasjon about upcoming changes to the API.
Here is a list of known libraries which may be useful. More will be added in the future.
Language | HTTP useragent | JSON parser |
---|---|---|
JavaScript | Fetch | Fetch |
Python | requests (example) | json |
Go | http-client | json |
Perl | Mojo::Useragent | Mojo::JSON::XS |