Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query Alpha Vantage API
#1
I hope everyone had/is having a great holiday season.
I wanted to use HttpClient  for Alpha Vantage's API instead of WebClient (deprecated). Alpha Vantage's docs use WebClient, which doesn't seem to me to be a great idea since it's deprecated, even though perhaps easier. Here's some code that gets Intraday quotes. You'll need to get a free API key and replace 'demo' in the code with that key.
 
Code:
Copy      Help
using System.Net.Http;
using System.Text.Json;

// Replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
string QUERY_URL = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo";
Uri queryUri = new Uri(QUERY_URL);

using (HttpClient client = new HttpClient()) {
    try {
        // Send the GET request
        HttpResponseMessage response = await client.GetAsync(queryUri);
        
        // Check if the request was successful
        if (response.IsSuccessStatusCode) {
            // Read and parse the response content
            string jsonContent = await response.Content.ReadAsStringAsync();
            
            // Deserialize JSON using System.Text.Json
            dynamic json_data = JsonSerializer.Deserialize<Dictionary<string, dynamic>>(jsonContent);
            
            // Do something with the json_data besides printing, if you like.
            print.it(json_data);
        }
else {
            print.it($"Error: {response.StatusCode} - {response.ReasonPhrase}");
        }
    }

    catch (Exception ex) {
        print.it($"An error occurred: {ex.Message}");
    }
}



Best regards and Happy New Year,
burque505


Forum Jump:


Users browsing this thread: 6 Guest(s)