C# Example API Call (Get)
An API, or Application Programming Interface, is a library of related programme code available for programmers to use. People and organisations can provide a service, which is available to use via an API. In the case of the example below, weather data is made available via an HTTP Get request, which returns the data in JSON format. This API returns a five-day forecast, for a specific area, at three hourly intervals. Full details of this API can be found on the Rapid API website here, as well as the API's own website here.
A sample of the weather data returned can be seen below, which covers two time periods on a particular day. It should be noted that the data is returned in one long JSON string, rather than nicely formatted, as shown here.
{
"timezone": "Europe/London",
"city_name": "Bath",
"lon": -2.362,
"state_code": "ENG",
"lat": 51.3849,
"data": [
{
"wind_cdir": "SW",
"wind_gust_spd": 8.52,
"pod": "d",
"pres": 995,
"clouds_hi": 0,
"clouds_low": 100,
"clouds_mid": 85,
"vis": 24,
"wind_spd": 4.22,
"wind_cdir_full": "southwest",
"slp": 1006,
"datetime": "2025-06-07:09",
"ts": 1749286800,
"dewpt": 8.7,
"uv": 2,
"snow": 0,
"ghi": 688,
"dhi": 107,
"precip": 0.00048828125,
"dni": 846,
"temp": 12.6,
"app_temp": 12.6,
"pop": 15,
"timestamp_utc": "2025-06-07T09:00:00",
"weather": {
"icon": "c04d",
"description": "Overcast clouds",
"code": 804
},
"solar_rad": 196.20615,
"clouds": 99,
"timestamp_local": "2025-06-07T10:00:00",
"snow_depth": 0,
"ozone": 394,
"rh": 77,
"wind_dir": 236
},
{
"wind_cdir": "WSW",
"wind_gust_spd": 10.74,
"pod": "d",
"pres": 995,
"clouds_hi": 100,
"clouds_low": 70,
"clouds_mid": 100,
"vis": 7.4,
"wind_spd": 4.88,
"wind_cdir_full": "west-southwest",
"slp": 1006,
"datetime": "2025-06-07:12",
"ts": 1749297600,
"dewpt": 11,
"uv": 3,
"snow": 0,
"ghi": 905,
"dhi": 119,
"precip": 0.4321289,
"dni": 905,
"temp": 14,
"app_temp": 14,
"pop": 30,
"timestamp_utc": "2025-06-07T12:00:00",
"weather": {
"icon": "r01d",
"description": "Light rain",
"code": 500
},
"solar_rad": 179.14203,
"clouds": 100,
"timestamp_local": "2025-06-07T13:00:00",
"snow_depth": 0,
"ozone": 391,
"rh": 82,
"wind_dir": 240
}
],
"country_code": "GB"
}
In order for this to work, the package 'Newtonsoft.Json' needs to be added to the project. This can be done in a couple of different ways, depending on what Integrated Development Environment (IDE) is being used. Visual Studio Professional incorporates NuGet Package Manager, which allows for packages to be searched for and installed. For IDEs that don't have a built-in package manager, the commmand line can be used. The following command can be used to install the above-mentioned package. Before running this command, it is necessary to navigate to the folder where the project resides.
dotnet add package Newtonsoft.Json
Once added, a ‘using‘ statement for the ‘Newtonsoft.Json.Linq’ namespace needs to be included.
using System; using System.Net.Http.Headers; using System.Xml.Linq; using Newtonsoft.Json.Linq; namespace DemoAPICallGet { class Program { static async System.Threading.Tasks.Task Main(string[] args) { try { // Client for HTTP request and response. var client = new HttpClient(); // API request. var request = new HttpRequestMessage { Method = HttpMethod.Get, RequestUri = new Uri( "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/" + "3hourly?lat=51.38488&lon=-2.36197"), Headers = { { "x-rapidapi-key", "api-key-goes-here" }, { "x-rapidapi-host", "weatherbit-v1-mashape.p.rapidapi.com" }, }, }; // API response. using (var response = await client.SendAsync(request)) { // Throw an error if there is not a successful response. response.EnsureSuccessStatusCode(); // Assign the body of the response to a variable and parse the JSON. var json = await response.Content.ReadAsStringAsync(); dynamic weather = JObject.Parse(json); // Display the city name. Console.WriteLine(weather.city_name); // Display the information for each day and time period. foreach (var dayTime in weather.data) { // Display as: 'Date Time - Temperature - Weather Description'. Console.WriteLine((DateTime)dayTime.timestamp_local + " - " + (int)Math.Round((double)dayTime.temp) + "\u00b0C - " + dayTime.weather.description); } } } catch (Exception e) { // Display generic API call unsuccessful message. Console.WriteLine("API call unsuccessful."); // Display actual error. Console.Write(e.Message); } } } }
Once the data has been retrieved, it is assigned to a variable called 'json', which is then parsed, so that the individual elements can be accessed. Firstly, the city name is displayed, followed by the weather data for each day and time period. The 'timestamp_local', which contains the date and time period, is cast, or converted, to a 'datetime' to improve its display. Similarly, the 'temp', which contains the temperature in Celsius, is cast to a 'double', before rounding to the nearest whole number and then casting to an 'int', or integer, to display.
Sample output to the console can be seen below.
Bath 07/06/2025 10:00:00 - 13°C - Overcast clouds 07/06/2025 13:00:00 - 14°C - Light rain 07/06/2025 16:00:00 - 14°C - Light rain 07/06/2025 19:00:00 - 14°C - Light shower rain 07/06/2025 22:00:00 - 12°C - Clear Sky 08/06/2025 01:00:00 - 10°C - Clear Sky 08/06/2025 04:00:00 - 9°C - Clear Sky 08/06/2025 07:00:00 - 10°C - Clear Sky 08/06/2025 10:00:00 - 13°C - Clear Sky 08/06/2025 13:00:00 - 16°C - Clear Sky 08/06/2025 16:00:00 - 16°C - Few clouds 08/06/2025 19:00:00 - 15°C - Clear Sky 08/06/2025 22:00:00 - 12°C - Clear Sky 09/06/2025 01:00:00 - 11°C - Clear Sky 09/06/2025 04:00:00 - 10°C - Clear Sky 09/06/2025 07:00:00 - 11°C - Scattered clouds 09/06/2025 10:00:00 - 15°C - Clear Sky 09/06/2025 13:00:00 - 17°C - Overcast clouds 09/06/2025 16:00:00 - 17°C - Scattered clouds 09/06/2025 19:00:00 - 16°C - Overcast clouds 09/06/2025 22:00:00 - 13°C - Overcast clouds 10/06/2025 01:00:00 - 13°C - Overcast clouds 10/06/2025 04:00:00 - 13°C - Clear Sky 10/06/2025 07:00:00 - 13°C - Overcast clouds 10/06/2025 10:00:00 - 17°C - Overcast clouds 10/06/2025 13:00:00 - 18°C - Overcast clouds 10/06/2025 16:00:00 - 19°C - Scattered clouds 10/06/2025 19:00:00 - 17°C - Scattered clouds 10/06/2025 22:00:00 - 15°C - Clear Sky 11/06/2025 01:00:00 - 13°C - Clear Sky 11/06/2025 04:00:00 - 11°C - Scattered clouds 11/06/2025 07:00:00 - 12°C - Overcast clouds 11/06/2025 10:00:00 - 17°C - Few clouds 11/06/2025 13:00:00 - 22°C - Clear Sky 11/06/2025 16:00:00 - 24°C - Clear Sky 11/06/2025 19:00:00 - 22°C - Scattered clouds 11/06/2025 22:00:00 - 19°C - Few clouds 12/06/2025 01:00:00 - 19°C - Few clouds 12/06/2025 04:00:00 - 16°C - Overcast clouds 12/06/2025 07:00:00 - 16°C - Overcast clouds