How the coinlayer API Helps Integrate Cryptocurrency Pricing Into Your App

Introduction

With the virtual explosion of cryptocurrency since Bitcoin's launch in 2009, there's a strong need for software services to help with the plethora of currencies, their pricing and exchange rates. With the coinlayer real time cryptocurrency API, you can keep up with more than 386 cryptocurrencies collected from more than 25 exchanges.

coinlayer is incredibly useful to developers building applications that aim to integrate cryptocurrency for calculation, payment, transfer and tracking.

The home page for the coinlayer.com website.

The home page for the coinlayer.com website.

For example, let's say you want to build a Venmo-like app that enables friends to pay each other with cryptocurrency. coinlayer can help you manage the data and calculations needed for peer to peer exchanges in real time. Or, if you launch your own cryptocurrency, coinlayer will help you price the exchange rate for customers buying your currency with another. coinlayer can also help you build a site that tracks and charts the evolution of crypto pricing or, you could build a cryptocurrency pricing ticker for your financial news website or blog.

Let your crypto-creativity guide you. Cryptocurrency remains a platform ripe for innovation, one which coinlayer is ready to support with real time data.

coinlayer's crypto data is gathered from some of the largest cryptocurrency exchanges and it's easy to use. You can use the API with straightforward HTTP GET requests and receive easily transformable JSON responses.

Further below, I'll show you how I generated a multi-series D3.js chart of historical cryptocurrency prices (in Euros) dynamically from a coinlayer API query.

apilayer is the service provider behind coinlayer. They're also the people that run currencylayer, a real time currency pricing and exchange rate service with more than 100,000 customers. The apilayer team also maintains the Fixer API which currencylayer evolved from.

I've written about apilayer products before, the company does an excellent job providing well-designed, easy to integrate, fast, scalable and affordable web services for a variety of customers. See my prior tutorials about them below:

coinlayer itself already has 25,000 customers. If you're building solutions for a large corporation, you can count on their reliability, quality of service and support. And, if you're a startup developer, they always offer a generous free plan with all their services.

Getting Started with the coinlayer API

Get Your Free Cryptocurrency API Key

To get started with coinlayer, click the red GET FREE API KEY button in the navigation bar. You can start with the free plan or choose a higher capacity plan. At this time, you can also select monthly or annual billing for as much as a 20% discount.

coinlayer Pricing

coinlayer's pricing page showing Free, Basic $9.99, Professional $39.99, Professional Plus $79.99 and custom volume plans.

coinlayer's pricing page showing Free, Basic $9.99, Professional $39.99, Professional Plus $79.99 and custom volume plans.

For individuals just starting out with the API, there is free account that allows 500 requests per month based on hourly data of currency pricing against the US dollar.

For $9.99 per month, you can price any cryptocurrency against any foreign currency and price exchanges with any other currency. You can make 5,000 requests per month and gain secure SSL encryption for security and privacy.

They allow you to exceed your account level volume limit by 20% before blocking access.

As your volume goes up additional accounts are available. Real time cryptocurrency pricing with 60 second updates is available at the $79.99 per month level, which provides 100,000 requests per month. Contact the company for higher volume, enterprise requirements.

Discounts are also available for annual subscribers.

The Sign Up Form

Once you've chosen a plan you'll be asked to complete a sign up form. It's very straightforward:

Blank sign up form at coinlayer's website.

Blank sign up form at coinlayer's website.

Once complete, you'll land in the dashboard and receive a friendly email with links to documentation and support.

Example email received after sign up at coinlayer.

Example email received after sign up at coinlayer.

The coinlayer API Dashboard

coinlayer's API dashboard provides your API key and a simple 3 step quickstart guide:

3-step quickstart guide at coinlayer after sign up.

3-step quickstart guide at coinlayer after sign up.

Step 1 - Your API Access Key

Your API access key provides access to the coinlayer API. It's required to be included as a parameter in every call. You can also reset the key to secure a new one whenever you wish.

Step 2 - Reviewing the Six API Endpoints

The coinlayer quick start provides a simple tabbed base dialog for exploring its six endpoints.

