This short article describes how to use AlphaVantage’s free stock market data API in your C# code.
What is AlphaVantage? AlphaVantage is a free stock market & currency market data feed. To get started, simply sign up with your email address to get your free API key.
How do I retrieve data from AlphaVantage using C#? You have two options:
First option. There are existing C# wrappers written for AlphaVantage. The library I found easiest to work with is LutsenkoKirill/AlphaVantage.Net hosted on GitHub.
To install this library in your project use NuGet command: Install-Package AlphaVantage.Net.Core
Usage example:
public static async Task AlphaVantageCoreDemo()
{
var coreClient = new AlphaVantageCoreClient(); string apiKey = "1"; // enter your API key here // retrieve stocks batch quoutes of Apple and Facebook
var query = new Dictionary<string, string>()
{
{"symbols", "FB,AAPL"}
};
JObject deserialisedResponse = await coreClient.RequestApiAsync(apiKey, ApiFunction.BATCH_STOCK_QUOTES, query);
// parse JObject here
}
Second option. Write your own code. This is much simpler than it sounds. I will show you below, how you can retrieve stock market data from AlphaVantage using one line of c# code.
Here is the magic one line C# code, which will retrieve stock market data from AlphaVantage:
To get this running in your own projects, you need to
1. Install ServiceStack.Text ( a free, open-source, high-performance text utility) using NuGet
PM> Install-Package ServiceStack.Text
2. Use below code in your project
Running this code in gistlyn (C# in the browser) produces this result:
Click here to run this code in gistlyn.
I hope this will be useful for your project. Feel free to leave any comments & I will be happy to answer any more questions.
If you enjoyed this article,
- you might also enjoy my AlphaVantage C# implementation on .NET MVC article
- my other posts on Medium here
- I run excel-automation-guide.com
Thanks
Mark