Use Cases







Sunrise forecasting with the Xweather API

5.16.2017//Developer, Tutorials, API & Mapping

Blog post banner

Ben Collin

Front-End Developer

The sunrise marks the beginning of a brand new day. The ever-changing nature of weather means each new day brings with it the need for a new forecast. Whether you're deciding what to wear for your morning run or wondering what the cloud cover will look like as you photograph the sunrise, weather conditions are important. The Xweather API provides the tools needed to generate a sunrise forecast. This article will walk through how to use two endpoints — sunmoon and forecasts — to find the sunrise forecast.

Sunmoon endpoint

The sunmoon endpoint provides a potpourri of information about both the sun and the moon — anything from sunrise/sunset, twilight, moonrise/moonset, and even the phase of the moon. In this example, we will grab the sunrise times for the next 7 days using the filter parameter to return just the sunrise time. This example shows two different time formats that are supported (dateTimeISO 8601, and UNIX timestamp):

The JSON object returned should look like this:

Example of sunmoon endpoint output

That's the first part of the puzzle. We have our sunrise times — but how do we find what the sunrise forecast looks like? To get this data, we can use the forecasts endpoint and insert the results from the sunmoon endpoint.

Step 2: Leveraging the forecasts endpoint

The forecasts endpoint returns information for given locations and times and is available in daily, day/night, and custom intervals. For this example, we will be using a 1-hour filter. The following will return the forecast results for the first sunrise time from the above snippet — labeled as [sun.setISO] in this example.

Now that we've now grabbed both the sunrise times their forecasts, let's combine everything in a short script to further iterate on what we've accomplished.

Sunrise forecast using the Xweather API

We've successfully queried the sunrise forecast for the next week using the sunmoon and forecasts endpoints. You can also do the same for the sunsets and see what the forecast looks like for viewing a full moon — The possibilities are endless. Start building today with a free 30-day trial. Happy coding!

Ben Collin

Front-End Developer



Querying weather conditions along the path of totality for the solar eclipse on April 8th