Six tabbed dialog of quickstart examples on their website introducing each method, open to second tab Live endpoint.

Six tabbed dialog of quickstart examples on their website introducing each method, open to second tab Live endpoint.

Before I dive into reviewing each method, here's a quick overview:

  • List - lists all 386 cryptocurrencies and international target currencies for conversion
  • Live - provides real time pricing for any number of cryptocurrencies against a country's financial currency
  • Historical - provides pricing for cryptocurrencies for a specific date in time
  • Convert - provides real time exchange and conversion pricing between cryptocurrencies as well as between international currencies
  • Timeframe - provides historical data for cryptocurrencies for a specific time range e.g. daily for the twelve months of 2014
  • Change - provides data on the fluctuation range of a cryptocurrency between two dates in time

The Live Endpoint

Let's try out the API for the first time with its live endpoint, which tells us the real time value of any cryptocurrencies we request:

For example, a request to the following URL:

https://api.coinlayer.com/api/live?access_key=XXXXXXX&symbols=BTC,ETH,ZEC

This returns JSON results:

{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","timestamp":1532114830,"target":"USD","rates":{"BTC":7381.516325,"ETH":446.777982,"ZEC":186.641816}}

Note the UNIX timestamp and the rates for Bitcoin, Ethereum and Zcash.

Querying the Endpoints in Multiple Languages

You can also query the endpoints easily in multiple languages. coinlayer's documentation provides examples in both PHP and JavaScript. Here's the one in PHP using cURL:
 

Screenshot of PHP cURL example of calling their API methods. Code available in their documentation.

Screenshot of PHP cURL example of calling their API methods. Code available in their documentation.

And here's one in JavaScript using AJAX:

Screenshot of jQuery AJAX example of calling their API methods. Code available in their documentation.

Screenshot of jQuery AJAX example of calling their API methods. Code available in their documentation.

Parsing the JSON Response Data

Parsing the JSON responses is fairly straightforward whichever language you prefer in the moment.

Here's an example of programmatically parsing the coinlayer API response using PHP's json_decode as shown above. Use the PHP interactive shell to try it:

$ php -a
php > print_r( json_decode('{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","timestamp":1532114830,"target":"USD","rates":{"BTC":7381.516325,"ETH":446.777982,"ZEC":186.641816}}',true));

This returns the following structured array:

Array
(
    [success] => 1
    [terms] => https://coinlayer.com/terms
    [privacy] => https://coinlayer.com/privacy
    [timestamp] => 1532114830
    [target] => USD
    [rates] => Array
        (
            [BTC] => 7381.516325
            [ETH] => 446.777982
            [ZEC] => 186.641816
        )
)

Or, you can use the JavaScript JSON.parse() method for JavaScript in your browser's developer console:

Screenshot of console output after JSON.parse operation on API result.

Screenshot of console output after JSON.parse operation on API result.

You can see how quickly and easily it is to translate the JSON strings coinlayer returns into usable arrays or objects.

Step 3 - Integrate into your application

Next, we're ready to dive deeper into this real time cryptocurrency API. Let's jump to the documentation and try out more methods.

coinlayer's Cryptocurrency API Documentation

One great aspect about coinlayer and apilayer is that they provide solid, well-organized, easy to follow documentation. And, it's generally consistent across their suite of services.
 

Screenshot of landing page for coinlayer API documentation.

Screenshot of landing page for coinlayer API documentation.

Let's continue on.

The List Endpoint

The list endpoint allows you to enumerate the 386 cryptocurrencies that coinlayer supports as well as the 186 target fiat currencies which it will price the cryptocurrencies against. A "fiat" currency is one in which its government issue stands behind the value of it.

Screenshot of tabbed quickstart dialog open to first List endpoint.

Screenshot of tabbed quickstart dialog open to first List endpoint.

Here's an example request to the list endpoint:

http://api.coinlayer.com/list?access_key=XXXXXXXXXXXXXXXX

The cryptocurrencies are returned as a crypto data set and include its name and symbol, its maximum supply, as well as an image URL for its logo:

{
  "success": true,
  "crypto": {
    "611": {
      "symbol": "611",
      "name": "SixEleven",
      "name_full": "SixEleven (611)",
      "max_supply": 611000,
      "icon_url": "https://coinlayer.com/images/assets/coinlayer_icons/611.png"
    },
    "ABC": {
      "symbol": "ABC",
      "name": "AB-Chain",
      "name_full": "AB-Chain (ABC)",
      "max_supply": 210000000,
      "icon_url": "https://coinlayer.com/images/assets/coinlayer_icons/ABC.png"
    },
    [...]
  },

Here are examples of rendered logos returned for Bitcoin, Ethereum and Digital Cash from the response's image URLs:

Three logos: Bitcoin, Ethereum and Digital Cash (DASH) cryptocurrency.

Three logos: Bitcoin, Ethereum and Digital Cash (DASH) cryptocurrency.

And, list returns a data set of named "fiat" listing the currencies.

"fiat": {
    "AED": "United Arab Emirates Dirham",
    "AFN": "Afghan Afghani",
    "ALL": "Albanian Lek",
    "AMD": "Armenian Dram",
    "ANG": "Netherlands Antillean Guilder",
    "AOA": "Angolan Kwanza",
    [...]
  }
}

You can also browse the 386 cryptocurrencies which apilayer supports from their website. Here's a screenshot of some of them:

Partial screenshot of first ten cryptocurrencies supported by coinlayer. The top of the list of 386 cryptocurrencies.

Partial screenshot of first ten cryptocurrencies supported by coinlayer. The top of the list of 386 cryptocurrencies.

Similarly, you can browse the 166 target currencies which coinlayer can price and convert to.

Let's move on to the next cryptocurrency API endpoint.

The Historical Endpoint

The historical endpoint allows you to request currency values from any point in time.

Tabbed apilayer quickstart dialog open to Historical endpoint.

Tabbed apilayer quickstart dialog open to Historical endpoint.

Here's an example historical request for Bitcoin and Ethereum pricing on January 1, 2017:

http://api.coinlayer.com/api/2017-01-01?access_key=XXXXXXX&symbols=BTC,ETH

Here is its result for that date:

{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","timestamp":1483228800,"target":"USD","historical":true,"date":"2017-01-01","rates":{"BTC":995.44,"ETH":8.14}}

Rather than iterate individually for data, I will use the Timeframe endpoint for a greater range of dates. Let's check it out.

The Timeframe Endpoint

Tabbed apilayer quickstart dialog open to Timeframe endpoint.

Tabbed apilayer quickstart dialog open to Timeframe endpoint.

You can use the timeframe endpoint to gather historical data for analysis, sharing historical tables of data and it can be used for charting, as you'll see me experiment with in a moment.

I've been primarily showing you the simpler Quickstart screenshots but here is what the coinlayer documentation looks like. Notice how each API endpoint tells you which plans they work with (see below, upper right corner):

Screenshot of apilayer documentation for Time-Frame API endpoint.

Screenshot of apilayer documentation for Time-Frame API endpoint.

The Timeframe endpoint is available at the Professional account level and above.

In this example, I gather January to April pricing data against the Euro for Bitcoin, Ethereum and Litecoin:

https://api.coinlayer.com/api/timeframe?access_key=XXXXXXXXXX&start_date=2018-01-01&end_date=2018-04-30&target=EUR&symbols=BTC,ETH,LTC

As you can see, it returns a lot of data:

Here's an example request:{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","timeframe":true,"start_date":"2018-01-01","end_date":"2018-04-30","target":"EUR","rates":{"2018-01-01":
{"BTC":11190.116412,"ETH":629.382042,"LTC":186.717227},"2018-01-02":
{"BTC":12226.710639,"ETH":714.312384,"LTC":208.674317},"2018-01-03":{"BTC":12614.831652,"ETH":783.276091,"LTC":203.605175}, …

"2018-04-27":{"BTC":7395.718114,"ETH":532.91795,"LTC":121.153179},"2018-04-28":
{"BTC":7704.446942,"ETH":564.346876,"LTC":125.464025},"2018-04-29":
{"BTC":7763.828153,"ETH":568.407412,"LTC":126.799785},"2018-04-30":{"BTC":7651.040843,"ETH":554.788575,"LTC":123.013965}}} ...

