Understanding the Basics of the 7Timer Public API

The 7Timer Public API is an online weather forecast service that provides hourly weather forecasts for different locations around the world. This API uses weather models from different sources to ensure the accuracy of the forecasts. This article will discuss the various features of the 7Timer API and will provide you with some JavaScript code examples to help you get started.

API Endpoints

The 7Timer Public API has several endpoints that allow you to access various weather information. Some of the most commonly used endpoints include:

  • astro: provides information on the location of the sun and moon at a particular location and time.
  • civil: provides the visibility, cloud cover, and precipitation forecast for the current location.
  • naut: provides information on maritime weather conditions, such as wind speed and wave height.
  • temp: provides hourly temperature, pressure and humidity data for a specific area.

Using the 7Timer API with JavaScript

To access the 7Timer API's data with JavaScript, you will need to make HTTP requests to the API's endpoints. Let's take a look at some example code for requesting data from the temp endpoint:

const url = "http://www.7timer.info/bin/api.pl?lon=113.17&lat=23.09&product=temp";
fetch(url)
    .then(response => response.json())
    .then(data => {
        console.log(data.dataseries[0].temp2m);
    })
    .catch(error => console.log(error));

In this code, we are fetching the temperature data for a location with coordinates 113.17 (longitude) and 23.09 (latitude). We first set the URL for the API request, which includes the endpoint we want to access (product=temp). We then use the fetch function to send the request and receive the response in JSON format. Finally, we log the first hourly temperature data value to the console.

Another example is retrieving astrological weather data:

const url = "http://www.7timer.info/bin/api.pl?lon=113.17&lat=23.09&product=astro";
fetch(url)
    .then(response => response.json())
    .then(data => {
        console.log(`Sunrise time: ${data.astronomy[0].sunrise}`);
        console.log(`Moon phase: ${data.astronomy[0].moonphase}`);
    })
    .catch(error => console.log(error));

In this code, we fetch the astro endpoint for the same location as before. We then log the sunrise time and current moon phase to the console.

Conclusion

In this article, we've explored the basics of the 7Timer Public API, and we've provided some JavaScript code examples for accessing weather data from the API. While the examples we've provided are simple, the 7Timer API offers a wealth of information for detailed weather forecasts, and with these basic examples, you'll be on your way to building more complex applications. So why not try using this API on your own project?

Related APIs

Public APIs — A directory of free and public apis

Built by @mddanishyusuf