Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query Alpha Vantage API
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.
Copy      Help
using System.Net.Http;
using System.Text.Json;

// Replace the "demo" apikey below with your own key from
string QUERY_URL = "";
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.
else {
  $"Error: {response.StatusCode} - {response.ReasonPhrase}");

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

Best regards and Happy New Year,

Forum Jump:

Users browsing this thread: 1 Guest(s)