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

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