API Tutorial: Using CoinMarketCap API to analyze cryptocurrencies market

Yasu
6 min readJul 5, 2019

--

CoinMarketCap is a website offering cryptocurrency market capitalizations with many graphs, charts, and other useful data to help you make educated cryptocurrency trading and investment decisions. It offers live, as well as historical data going back as far as 2013.

CoinMarketCap is a popular service, which receives monthly more than 78 Million estimated visits according to Similarweb:

In this tutorial, we will cover how to leverage the CoinMarketCap API to list cryptocurrencies and get global aggregated market metrics via the CoinMarketCap API using javascript and node.js.

Setup

To get started you’ll need

  1. Javascript editor and npm unirest package that will speed up code writing
  2. Rakuten RapidApi account and the API key that comes with it

Getting the Javascript Code

Rakuten RapidApi got you covered and packaged all the javascript code you need to work with the API in an easy unirest SDK, which is a library to simplify HTTP REST requests and save you time writing code.

Get started by installing the unirest package with the following command:

  1. npm install unirest — save

As soon as that’s done, you can start exploring the API. It’s safe to ignore the npm warn messages at this point if you see any.

Start with Signing Up to Rakuten RapidApi

About Rakuten RapidAPI

Rakuten RapidAPI is the world’s largest API marketplace with 8,000+ third-party APIs and used by over 500,000 active developers. We enable developers to build transformative apps through the power of APIs. Find, test and connect to all the APIs you need in one place!

We’ve also got you covered with free CoinMarketCap API access. You can try it, without leaving the browser, on the CoinMarketCap API page. You’ll need to sign up to get the API keys and make requests to the API. With these keys you’ll be able to make and authenticate the requests, so we will know they come from you.

As soon as you sign-in with your account, the view above will be replaced with another view, where you can see your API keys (X-RapidAPI-Key) in the Request Headers section and a ready to copy snippet of code at the right side.

CoinMarketCap API Overview

CoinMarketCap provides through Rakuten RapidAPI 3 endpoints, each of which covers a specific feature that you can use:

  • getCryptocurrenciesList: Endpoint that returns data around cryptocurrencies such as ordered cryptocurrency lists or price and volume data.
  • getCryptocurrency: Endpoints that return data around cryptocurrency exchanges such as ordered exchange lists and market pair data.
  • getGlobalData: Endpoints that return aggregate market data such as global market cap and BTC dominance.

Making sense of the cryptocurrency market

Before we can start trading or investing in the different cryptocurrencies, we need to understand the market, analyze how each currency fluctuates, and determine the best time to make our purchase/sale.

We’re going to tackle this problem by using the CoinMarketCap API to retrieve the relevant market data.

Let’s get started.

Listing cryptocurrencies

The first endpoint we will be covering allows listing the cryptocurrency market data.

These are the parameters we need to set:

Query Params:

startintegerThe offset of the first item of the pagelimitintegerThe maximum number of items to return for the pageconvertstringReturn price, 24h volume, and market cap in terms of another currency.

Upon successful completion, the CoinMarketCap API returns the following response body which contains a callback key indicating the status of the request, and a contextWrites.to key which is an array of objects each representing a cryptocurrency:

Response body:

  1. {
  2. “callback”: “success”,
  3. “contextWrites”: {
  4. “to”: [ // Array of cryptocurrencies
  5. {
  6. “id”: “bitcoin”,
  7. “name”: “Bitcoin”,
  8. “symbol”: “BTC”,
  9. “rank”: “1”,
  10. “price_usd”: “3482.03529556”, // Price in USD
  11. “price_btc”: “1.0”, // Price in BTC
  12. “24h_volume_usd”: “5936542451.41”,
  13. “market_cap_usd”: “60971176217.0”, // Market cap in specified currency
  14. “available_supply”: “17510212.0”,
  15. “total_supply”: “17510212.0”, // Approx. number of coins in existence
  16. “max_supply”: “21000000.0”, // Max number of coins to be available
  17. “percent_change_1h”: “0.03”, // Percent of change in the last 1h
  18. “percent_change_24h”: “0.76”, // Percent of change in the last 24h
  19. “percent_change_7d”: “-2.97”, // Percent of change in the last 7 days
  20. “last_updated”: “1548883704”
  21. },
  22. ]
  23. }
  24. }

