Currency Exchange API

This might be a cludge

Lyman Johnson
2 min readJul 31, 2017

BACKGROUND:

Our latest assignment involved fetching the prices of various etsy items and calculating their average price.

The items were stored as objects in an array, with one of the object attributes being price.

One of the other attributes was currency. As an added challenge I decided to make my code automatically convert non-USD items into USD using a currency exchange rate API.

To do this I used a free currency exchange API at www.apilayer.net

Once you register with apilayer.net, they provide a URL that links to a website with a single block of text, that looks like an object {} filled with key:value pairs of CURRENCY-CODE:EXCHANGE-RATE.

It looks like this:

{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"timestamp":1501519146,
"source":"USD",
"quotes":{
"USDAED":3.672903,
"USDAFN":68.370003,
"USDALL":112.400002,
"USDAMD":477.859985,
"USDANG":1.770284,
"USDAOA":165.093002,
"USDARS":17.653999,
"USDAUD":1.252602,
"USDAWG":1.79,
"USDAZN":1.700402,
"USDBAM":1.657901,
"USDBBD":2,
"USDBDT":81.209999,
"USDBGN":1.667802,
"USDBHD":0.376902,
"USDBIF":1722.209961,
"USDBMD":1,
"USDBND":1.355803,
// way more currencies after this, deleted for brevity - lyman
}
}

I structured my code to:

  1. Grab that text from the website as a string
  2. Parse that string into an object
  3. Access the appropriate exchange rate (value) by calling its currency code (key) as you would with any other object.

There was some more devil in the details, but that’s the basic idea. Read the // comments in the .js file (link 5 below) for more details.

My code also had some extra frills to display the results in a readable, grammatically correct fashion. However, I don’t want to discuss that in this blog post as it’s unrelated to accessing API.

THE BIG TAKEAWAY: These two blocks of code access the API and store its contents as an object in your javascript

1. This first function retrieves the text contents from an external website and spits them out as a string:

function loadXMLDoc(theURL)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari, SeaMonkey
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", theURL, false);
xmlhttp.send();
return xmlhttp.responseText;
}

2. This next part takes the string created by the previous function and turns it into an object, storing it as the const "exchangeTable". JSON.parse() turns strings into objects:

const exchangeTable = JSON.parse(loadXMLDoc("http://www.apilayer.net/api/live?access_key=YOUR-ACCESS-TOKEN&format=1"));

IMPORTANT: You need to replace “YOUR-ACCESS-TOKEN” in the above URL with your own token that you get by registering at apilayer.net. There is a free option.

MAIN LINKS:

  1. The currency exchange rate API host, www.apilayer.net
  2. Loading the text from an external website into a variable in your code
  3. Turning a string into an object
  4. Notes about eval(string) function, which turns a string into a line of code to be executed right there where you wrote it. (I found this necessary in oder to dynamically access the correct currency code key based on what the etsy item’s currency code was.)
  5. The final version of my code on github

NOTE:

For the API to work you need to register with the website to obtain your own access token. The url used to access your exchange rate object will be of the format:

  • http://www.apilayer.net/api/live?access_key=YOUR-ACCESS-TOKEN-HERE&format=1

--

--

Lyman Johnson

Django Developer | NCEES-Licensed Engineer in New York, California, and North Carolina