Getting Started With ClimaCell Weather

Introduction

 
Worldwide, more and more people are often affected by sudden climate change, leading to possible unforeseen disasters. Hence, people and organizations rely on accurate and real-time forecasting weather applications to have better decisions involving their safety and business continuity. As we rely more on weather forecasting applications, these applications must give us accurate results and provide us with historical data, real-time data, and forecasting data.
 
Moreover, developers who are into weather application development are most likely to consume weather forecasting APIs, and there are plenty of reliable providers in the market. And if you haven't decided yet which provider to use and are currently experimenting with some platforms, this article might help you select and start with the ClimaCell Weather API platform.
 

Background

 
There are various programming languages you can choose from to get you started with a weather API.
 
In this guide, we will focus on JavaScript.
 
Here are the things needed to get you started,
  • Basics of the JavaScript language.
  • A text editor on your machine such as Notepad, Notepad++, VS Code, etc.
  • Access to ClimaCell Weather API platform. If you haven't registered yet, please register within their portal.

How to Integrate the API

 
Getting Your API Key
 
To use the ClimaCell API, we need to have an API key. This key will be available in your dashboard once you have completed your registration. The screenshot below displays the API key location within your dashboard page.
 
 
Getting Your Current Location
 
Before we can use the API, we need to have our browser coordinates. Our browser provides a native call to the geolocation interface. This object can obtain a device's position when using the navigator.geolocation property.
  1. const nav = navigator.geolocation;   
  2.   
  3. if(nav){    
  4.   nav.getCurrentPosition((position) => {  
  5.     let coordinates = position.coords;  
  6.     console.log(coordinates.latitude, coordinates.longitude);  
  7.   });  
  8. }  
Making an API Call
 
In this article, we are going to be using ClimaCell’s daily API. This API provides a daily forecast that summaries up to 15 days. Lastly, with every successful request when using the daily API, you'll get the data in JSON; it's up to you how you will present your data to your user.
 
To see where the daily API documentation location is on the reference page, please see the screenshot below.
 
 
Now that we have the coordinates needed for the weather forecast, we can then pass these coordinates as a parameter for our API call.
 
Don't forget your API key before making a call.
  1. const api_key = ''your_api_key",  
  2.       url = 'https://api.climacell.co/v3/weather/forecast/daily',  
  3.       nav = navigator.geolocation;   
  4.   
  5. let lat = 0.0,   
  6.     lon = 0.0,  
  7.     unit_system='si',  
  8.     start_time = 'now',  
  9.     fields  = ['humidity''temp''weather_code'],  
  10.     requesturl =  "";  
  11.   
  12. if(nav){    
  13.   nav.getCurrentPosition((position) => {  
  14.     let coordinates = position.coords;  
  15.     lat = coordinates.latitude;  
  16.     lon = coordinates.longitude;  
  17.     requesturl = encodeURI(`${url}?lat=${lat}&lon=${lon}&unit_system=${unit_system}&start_time=${start_time}&fields=${fields.join(',')}&apikey=${api_key}`);  
  18.     getWeatherForeCast();  
  19.   });  
  20. }  
  21.   
  22. async function getWeatherForeCast(){  
  23.   const response = await fetch(requesturl, { "method""GET""headers": {}});  
  24.   const json  = await response.json();  
  25.   console.log(json);  
  26. }  
This is how the code runs:
  • We have declared the application's const, such as the API key, URL, and Navigator object, assigning them to their specific variables.
  • After that, we have another set of variable declarations; these act as placeholders.
  • We have condition statements that check the navigator object, get the device's current location, and set our placeholders' values and build the URL.
  • Lastly, make a request call to the API and check the results.
Output
 
 

Creating a Simple Weather App

 
Knowing how to integrate your chosen programming language to any weather API seems straightforward.
 
To appreciate ClimaCell's weather API, let's try to create a simple weather application that will predict the weather with the first 15 days.
 