Code snippet:

  1. const unirest = require(‘unirest’);
  2. const API_KEY = “YOUR_API_KEY_HERE”;
  3. unirest.post(“https://CoinMarketCapzakutynskyV1.p.rapidapi.com/getCryptocurrenciesList")
  4. .header(“X-RapidAPI-Key”, API_KEY)
  5. .header(“Content-Type”, “application/x-www-form-urlencoded”)
  6. .end(function (result) {
  7. console.log(result.status, result.headers, result.body);
  8. });

Get a cryptocurrency

This endpoint allows retrieving the data for one cryptocurrency. In this example, we’re going to retrieve the Bitcoin data, identified by the id bitcoin.

Query Params:

idstringThe id of the cryptocurrency wantedconvertstringReturn price, 24h volume, and market cap in terms of another currency.

Upon successful completion, the CoinMarketCap API returns the following response body that contains bitcoin related data:

Response body:

  1. {
  2. “callback”: “success”,
  3. “contextWrites”: {
  4. “to”: {
  5. “id”: “bitcoin”,
  6. “name”: “Bitcoin”,
  7. “symbol”: “BTC”,
  8. “rank”: “1”,
  9. “price_usd”: “3482.03529556”, // Price in USD
  10. “price_btc”: “1.0”, // Price in BTC
  11. “24h_volume_usd”: “5936542451.41”,
  12. “market_cap_usd”: “60971176217.0”, // Market cap in specified currency
  13. “available_supply”: “17510212.0”,
  14. “total_supply”: “17510212.0”, // Approx. number of coins in existence
  15. “max_supply”: “21000000.0”, // Max number of coins to be available
  16. “percent_change_1h”: “0.03”, // Percent of change in the last 1h
  17. “percent_change_24h”: “0.76”, // Percent of change in the last 24h
  18. “percent_change_7d”: “-2.97”, // Percent of change in the last 7 days
  19. “last_updated”: “1548883704”
  20. }
  21. }
  22. }

Code snippet:

  1. const unirest = require(‘unirest’);
  2. const API_KEY = “YOUR_API_KEY”;
  3. unirest.post(“https://CoinMarketCapzakutynskyV1.p.rapidapi.com/getCryptocurrency")
  4. .header(“X-RapidAPI-Key”, API_KEY)
  5. .header(“Content-Type”, “application/x-www-form-urlencoded”)
  6. .send(“id=bitcoin”)
  7. .end(function (result) {
  8. console.log(JSON.stringify(result.body, null, 4));
  9. });

Listing all aggregate market metrics

This final endpoint allows retrieving global aggregated market metrics.

Query Params:

convertstringComma-separated list of up to 40 currencies to be used for market quotes calculations

Upon successful completion, the CoinMarketCap API returns the following response body:

Response body:

  1. {
  2. “callback”: “success”,
  3. “contextWrites”: {
  4. “to”: {
  5. “total_market_cap_usd”: 120743650384, // Total market cap
  6. “total_24h_volume_usd”: 18125459038, // Total market volume for last 24h
  7. “bitcoin_percentage_of_market_cap”: 52.99, // Percentage of market cap for bitcoin
  8. “active_currencies”: 846, // Number of active currencies
  9. “active_assets”: 1216, // Number of active assets
  10. “active_markets”: 16011, // Number of active markets
  11. “last_updated”: 1549815742 // Last updated timestamp for the global data
  12. }
  13. }
  14. }

Code snippet:

  1. const unirest = require(‘unirest’);
  2. const API_KEY = “YOUR_API_KEY_HERE”;
  3. unirest.post(“https://CoinMarketCapzakutynskyV1.p.rapidapi.com/getGlobalData")
  4. .header(“X-RapidAPI-Key”, API_KEY)
  5. .header(“Content-Type”, “application/x-www-form-urlencoded”)
  6. .end(function (result) {
  7. console.log(JSON.stringify(result.body, null, 4));
  8. });

That’s It

You are ready to start analyzing the cryptocurrencies market and make better trading and investments decisions. Feel free to use the code in your production applications and check out APIs on Rakuten RapidAPI to enhance your application even further.

Original Post: https://blog.api.rakuten.net/using-coinmarketcap-api/

About Rakuten RapidAPI

Rakuten RapidAPI is the world’s largest API marketplace with 8,000+ third-party APIs and used by over 500,000 active developers. We enable developers to build transformative apps through the power of APIs. Find, test and connect to all the APIs you need in one place!

Check out some of the world’s best APIs including Microsoft, Sendgrid, Crunchbase, and Skyscanner.

Facebook | LinkedIn | Twitter

--

--