Crypto Data API for Javascript Developers: A Beginner’s Guide

Grant Ferowich
Coinmonks
4 min readFeb 25, 2020

--

Get 20% off any CoinGecko API plan when you subscribe with my referral code “GRANT20” at www.coingecko.com/api.

Software developers talk about APIs a lot. What is an API? API stands for an application programming interface. I wanted to gather specific market data on Bitcoin, Ethereum and other digital assets for a JavaScript project I was working on. My first stop was CoinGecko.

CoinGecko is free to use and allows up to 100 requests per minute.

I chose CoinGecko’s API because the other major digital asset information site, CoinMarketCap, is notorious for publishing fake volume data on coins.

  1. Decide what data you want to get. My application required current market price for a given coin, and the market price of a coin on a certain historical date, and a snapshot of the current market.

a. Go to https://www.coingecko.com/en/api and scroll about halfway down. There, you’ll see the types of requests you can make. Since my application needs to bring in some data from CoinGecko, I want to make a GET request. That’s exactly what CoinGecko enables. Wicked.

b. I will use /simple/price to get current market prices of coins X, Y, and Z. I will use /coins/{id}/history to get the price of a coin at some previous date.

2. Test how the data will be sent to your application. The testing environment on CoinGecko was excellent. It gives you an example of exactly how the information will pass to my application when I make a request. Let’s say I want to get the current market prices of Bitcoin and Ether. I can input these parameters right in the browser. Be sure to separate the ids of coins only with a comma, and no spaces, or you will hit a problem.

Click execute.

CoinGecko spits out the response it will send when your application makes this same request.

Great, so now we know that bitcoin.usd will bring out 9627.02 as a number from this object. Check it out by going to the API, copying that data structure, pasting it in your console, and testing out how to get the information you need.

3. Import the dependencies in JavaScript

All you need is to run from the terminal:

You may want to double check this lands in your package.json file like so:

Nice and easy!

You only need the dependency to use the node.js wrapper, or else you can just hit the URL directly. (See the implementation below.)

4. Make your requests.

Lesson learned #1: If you need to get data for multiple coins, put them in an array when sending your fetch request to get the data.

Lesson learned #2: If the fetch request throws an error on historical queries, it may be because the data is only served from since the time CoinGecko started tracking that coin.

Lesson learned #3: Don’t forget that await functions (like this API query) must be conducted inside an async function. Here’s an implementation using the node.js wrapper inside a React class component:

Lesson learned #4: Running your requests in a JS file that is separate from your application can help avoid headaches. Just use that environment to practice how you’ll consume the data, and where you’ll send it. I spent half-a-day practicing requests in a coin-gecko-api.js file and rendering them in a simple react app just to get a feel for making the different requests. Time well-spent.

Here’s how an implementation of a historical request might look:

5. Do what you want with the data!

Now you’ve got the data all ready to go. Who dares wins!

Curious Weekly — writing inspired by curiosity.

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Grant Ferowich
Coinmonks

Writing code. "Everyone you meet is fighting a battle you know nothing about. Be kind." Curious Weekly - https://curiousweekly.com/.