Here’s a sample output for this application,
 
 
To give more of a background about the screenshot above, it’s the weather application’s final output.
 
Once the web-application runs, it will automatically detect your coordinates. Once it has the proper coordinates, it will start to call the ClimaCell API to give a 15 days weather forecast as shown above. Moreover, the widget’s upper or header part will show the current weather for the day, and it also includes the temperature and humidity.
 
After that, you’ll see the next forecasted weather in 14 days. That’s why we have seven columns that represent the day and rows to represent the two-week weather forecast.
 
Lastly, within the table’s column, you’ll see an image representing the weather forecast for that day.
 

Here are the steps you should take to create this application.

 
Analyze the JSON results
 
We only need to analyze one item in the JSON result as the data structure is the same, and values will vary from day-to-day. 
  1. humidity: Array(2)  
  2. 0: {observation_time: "2020-12-01T06:00:00Z", min: {…}}  
  3. 1: {observation_time: "2020-11-30T21:00:00Z", max: {…}}  
  4. length: 2  
  5. __proto__: Array(0)  
  6. lat: 14.5708313  
  7. lon: 121.02040299999999  
  8. observation_time: {value: "2020-11-30"}  
  9. temp: Array(2)  
  10. 0: {observation_time: "2020-11-30T22:00:00Z", min: {…}}  
  11. 1: {observation_time: "2020-12-01T04:00:00Z", max: {…}}  
  12. length: 2  
  13. __proto__: Array(0)  
  14. weather_code: {value: "cloudy"}  
As you can see, we have two entries within the humidity property and temperature property.
 
Based on the data, this is because it will give you the lowest and highest possible reading. Lastly, weather_code is the forecasted weather of the day. 
 
Display weather information
  1. function showFirstDetails(weatherInfo) {  
  2.       
  3.     const doc = document,  
  4.         weatherCode = doc.querySelector('#weather_code'),  
  5.         tempHigh = doc.querySelector('#tempHigh'),  
  6.         tempLow = doc.querySelector('#tempLow'),  
  7.         humidityPercentageHigh = doc.querySelector('#humidityPercentageHigh'),  
  8.         humidityPercentageLow = doc.querySelector("#humidityPercentageLow"),  
  9.         currentDateRequest = doc.querySelector('#currentDateRequest'),  
  10.         weather_code_string = doc.querySelector("#weather_code_string");  
  11.   
  12.     let firstInfo = weatherInfo;  
  13.   
  14.     console.log(firstInfo);  
  15.   
  16.     if (firstInfo) {  
  17.         weatherCode.classList.add(firstInfo.weather_code.value);  
  18.         tempHigh.innerHTML = firstInfo.temp[1].max.value + "°" + firstInfo.temp[1].max.units;  
  19.         tempLow.innerHTML = firstInfo.temp[0].min.value + "°" + firstInfo.temp[0].min.units;  
  20.         humidityPercentageHigh.innerHTML = firstInfo.humidity[1].max.value + firstInfo.humidity[1].max.units;  
  21.         humidityPercentageLow.innerHTML = firstInfo.humidity[0].min.value + firstInfo.humidity[0].min.units;  
  22.         currentDateRequest.innerHTML = new Date(firstInfo.observation_time.value).toDateString();  
  23.         weather_code_string.innerHTML = firstInfo.weather_code.value;  
  24.     }  
  25. }  
As you can see in the code snippet, we chose the first index of the collection to show within the header section of the widget, such as temperature, humidity min and max, and the weather forecast for that day.
 
 
The images used for the weather forecast came from ClimaCell’s GitHub repository
 
Now, it’s good to put each weather code into a class equivalent to its image to present the image inside the widget.
 
