Fetching Free Stock Market Data using AlphaVantage & .NET MVC Core

Mark
3 min readSep 16, 2019

--

First, there was Yahoo Finance. Then there was Google Finance. Finally, there was AlphaVantage. The API which gives you free stock market data.

Here is a free, bare-bones AlphaVantage MVC .NET Core application template & three tips for developing your own AlphaVantage MVC application.

Fetching monthly prices from AlphaVantage — MVC Template

Users enter a ticker (e.g. MSFT) & can fetch monthly prices from AlphaVantage API. The results are displayed in a paginated table. It is purposefully a very simple MVC template (using Razor & Bootstrap) to help you get going in your AlphaVantage MVC project.

I would like to share three tips for your AlphaVantage development using .NET:

Tip #1: Use the CSV dataformat when retrieving data from AlphaVantage API

AlphaVantage allows you to retrieve data in JSON or CSV format. The JSON data returned is more difficult to work with & requires you to jump through a number of hoops to convert this to a C# object.

AlphaVantage’s JSON data is not easy to work with

Instead of working with AlphaVantage’s JSON data format, simply add &datatype=csv to the AlphaVantage URL. The response will be in CSV format, which is straight forward to work with. See more in tip #2.

Tip #2: Use these open-source libraries to do HTTP Get Requests

There are hour-long, boring instructional videos showing how you can do a simple HTTP GET request to an external API (e.g. AlphaVantage API) in C#. Most of them use .NET ‘s built-in HTTP web clients, such as WebRequest or WebClient. Here is a piece of advice that will save you a lot of frustration & time — DO NOT USE them.

Why? WebRequest & WebClient are prime examples of how to make the life of a developer complicated. They are super frustrating to work with; when all you want to do is a simple Get request in C#. HttpClient goes some way in addressing these issues, but it still falls short. On top of that Microsoft’s documentation is bad … really bad; unless you want to sift through pages and pages of technical blurb.

What to do instead? There are three excellent open-source, free NuGet as alternatives. Thank goodness! These are all well supported, documented and easy to work with. They make you a much happier developer.

In the free MVC template, I use ServiceStack.Text to call the AlphaVantage API end-point in two lines of code.

AlphaVantage MVC Template Controller — extract of code to retrieve prices from AlphaVantage

To truly appreciate the simplicity of this, have a look at the top results in google for “How to do a GET request in C# to an external API” & the resulting code.

Tip #3: Don’t call the AlphaVantage from the client directly

Ok this may or may not be an obvious one to you — but “why not simply call the AlphaVantage API from the client using javascript ?”

Note that you have to include your personal API key in any calls to the AlphaVantage API — to retrieve any prices other than Microsoft’s. Fetching data from AlphaVantage’s API is best done server-side (or via an API Gateway). If you call AlphaVantage API from the client, this will leave your AlphaVantage key exposed to people with ill intent.

In the free template, you will note that the call to the AlphaVantage API is done from the Controller (ie server-side) & not client-side.

Happy development.

Programming with Mark (excel-automation-guide.com)

--

--