Hi chinmayy,
Please refer the below sample.
I have used goldapi to get the gold price.
First you need to generate the API key using the following link.
https://www.goldapi.io/
Then using the key, call the API using HttpClient class.
Metal Symbol for Gold is XAU and XAG is for Silver.
Note: This API has 100 Reqs/Month Limit. For more request you need to unlock the unlimited plan.
Namespaces
You need to import the following namespaces.
C#
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Script.Serialization;
VB.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Web.Script.Serialization
Property Class
The Class consists of following properties.
C#
public class Root
{
public int timestamp { get; set; }
public string metal { get; set; }
public string currency { get; set; }
public string exchange { get; set; }
public string symbol { get; set; }
public double prev_close_price { get; set; }
public double open_price { get; set; }
public double low_price { get; set; }
public double high_price { get; set; }
public int open_time { get; set; }
public double price { get; set; }
public double ch { get; set; }
public double chp { get; set; }
public double ask { get; set; }
public double bid { get; set; }
public double price_gram_24k { get; set; }
public double price_gram_22k { get; set; }
public double price_gram_21k { get; set; }
public double price_gram_20k { get; set; }
public double price_gram_18k { get; set; }
public double price_gram_16k { get; set; }
public double price_gram_14k { get; set; }
public double price_gram_10k { get; set; }
}
VB.Net
Public Class Root
Public Property timestamp As Integer
Public Property metal As String
Public Property currency As String
Public Property exchange As String
Public Property symbol As String
Public Property prev_close_price As Double
Public Property open_price As Double
Public Property low_price As Double
Public Property high_price As Double
Public Property open_time As Integer
Public Property price As Double
Public Property ch As Double
Public Property chp As Double
Public Property ask As Double
Public Property bid As Double
Public Property price_gram_24k As Double
Public Property price_gram_22k As Double
Public Property price_gram_21k As Double
Public Property price_gram_20k As Double
Public Property price_gram_18k As Double
Public Property price_gram_16k As Double
Public Property price_gram_14k As Double
Public Property price_gram_10k As Double
End Class
Code
Inside the Page Load event handler, first the ApiKey, Metal symbol and Currency are set.
Then the goldapi is called using the HttpClient class by passing the ApiKey, Metal symbol and Currency.
Then the response is deserialized to property class using Deserialize method of JavaScriptSerializer class.
Finally, the gold price in USD is displayed in JavaScript Alert Message Box.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string apiKey = "goldapikey";
string metalSymbol = "XAU";
string currency = "USD";
// Creating HttpClient object.
using (HttpClient client = new HttpClient())
{
// Adding the API Key to RequestHeader using x-access-token.
client.DefaultRequestHeaders.Add("x-access-token", apiKey);
// Adding the ContentType to RequestHeader.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Generating the URL.
string url = string.Format("https://www.goldapi.io/api/{0}/{1}", metalSymbol, currency);
// Reading the result in HTTP response message object.
HttpResponseMessage response = client.GetAsync(url).Result;
// Checks whether response is Success.
if (response.IsSuccessStatusCode)
{
// Reading the result.
string result = response.Content.ReadAsStringAsync().Result;
// Deserialize to class object.
JavaScriptSerializer js = new JavaScriptSerializer();
Root root = new JavaScriptSerializer().Deserialize<Root>(result);
// Displaying Price in JavaScript Alert Message Box.
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Gold price today: " + root.price + " USD.');", true);
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim apiKey As String = "goldapikey"
Dim metalSymbol As String = "XAG"
Dim currency As String = "XAU"
' Creating HttpClient object.
Using client As HttpClient = New HttpClient()
' Adding the API Key to RequestHeader using x-access-token.
client.DefaultRequestHeaders.Add("x-access-token", apiKey)
' Adding the ContentType to RequestHeader.
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
' Generating the URL.
Dim url As String = String.Format("https://www.goldapi.io/api/{0}/{1}", metalSymbol, currency)
' Reading the result in HTTP response message object.
Dim response As HttpResponseMessage = client.GetAsync(url).Result
' Checks whether response is Success.
If response.IsSuccessStatusCode Then
' Reading the result.
Dim result As String = response.Content.ReadAsStringAsync().Result
' Deserialize to class object.
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim root As Root = New JavaScriptSerializer().Deserialize(Of Root)(result)
' Displaying Price in JavaScript Alert Message Box.
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('Gold price today: " & root.price & " USD.');", True)
End If
End Using
End If
End Sub
Screenshot
