Web API’s open up a world of possibilities for us geeks.  When I first learned how to access API’s using C#, I went through the ringer trying to find out the best and easiest way to extract data.   For this post, we’ll be using OpenWeatherMap.org‘s free api to extract some weather information.

First thing’s first, go to https://www.openweathermap.org/ and create a free account.  Navigate to the API Keys section from the home page of your account and create a new key.

Create a new blank console app in Visual Studio, and add the code below.  You’ll want to add a break point at the var response line (below in red).

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace OpenWeatherAPI
{
class Program
{

static void Main(string[] args)
{
//Create a list and object to house APIData. The list is here so we can loop through data
//List<WeatherData> myweather = new List<WeatherData>(); //********************Uncomment this after creating the class that will hold the API data*********************
//WeatherData APIData = new WeatherData();//********************Uncomment this after creating the class that will hold the API data*********************

//Query parameters
string BaseURI = “http://api.openweathermap.org/data/2.5/weather”;
string qparam = “zip=80525,US&units=imperial”; //<———————-Add your own city zip here****************************
string APIKey = <your api key>;

//Building the request URL
string url = BaseURI + “?” + qparam + “&APPID=” + APIKey;

WebClient apiclient = new WebClient();

var response = apiclient.DownloadString(url);

//Serializes the json response into the weather object

//**********************Uncomment the remaining code after creating the weather class to hold the API data***************************
//DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(WeatherData));

////Adds data to the objects we defined above
//using (var data = new MemoryStream(Encoding.Unicode.GetBytes(response)))

//{
//APIData = (WeatherData)serializer.ReadObject(data);
//}

//myweather.Add(APIData);

////Prints the response data
//foreach (var a in myweather)

//{
//Console.WriteLine(a.name + ” forecast for “+ DateTime.Today.ToString(“MMMM dd, yyyy”));
//Console.WriteLine(“Temperature: ” + a.main.temp);
//Console.WriteLine(“High today: ” + a.main.temp_max);
//Console.WriteLine(“Low today: ” + a.main.temp_min);
//Console.WriteLine(“Wind: ” + a.wind.speed);
//}

Console.ReadLine();

}
}

}

Add the needed references and run the code to the break point then step over it.  Without exiting the debugger, copy the json data that is inside of the response object.  Paste that data into http://json2csharp.com/.  This will give you the objects that are in the json response.  Add a class to the console app and paste in the object definitions, then add a data contract before the root object and data members to each of the root object’s members.  Below I renamed the root object to WeatherData.

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace OpenWeatherAPI
{

[DataContract]
public class WeatherData
{
[DataMember]
public Coord coord { get; set; }
[DataMember]
public Sys sys { get; set; }
[DataMember]
public List<Weather> weather { get; set; }
[DataMember]
public string @base { get; set; }
[DataMember]
public Main main { get; set; }
[DataMember]
public int visibility { get; set; }
[DataMember]
public Wind wind { get; set; }
[DataMember]
public Clouds clouds { get; set; }
[DataMember]
public int dt { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public int cod { get; set; }
}

public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}

public class Sys
{
public int type { get; set; }
public int id { get; set; }
public double message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}

public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}

public class Main
{
public double temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
}

public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}

public class Clouds
{
public int all { get; set; }
}

}

Now you can remove the break point and run the code!  You should have a response like this:

<Your city name> forecast for today’s date
Temperature: 64.45
High today: 72
Low today: 59.9
Wind: 5.95