C# - How to call the Yahoo Finance API in .Net

Yahoo has one of the best free financial APIs out there and in this post you will see an example of how to call it. Yahoo finance has many different APIs to choose from but I will use v11/finance/quoteSummary as an example in this post for simplicity. You can sign up and get an API key here, you will not have to provide your credit card, just use the email that you get or go back using browser history when it wants you to choose a plan - that is what I did at least.

Below is an example of how you can call the Yahoo Finance API:

var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("https://yfapi.net/");
httpClient.DefaultRequestHeaders.Add("X-API-KEY", 
    "<your API key>");
httpClient.DefaultRequestHeaders.Add("accept", 
    "application/json");

var response = await httpClient.GetAsync(
    "v11/finance/quoteSummary/AAPL?lang=en&region=US&modules=defaultKeyStatistics%2CassetProfile");
var responseBody = await response.Content.ReadAsStringAsync();

Note: Remember to replace <your API key> with yur API key!

First we create a new HTTPClient with a baseURL and some headers. The first header is your API key which you can get by signing up at yahoo finance, without this key all your requests will be rejected. Remember, without buying a plan for the Yahoo Finance API you only have 100 calls per day. The other header determines the format which we wish to have returned, for this example we use application/json. After setting up the HTTPClient we make a HTTP request to one of the Yahoo finance API endpoints and read the response. It will look like the following:

Calling-Yahoo-Finance-API

Parsing the JSON

There are several ways to pass JSON. A simple one is to use JObject from newtonsoft. If we call the yahoo API using AAPL we get the following (shortened) JSON

{
  "quoteSummary": {
    "result": [
      {
        "assetProfile": {
          "address1": "One Apple Park Way",
          "city": "Cupertino",
          "state": "CA",
          "zip": "95014",
          "country": "United States",
          "phone": "408 996 1010",
          "website": "https://www.apple.com",
          "industry": "Consumer Electronics",
          "sector": "Technology",
...

Using JObject we can get the address using SelectToken:

var data = (JObject)JsonConvert.DeserializeObject(responseBody);
var address = data.SelectToken("quoteSummary.result[0].assetProfile.address1").Value<string>();

The address variable in the above will be "One Apple Park Way". This is a simple way to query data without much work.

Another way is to pass the JSON to an object that represents the JSON. This is done using DeserializeObject as above but giving it a class as a generic type:

var data = JsonConvert.DeserializeObject<QuoteSummaryResponse>(responseBody);
var address = data.QuoteSummary.Result[0].AssetProfile.address1;

Your QuoteSummaryResponse class will have to represent the JSON object, like the following:

public class QuoteSummaryResponse
{
    public QuoteSummary QuoteSummary { get; set; }
}

public class QuoteSummary
{
    public QuoteSummaryResult[] Result { get; set; }
}

public class QuoteSummaryResult
{
    public AssetProfile AssetProfile { get;set; }
}

public class AssetProfile
{
    public string address1 { get; set; }
    //And all the other attributes..
}

If you choose to parse it to an object, you can use an online generator like Json2CSharp. You will likely find that not all attributes are filled out and you will have to "patch" the classes over time. Also if the value of a json property is "null" the parser cannot guess the value.

That is it!

I hope you found this helpful, let me know in the comments down below if you did or need more help!