How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (2024)

Curious Weekly — writing inspired by curiosity.

Are you looking to buy a CoinGecko Annual API subscription here? Use referral code CGGRANT to get $500 off an Annual API subscription purchase.

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.

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (3)

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.

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (4)

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.

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (5)

Click execute.

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

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (6)

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.

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (7)

3. Import the dependencies in JavaScript

All you need is to run from the terminal:

npm install 'coingecko-api'

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

"coingecko-api": "^1.0.10"

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:

async componentDidMount() {await this.CoinGeckoClient.coins.markets(
{ vs_currency: "usd",
order: "market_cap_desc",
per_page: 100,
page: 1,
sparkline: false,
price_change_percentage: "24h"})
.then(data => console.log(data))}

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:

const getHistoricalPrice = async event => {let currency1API= "bitcoin";
let indexDate="01-01-2020";
let
string = "https://api.coingecko.com/api/v3/coins/" + currency1API +"/history?date="+indexDate+"&localization=false";
await fetch(string)
.then(resp => resp.json())
.then(data => console.log(data.market_data.current_price.usd))}
//=> 7195.15

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

How to Use CoinGecko’s Free API to Get Digital Asset Data: Just the Basics (2024)
Top Articles
Latest Posts
Article information

Author: Tuan Roob DDS

Last Updated:

Views: 5946

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Tuan Roob DDS

Birthday: 1999-11-20

Address: Suite 592 642 Pfannerstill Island, South Keila, LA 74970-3076

Phone: +9617721773649

Job: Marketing Producer

Hobby: Skydiving, Flag Football, Knitting, Running, Lego building, Hunting, Juggling

Introduction: My name is Tuan Roob DDS, I am a friendly, good, energetic, faithful, fantastic, gentle, enchanting person who loves writing and wants to share my knowledge and understanding with you.