Here’s how,
  1. .weather {  
  2.     background-repeat: no-repeat;  
  3.     background-position: center;  
  4.     height: 50px;  
  5.     width: 100px;  
  6. }  
  7.   
  8. .freezing_rain_heavy {  
  9.     background-image: url("/weather-code-icons-master/color/freezing_rain_heavy.svg");  
  10. }  
  11.   
  12. .freezing_rain {  
  13.     background-image: url("/weather-code-icons-master/color/freezing_rain.svg");  
  14. }  
  15.   
  16. .freezing_rain_light {  
  17.     background-image: url("/weather-code-icons-master/color/freezing_rain_light.svg");  
  18. }  
  19.   
  20. .freezing_drizzle {  
  21.     background-image: url("/weather-code-icons-master/color/freezing_drizzle.svg");  
  22. }  
  23.   
  24. .ice_pellets_heavy {  
  25.     background-image: url("/weather-code-icons-master/color/ice_pellets_heavy.svg");  
  26. }  
  27.   
  28. .ice_pellets {  
  29.     background-image: url("/weather-code-icons-master/color/ice_pellets.svg");  
  30. }  
  31.   
  32. .ice_pellets_light {  
  33.     background-image: url("/weather-code-icons-master/color/ice_pellets_light.svg");  
  34. }  
  35.   
  36. .snow_heavy {  
  37.     background-image: url("/weather-code-icons-master/color/snow_heavy.svg");  
  38. }  
  39.   
  40. .snow {  
  41.     background-image: url("/weather-code-icons-master/color/snow.svg");  
  42. }  
  43.   
  44. .snow_light {  
  45.     background-image: url("/weather-code-icons-master/color/snow_light.svg");  
  46. }  
  47.   
  48. .flurries {  
  49.     background-image: url("/weather-code-icons-master/color/flurries.svg");  
  50. }  
  51.   
  52. .tstorm {  
  53.     background-image: url("/weather-code-icons-master/color/tstorm.svg");  
  54. }  
  55.   
  56. .rain_heavy {  
  57.     background-image: url("/weather-code-icons-master/color/rain_heavy.svg");  
  58. }  
  59.   
  60. .rain {  
  61.     background-image: url("/weather-code-icons-master/color/rain.svg");  
  62. }  
  63.   
  64. .rain_light {  
  65.     background-image: url("/weather-code-icons-master/color/rain_light.svg");  
  66. }  
  67.   
  68. .drizzle {  
  69.     background-image: url("/weather-code-icons-master/color/drizzle.svg");  
  70. }  
  71.   
  72. .fog_light {  
  73.     background-image: url("/weather-code-icons-master/color/fog_light.svg");  
  74. }  
  75.   
  76. .fog {  
  77.     background-image: url("/weather-code-icons-master/color/fog.svg");  
  78. }  
  79.   
  80. .cloudy {  
  81.     background-image: url("/weather-code-icons-master/color/cloudy.svg");  
  82. }  
  83.   
  84. .mostly_cloudy {  
  85.     background-image: url("/weather-code-icons-master/color/mostly_cloudy.svg");  
  86. }  
  87.   
  88. .partly_cloudy {  
  89.     background-image: url("/weather-code-icons-master/color/partly_cloudy_day.svg");  
  90. }  
  91.   
  92. .mostly_clear {  
  93.     background-image: url("/weather-code-icons-master/color/mostly_clear_day.svg");  
  94. }  
  95.   
  96. .clear {  
  97.     background-image: url("/weather-code-icons-master/color/clear_day.svg");  
  98. }  
The next and last step is showing the remaining forecasted days.
 
We iterated to the data that the API provided to us and put those data in a table to show the related weather codes via images.
 
