Consuming JSON APIs with Go —CurrencyLayer

We live in a time where there is an API for almost anything.

Part 2


These study notes is an attempt to write about “Consuming various JSON APIs with Go” as I explore and experiment, in the hope that a new Go developer can understand and get up-to-speed with them in no time.

ProgrammableWeb is the world’s largest API repository (also do look at Mashape) and the second API I am going to take a look at is CurrencyLayer.

Currencylayer provides a simple REST API with real-time and historical exchange rates for 168 world currencies, delivering currency pairs in universally usable JSON format — compatible with any of your applications.

After signing up (Free Plan), every user is assigned a personal API Access Key — a unique “password” used to make requests to the API.

To authenticate with the currencylayer API, simply attach your access_key to your preferred Endpoint URL:

http://apilayer.net/api/live?access_key=YOUR_ACCESS_KEY

API Response

The standard API response is returned in JSON format consisting of different objects and properties. Along with the “source” currency (default USD), a “timestamp” and some legal reminders, the API will return a “quotes” object containing all available or specified currency pairs with their respective exchange rate values (quotes).

What we need

We want to know what 100 Indian Rupees (INR) is worth in Euros (EUR).

currencylayer.go

In your browser window type:

http://apilayer.net/api/live?access_key=YOUR_ACCESS_KEY&currencies=EUR,INR&source=USD

Copy the JSON output (in your browser) to the JSON-to-Go tool that instantly converts JSON into a Go type definition. Using it, I get:

Our Go program “currencylayer.go” is a bare bones program showing you how to access the “currencylayer” API. Later on you could convert this to a web app and host it on Google App Engine or Heroku.

Here’s the complete program:

Almost all the functions used in the above program have been explained in Part 1 of this series.

For our free plan, the response “source” is always in USD. Our objective was to find out what 100 Indian Rupees are worth in Euros. The formula used is:

(USDEUR rate / USDINR rate) * 100

That’s it!


Note: I would love your feedback on these study notes. If I can make it better, I’d love it!


In Part 1 we talked about using NumVerify API that offers a full-featured yet simple RESTful JSON API for national and international phone number validation and information lookup for a total of 232 countries around the world.