VTW Currency Converter

Here is a sample code for an asynchronous request in vbscript with Viz Template Wizard.

In this example we use the API of https://twelvedata.com

You need to register on their website in order to get a free api key.

To process the JSON response we use the parsing method of an html page in the vbscript code.

Code:

const ApiKey = "<replace with your api key here>"
const Url = "https://api.twelvedata.com/quote?symbol="
Dim http

function ParseJson(strJson)
  dim html : set html = CreateObject("htmlfile")
  dim window : set window = html.parentWindow
  window.execScript "var json = " & strJson, "JScript"
  set ParseJson = window.json
  set window = nothing
  set html = nothing
end function

Function Request(Url)
        Dim fullUrl, resolveTimeout, connectTimeout, sendTimeout, receiveTimeout
        fullUrl = Url & cb_currency_from.text & "/" & cb_currency_to.text & "&apikey=" & ApiKey
        resolveTimeout = 5 * 1000
        connectTimeout = 5 * 1000
        sendTimeout = 15 * 1000
        receiveTimeout = 15 * 1000
        Set http = CreateObject("MSXML2.ServerXMLHTTP")
        With http
        .onreadystatechange = getRef("state_Change")
        .setTimeouts resolveTimeout, connectTimeout, sendTimeout, receiveTimeout
        .Open "GET", fullUrl, true
        .Send
        End With
End function

Function state_Change()
        If http.ReadyState = 4 Then
                If http.Status = 200 Then
                        tb_data.text = http.ResponseText
                        lb_currencyData.Items.Clear()
                        Dim json : Set json = ParseJson(http.ResponseText)
                        Dim symbol : symbol = eval("json.symbol")
                        If Not IsNull(symbol) Then
                                 lb_currencyData.Items.Add(symbol)
                        End If
                        Dim name : name = eval("json.name")
                        If Not IsNull(name) Then
                                 lb_currencyData.Items.Add(name)
                        End If
                        Dim datetime : datetime = eval("json.datetime")
                        If Not IsNull(datetime) Then
                                 lb_currencyData.Items.Add(datetime)
                        End If
                        Dim high : high = eval("json.high")
                        If Not IsNull(high) Then
                                 lb_currencyData.Items.Add("high:"&high)
                        End If
                        Dim low : low = eval("json.low")
                        If Not IsNull(low) Then
                                 lb_currencyData.Items.Add("low:"&low)
                        End If
                        Dim percent_change : percent_change = eval("json.percent_change")
                        If Not IsNull(percent_change) Then
                                 lb_currencyData.Items.Add("percent_change:"&percent_change)
                        End If
                Else
                        tb_data.text = ""
                        Set http = Nothing
                End If
        End If
End function

Sub OnBtnGetDataClick(Sender)
        tb_data.text = ""
        Request(Url)
End sub

Sample of JSON response to query:

{
"symbol":"USD/EUR",
"name":"US Dollar Euro",
"exchange":"Forex",
"datetime":"2023-04-24",
"timestamp":1682338600,
"open":"0.90975",
"high":"0.91190",
"low":"0.90735",
"close":"0.90890",
"previous_close":"0.90995",
"change":"-0.00105",
"percent_change":"-0.11539",
"average_volume":"0",
"is_market_open":true,
"fifty_two_week":{
"low":"0.90285",
"high":"1.04870",
"low_change":"0.00605",
"high_change":"-0.13980",
"low_change_percent":"0.67011",
"high_change_percent":"-13.33079",
"range":"0.902850 - 1.048700"
}
}

VTW User interface:

Shopping Cart