See the code sample below.
  1. function showDetails(weatherInfos) {  
  2.       
  3.     for (let counter = 1; counter < 8; counter++){  
  4.         document.querySelector("#date" + (counter)).innerHTML =  
  5.             showDays(new Date(weatherInfos[counter].observation_time.value).getDay());  
  6.           
  7.         document.querySelectorAll("#firstGroup tr td")[counter-1].classList.add(weatherInfos[counter].weather_code.value);   
  8.           
  9.     }  
  10.   
  11.     for (let counter2 = 8; counter2 < weatherInfos.length; counter2++) {  
  12.         document.querySelector("#date" + (counter2)).innerHTML =  
  13.             showDays(new Date(weatherInfos[counter2].observation_time.value).getDay());  
  14.   
  15.         document.querySelectorAll("#secondGroup tr td")[counter2 - 8].classList.add(weatherInfos[counter2].weather_code.value);  
  16.   
  17.     }  
  18. }  
 And for us to visualize the HTML template that we have, see the sample code below.
  1. <div class="container">    
  2.     <div class="topContent">    
  3.         <div class="topLeft">    
  4.             <div id="weather_code" class="weather">    
  5.                 <span>    
  6.                     <small class="small" id="weather_code_string"></small>    
  7.                 </span>    
  8.             </div>    
  9.         </div>    
  10.         <div class="topRight">    
  11.             <div>    
  12.                 High: <span id="tempHigh"></span>     
  13.                 Low: <span id="tempLow"></span>    
  14.             </div>    
  15.             <div>Humidity     
  16.                 <span id="humidityPercentageHigh"></span>     
  17.                 <span id="humidityPercentageLow"></span>    
  18.             </div>    
  19.             <div>Date: <span id="currentDateRequest"></span>    
  20.             </div>    
  21.         </div>    
  22.     </div>    
  23.     <div class="bottomContent">    
  24.         <table id="firstGroup">    
  25.                 <thead>    
  26.                     <tr>    
  27.                         <th id="date1"></th>    
  28.                         <th id="date2"></th>    
  29.                         <th id="date3"></th>    
  30.                         <th id="date4"></th>    
  31.                         <th id="date5"></th>    
  32.                         <th id="date6"></th>    
  33.                         <th id="date7"></th>    
  34.                     </tr>    
  35.                 </thead>    
  36.                 <tbody>    
  37.                     <tr>    
  38.                         <td class="weather"></td>    
  39.                         <td class="weather"></td>    
  40.                         <td class="weather"></td>    
  41.                         <td class="weather"></td>    
  42.                         <td class="weather"></td>    
  43.                         <td class="weather"></td>    
  44.                         <td class="weather"></td>    
  45.                     </tr>    
  46.                 </tbody>    
  47.         </table>    
  48.         <table id="secondGroup">    
  49.             <thead>    
  50.                 <tr>    
  51.                     <th id="date8"></th>    
  52.                     <th id="date9"></th>    
  53.                     <th id="date10"></th>    
  54.                     <th id="date11"></th>    
  55.                     <th id="date12"></th>    
  56.                     <th id="date13"></th>    
  57.                     <th id="date14"></th>    
  58.                 </tr>    
  59.             </thead>    
  60.             <tbody>    
  61.                 <tr>    
  62.                     <td class="weather"></td>    
  63.                     <td class="weather"></td>    
  64.                     <td class="weather"></td>    
  65.                     <td class="weather"></td>    
  66.                     <td class="weather"></td>    
  67.                     <td class="weather"></td>    
  68.                     <td class="weather"></td>    
  69.                 </tr>    
  70.             </tbody>    
  71.         </table>    
  72.     </div>    
  73. </div>     
You can access the source code of this application here.
 

Summary

 
We have now seen how to get started with ClimaCell's weather API calls using the daily API. We have shown the following: get your API key, set your current location, how to make an API request, and see the results in JSON format. After we got the basic knowledge of integrating JavaScript with ClimaCell's API, we created a simple weather application to understand the data we have received and present it to a user interface.
 
Lastly, in my opinion, using ClimaCell's weather API is straightforward, and their documentation is easy to understand. 
 
I hope you have enjoyed this article, as much as I have enjoyed writing it. Stay tuned for more. Until next time, happy programming!


Similar Articles