---
# System prepended metadata

title: .NET Core Wrapper for a Weather API

---

# .NET Core Wrapper for a Weather API

![](https://lh4.googleusercontent.com/d6LD6qvDjT5vFIIdKpAnGpqPDVPXisBBVVKmMPSh3UHRLTyJE9-ipKxlEkD2Tp-wBXpP-bpEYnHUX4VyCECGfakdbJAYC6BMCELro1eQyds_c4THNBk8Apxc5yB-FBv86fFZwPA)[Photo](https://unsplash.com/photos/TG1Iudp02Xg) by [NOAA](https://unsplash.com/@noaa) on Unsplash

Let me guess: You're a C# developer who landed on this page after scouring the internet for Weather API wrappers and finding solutions that work mainly with JavaScript. Well, welcome aboard! In this article, we'll focus on the .NET Core stack using the C# programming language.

Prerequisites 


We won't be dealing with advanced algorithms or techniques of the C# language here. However, you must be familiar with the basics of object-oriented programming ([OOP](https://en.wikipedia.org/wiki/Object-oriented_programming)) concepts, basic unit testing, and creating a project inside Visual Studio.

We'll be making a class library project and using it in our different projects within a Visual Studio solution. The example here is as-is. It has not been tested in any production environment and has been created for educational purposes only. But, of course, it's up to you if you want to extend or use this example.

For this tutorial, we will be using the free [Tomorrow.io Weather API](https://www.tomorrow.io/weather-api/). This API provider has a lot of great features to offer, like:

* A weather-intelligence platform
* Weather timelines and trendlines
* Core weather data layers
* 5-day weather forecast
* 6 hours of historical weather data
* One automatically monitored location
* One weather-based alert

The API has many more capabilities as well. However, as we're using the free version, we'll stick to the above features.

## 1\.  Project Setup 


We first created a Visual Studio blank solution in our project setup, named it "Weather.Solution", and added more projects as needed.

You'll see that inside our solution, we have created a class library project (Weather.ApiWrapper), unit test project (WeatherApiWrapper.Test), and console application (ConsoleWeatherApp1).  Each of these projects is created to serve its purpose.

The following is our solution-project structure.


![[](https://lh3.googleusercontent.com/VRl9bauV1Is0OmgomMqDv2Q0qlyxepbqg1WEo19-iVFMtA_qHdHKRy631qFboxbVdIyiRzh4QSuk7kWT1lGkpiplQLkAOQJibzf1TdFnk3B7E-IGH5r3MFXJL2wHgLDNpwlRfiI](https://lh4.googleusercontent.com/NpClSJvj_tVRIKYCojTMKdeO8pPwtB2vV8D2_Lz6uQDAUMZDYTrMrEJ27F_pHH-OEy9ocmcxD4rE83UKe6_IflAgW6Xt9KVuyKJHrBMAH4uERT5gj04c0Bili8cR6ADI39HHkt4)




## 2\.  Weather API Configuration Settings



appSettings.json

Inside this file, you'll see the configuration representing the arguments that need to be passed to the TomorrowIO Weather API. These values will be created on your profile, and everything is available in the documentation. So, you need to register on the TomorrowIO site and get an API key to use.

It is also important to mention that these keys/properties are required when requesting new weather information: the [API key](https://rapidapi.com/blog/api-glossary/api-key/), location, and fields. Without these, the API will return an exception.

Let's see the sample code below.

![](https://lh3.googleusercontent.com/VRl9bauV1Is0OmgomMqDv2Q0qlyxepbqg1WEo19-iVFMtA_qHdHKRy631qFboxbVdIyiRzh4QSuk7kWT1lGkpiplQLkAOQJibzf1TdFnk3B7E-IGH5r3MFXJL2wHgLDNpwlRfiI)

[Source](https://carbon.now.sh/ZzqD0Yd5oRjvYRRDhEJr)

For the location coordinates, you can use the Locations page of Tomorrow.io. You can then create a new location there and copy the coordinates.

![](https://lh6.googleusercontent.com/lf79bnaX6GR3KrIDRkL7RJRi4OLuaR0OqkTSFArt6c4yz-nHzPf9n4SASJXVK4frOWPcKxONNEMj7MdHg7n57pXTU2mKP10Al1AFwfgYAlHuZl98n1drWh73b3T1dU4OGifGP58)



## 3\.  WeatherSettings.cs


I know what you're thinking: How are we going to read the [JSON configuration file](https://www.c-sharpcorner.com/article/read-configuration-files-json-in-net-core-application-day-four/)? This third file---WeatherSettings.cs---is for that.

It uses the ConfigurationBuilder class to build and get the settings inside our JSON file. There is a public method that will help you get the specific section and property of the WeatherSettings inside the JSON file.

Let's see the sample code below.

![](https://lh3.googleusercontent.com/UraKj9oOEb-KV94eKHR0ocXiar5OUjU0httwTd_sSbIVbALRxmQrV96JjXUIgDw_l9tNF-8KKKQjL3zgdqy5bIGMr1tASyZ3oHCiaoNuiwcO0yRyE3bGG2UYNL2rvbo4io_jsZ8)

[Source](https://carbon.now.sh/rC3Ul06MzIn9CBOdYAk2)



## 4\.  TestWeatherSettings.cs
    

To be sure that we can retrieve our configuration, we need to test it. This file serves that purpose. See the code sample below. The test explorer shows that all tests pass.

![](https://lh3.googleusercontent.com/VTZ-mCjZZeoU0xo3Fjof01-zyJNAfU5c6WhxiIeFRsEkONXDlKSxRYqL1W89K_HQKoNKZle9Hyj3HXjX-DeuoToYqY1t6iMGBLi_cXdeZRm8LP1swUTve54x0gKl3QZcd2SzBUQ)

[Source](https://carbon.now.sh/kNFid3ffqCX1wOCqNvXa)

![](https://lh5.googleusercontent.com/EN-83swKcNB7cz1CPNTS5ziZ4zNVuzoqP_EG6TFhQkHvJd4gaKHTEs1P1XmFviZHObAA0IDlAGY9hWSw1yjkkOCyHpndNtBLvu4HxEeBtA7_4PFqTIZP9_FUbTX8MKqFXJu7Rko)



## 5\.  Weather API Parameters 


We need to get the proper values of our parameters to send them to the TomorrowIO Weather API. Therefore, once we retrieve the values on our configuration file, we need to represent, validate (if required), and convert those values into one URL string.

In other words, we need to get all the possible values inside our configuration file and build a URL string that will represent the whole configuration so we can pass it to the TomorrowIO API.

![](https://lh3.googleusercontent.com/seGyDXWIICFAFfZKam4B-mM_eZl-cytE32Er5xlcLT8pflDf64iJTa4hI3fQw8r-CJ1_vlxkX8s6UwdO1uPofHomZScbPqy_rBXalOPVW8wp1z2EX7rGVihNO1vYfJ0jOrrFrvE)

[Source](https://carbon.now.sh/305mqq8qRUgE3pebYg0v)

The interface, IWeatherParameters, will get the values coming from our configuration file.

Before moving on, let's see the sample code of the WeatherParameter class.

![](https://lh3.googleusercontent.com/jTz_AS1pmOB26kVLvXr4lWoxvw5Wyo8ejFwkL4a7cDB0tqtUcUxLWFVgCSd6SAY3HCzk9P5-ZafrKTqbZA5iRJt00txZ2HKCnVjfY1Bu21ak0A5oy4Mjf-2cBB_dx2JfTloRIPQ)

[Source](https://carbon.now.sh/U1kQ5gQd1bNkiS8A4U99)

The GetUri() method returns the string URL that we need. It works by looping through all the class instance properties and checking whether they are required. If it is required, it will be included in the URL.

That's why you'll see the attribute [WeatherParameterRequired(true)] and set the property to 'if required'.

Let's see the code sample of our WeatherParameterRequiredAttribute.

![](https://lh5.googleusercontent.com/EkvmOBxf1aqSXBXTuPcPPidXl6JVoNX2niv0ttt5BF0its5THJgbwrhnWHiRr6acxyP36JMBT1KM07ftY-oEuPDiiHGCQp5KP6zOFF4XWZ9UIhcBUCh53RiE1qfps0j0YfKQXDk)

[Source](https://carbon.now.sh/eRL2CFF7YcJ6o3i70ANI)



## 6\.  Consuming/Calling the TomorrowIO API 
  

To call the TomorrowIO Weather API, we need the Http Client class to make an HTTP request.  And, of course, we need the URL generated from the WeatherParameter. That's why we have two private members inside the WeatherApi class.

Let's see the sample code below.

![](https://lh3.googleusercontent.com/VQj5od0wSWGtStJDrws4U2Yv2J2X1llE6aPG99biyqUvRl9IUDLw1PwSlV5d9FseI_2Krt6CqYm_kSOoDLnpUBG5cTYxuSHR8JHqUvqqHxQ6HMRXHDjgbEh_Lmj1iBBR-mNtOjo)

[Source](https://carbon.now.sh/wzHKE5Y9lkkm5LeiCMBM)

Once we combine these two inside our class, we can request new weather information.

![](https://lh4.googleusercontent.com/gJUzHZ384QgzDsvSwbWR6jgX24ZVm4KM8dfAjhPkqIepMF9QdeRppzeimyTzlUhZkPBNSxvMdkpCWR5AQTsdaik7_6efC1HkgqIRtIYnImWkdSxdle4UmDm7EBT36MS6uJr7-Yw)

[Source](https://carbon.now.sh/umQDbzYjIQj07eNkQ7e2)

As you can see, the GetResult method is where all the magic happens. You'll be able to see the data returned by the TomorrowIO Weather API.

![](https://lh4.googleusercontent.com/GhBwcLXL9M8WFo9UQHvB5LY2D_Wh77K2_BSR96-gpiE0Q2YeECUHZX8NLM9Dk0kDNTGxq4IcofnqOTg1RL6AuTv7eMGSlKSLXpx4AEdupafxgBGlS1HKXaKIHVMltex9u4UbEQc)

As you see, we're now getting the correct JSON data from our provider. We can use this in any project to include the new weather information. But, of course, it's up to you how you represent your data in your application's UI (user interface).

Lastly, if you want to run the test of this wrapper, you can run the TestWeatherApiWrapper. Thereafter, you can debug it thoroughly.

Let's see the sample code of the TestWeatherApiWrapper.

![](https://lh6.googleusercontent.com/BbWwd7M7ZYj2nyI3a5CEIvqF1zR9XY4H9BGaTufCSFqvj78oGyuiJKyi-mdUYk6flBXgaVBFQzKn3WA2u4zdPwZq5cRhJqwbBA2n9YZxYi67ygpD558wPAqhpJ4VFkeflxQOog0)

[Source](https://carbon.now.sh/2KQpSTJkGuJGcFWHFpnv)



## 7\.  Using the Custom Built Library Wrapper Weather API 


Easy, isn't it? We need to have a client-side that will request new weather information for the moment of truth. For this purpose, our project has a console application named "ConsoleWeatherApp1" that will show us how to use our customized weather API efficiently.

Finally, we have installed the package "Microsoft.AspNetCore.Mvc.NewtonsoftJson" in this project to see good JSON formatting when printing the JSON data in a console window.

![](https://lh4.googleusercontent.com/x7dmnXZAloYsfF3GeeNYR8y9yHBZH9evaS8EyV0o1in7BTO5rigLr5gxhaQzKmFrdEVCQ_kl1TPn_XC0JWyNmxFNExoezc5_FfXJSwm4UrcNXhCMbzjYR5UsoMwVk73n9LiS3Ts)

[Source](https://carbon.now.sh/3ac27g5UWmqShXF7ONV1)

You will now see the following output.

![](https://lh6.googleusercontent.com/AnXlDLBzQXegDPcMky_8PiTh1FKriuXOjJeAPVxbGgwiGTUFRtfNCC7LJLxGzgo0WDs9e_ovj0uIoPf2rMtKNkVzhBqcRXsEDRqPlp8ACMH75tlLWtMidhB06-ZMCu1l7BwzDdE)

If you're interested, you can create a new web application inside your solution. Add a reference to this class library, and try it yourself. Please let me know what happens in the comments below.

## Summary


As you can see, it's not hard to create a custom weather API using the [.Net Core](https://dotnet.microsoft.com/download/dotnet/3.1) framework and C# language.

We started by seeing the structure of the solution and the different projects inside it. Then we established the classes that will get the configuration and built the values to generate a URL string. Finally, we made an HTTP request by passing the URL string and extracting weather data from the TomorrowIO Weather API represented as a JSON string.

I hope you have enjoyed this post as I have enjoyed writing it. Until next time, happy programming! Stay tuned for more.