Now, let's use these results to feed another data driven library, charting.

Dynamic Charting with coinlayer Cryptocurrency API Results

You can also learn to take dynamic, real time data from coinlayer and apply it to your applications. In this example, I will build a multi-series D3.js chart from a coinlayer historical request.

I used Building a Multi-Line Chart Using D3.js, a charting tutorial from EnvatoTuts+, to create a very rough multi-series chart of dynamically retrieved historical coinlayer data. I apologize for my limited D3.js skills. The chart is fairly primitive overall. But, it clearly shows the power of combining dynamic real time pricing and exchange data from coinlayer with other kinds of services and applications.
 

Screenshot of rough example of multi-series D3 chart for seven cryptocurrencies.

Screenshot of rough example of multi-series D3 chart for seven cryptocurrencies.

Here's the code that queries six months of historical data for seven different cryptocurrencies and builds an array usable by the D3.js tutorial demo.

function getData() {
  // set endpoint and your API key
  endpoint = 'timeframe';
  access_key = 'XXXXXXXXXXX';
  // define request variables
  symbols = 'DASH,LTC,ETH,XMR,XRP,ZEC,BCH';
  target = 'EUR';
  start_date='2018-01-01';
  end_date='2018-06-30';
  endpointUrl = 'https://api.coinlayer.com/api/' + endpoint +
    '?access_key=' + access_key +
    '&target=' + target + '&start_date=' + start_date + '&end_date=' + end_date +
    '&symbols=' + symbols;
  $.ajax({
      url: endpointUrl,
      success: function(json) {
        rates = json.rates;
        data=[];
        indexCnt=0;
    // transpose the results into a structure for the charting
        $.each(rates, function( index, value ) {
            $.each(value, function (idx,val) {
              data[indexCnt]={
                "Coin":idx,
                "price":val,           
"Date":
(toTimestamp(index)-toTimestamp(start_date))/(3600*24,
                };
                indexCnt+=1;
            });
          });
          InitChart(data);
      }
  });
}

When the code calls the InitChart(data) function, it passes the coinlayer timeframe API results to the modified charting code.

The X axis labels below are the count of the number of days since January 1, 2018, thru July 2018.

Obviously, this is an imperfect chart, but I placed the script of the demo you can try out from a Github Gist and adapt to your needs. There are some great stock charting scripts for D3.js that you might explore on your own; just google "d3 stock chart example".

Let's continue on to the last two API endpoints!

The Convert Endpoint

The convert endpoint provides for coinlayer's real time capability to calculate cryptocurrency exchange rates between both two cryptocurrencies and between one cryptocurrency and a fiat currency. It's one of coinlayer's most most interesting features. You can even request currency exchange rates by date to look back in time.

The free plan does not have access to this endpoint, but the basic plan and higher do. I'll show you how to upgrade your plan in a moment.
 

Screenshot of apilayer Quickstart tabbed dialog open to Convert endpoint.

Screenshot of apilayer Quickstart tabbed dialog open to Convert endpoint.

[[image 18]] Alt text: Screenshot of apilayer Quickstart tabbed dialog open to Convert endpoint.

Here's an example request:

https://api.coinlayer.com/api/convert?access_key=XXXXX&from=BTC&to=DASH&amount=25

The results show that Bitcoin is 30.083776 times more valuable than DASH cryptocurrency; so 25 BTC yields 751.8444 DASH.

{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","query":{"from":"BTC","to":"DASH","amount":25},"info":{"timestamp":1532152029,"rate":30.073776},"result":751.8444}

Then, let's ask how many US dollars the DASH coins create:

https://api.coinlayer.com/api/convert?access_key=a2089ba7552c20b7a4f08ab4b733bd8c&from=DASH&to=USD&amount=751.8444

The rate is 243.62, yielding $183,166.34 US Dollars:

{"success":true,"terms":"https:\/\/coinlayer.com\/terms","privacy":"https:\/\/coinlayer.com\/privacy","query":{"from":"DASH","to":"USD","amount":751.8444},"info":{"timestamp":1532152150,"rate":243.622669},"result":183166.339401}

The Change Endpoint

Now let's explore the change endpoint, coinlayer's final endpoint. This allows you to track the fluctuation of a currency within a timeframe using a target fiat currency.

This endpoint is only available for Professional Plus and Enterprise plans.
 

Screenshot of apilayer Quickstart tabbed dialog open to Change endpoint.

Screenshot of apilayer Quickstart tabbed dialog open to Change endpoint.

Here's an example change request, which asks to see the fluctuation for Bitcoin, Ethereum and Ripple in April 2018: Note that our WYSIWYG likes the tags in the following manner

https://api.coinlayer.com/change
    ? access_key = YOUR_ACCESS_KEY
    &smp; start_date = 2018-04-01
    &smp; end_date = 2018-04-30
    & symbols = BTC,ETH,XRP

And, here's the response it yields. It measures cryptocurrency fluctuation in net change and change in percent:

{
  "success": true,
  "terms": "https://coinlayer.com/terms",
  "privacy": "https://coinlayer.com/privacy",
  "change": true,
  "start_date": "2018-04-01",
  "end_date": "2018-04-30",
  "target": "USD",
  "rates": {
    "BTC": {
      "start_rate": 6903.113849,
      "end_rate": 9245.982724,
      "change": 2342.86887,
      "change_pct": 1.33939305
    },
    "ETH": {
      "start_rate": 383.02749,
      "end_rate": 670.440229,
      "change": 287.412739,
      "change_pct": 1.75037105
    },
    "XRP":{
      "start_rate": 0.482917,
      "end_rate": 0.831833,
      "change": 0.348916,
      "change_pct": 1.72251753
    }
  }
} 

Upgrading Your Account

coinlayer is a subscription-based service and your chosen plan renews automatically each month. You can upgrade, downgrade or cancel anytime.

You might want to upgrade your account for any one or combination of these reasons:

  • To use SSL encryption
  • To calculate currency exchange
  • To calculate currency fluctuation
  • You need higher transaction limits
  • You need closer to real time pricing

And, coinlayer provides a page to calculate your usage statistics in the current period and historically over time. (I've just recently been using my account so the usage is over a brief timeframe.)

Screenshot of apilayer This Month's API Usage chart with statistics table by day.

Screenshot of apilayer This Month's API Usage chart with statistics table by day.

If you're ready to upgrade from your free account level to try some of these additional APIs, go to the Dashboard and click Subscription Plan.

Screenshot of apilayer pricing table for account upgrades. Same pricing as new account levels described earlier.

Screenshot of apilayer pricing table for account upgrades. Same pricing as new account levels described earlier.

In Conclusion

I hope you've enjoyed this introduction. Writing about coinlayer has been fun for me.

I always enjoy catching up with the apilayer team and writing about their new projects. They are sharp technologists always offering utilitarian services with affordable prices, easy to integrate APIs and high performing scalable technologies. coinlayer is an essential cryptocurrency API for our digital currency future. Check out their suite of products and you'll likely find more that interest you.

I hope you'll check out the coinlayer real time cryptocurrency API too and currencylayer. As you build with their pricing and cryptocurrency conversion API, please post examples in the comments to share with readers.

I'll be writing about another service apilayer is launching next month. I can't tell you what it is but I guarantee it will appeal to almost any web or application developer. Stay tuned!

apilayer and coinlayer appreciate your questions, comments and feedback. You can also follow them on Twitter @apilayernet and the apilayer Facebook page.

About apilayer

coinlayer is the most recent solution from apilayer, an established leader in product APIs. apilayer aims to help developers and businesses automate and outsource complex processes by serving them with dedicated and handy programming interfaces.

Two other products by apilayer include Invoicely, the invoicing platform, and the geo API, streetlayer, the latter of which I wrote about recently for ProgrammableWeb.

Jeff Reifman is the founder of Meeting Planner, making scheduling meetings simple, and Lookahead Consulting for your software development and content needs.
 

Comments (0)