Currency Exchange API


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

Once you register with, 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:

// 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();
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}"GET", theURL, false);
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(""));

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


  1. The currency exchange rate API host,
  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


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:





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

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lyman Johnson

Lyman Johnson

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

More from Medium

How to store uploaded files in the S3 bucket when using elastic beanstalk PHP App

What exactly is a REST / RESTful API?

Deploying web application on CI/CD Pipeline using Heroku and GitHub Actions

A Guide to Hosting a Database