Back to Documentation

ASP.NET Sample Code for Currency Conversion

<%
currcall = convertCurrency("GBP", "EUR", "YOUR-API-KEY")

currFrom = currcall(0)
currTo = currcall(1)
currrate = currcall(2)

'response.write currFrom & "<br />"
'response.write currTo & "<br />"
'response.write currrate & "<br />"

Function convertCurrency(currFrom, currTo, apiKey)
  apikey = apiKey
  from_currency = currFrom
  to_currency = currTo

  Server.URLEncode(from_currency)
  Server.URLEncode(to_currency)

  query =  from_Currency & "_" & to_Currency
  json = file_get_contents("https://api.currencyconverterapi.com/api/v6/convert?q=" & query & "&compact=ultra&apiKey=" & apikey & "")

  if json <> "{}" then

    curr = split(json, ":")
    currfrom = mid(curr(0),3,3)
    currto = mid(curr(0),7,3)
    currrate = cstr(curr(1))
    currrate = left(curr(1), len(currrate)-1)

  end if

  convertCurrency = array(currfrom, currto, currrate)

end Function

function file_get_contents(sURL)
  dim xmlhttp, sResult
  set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
  xmlhttp.open "GET",sURL, false
  xmlhttp.send sURL
  sResult = xmlhttp.ResponseText
  set xmlhttp = nothing
  file_get_contents = sResult
end function
%>

Special thanks to Mark Pepper