This article will guide you through fetching cryptocurrency market data using the GOATSDK and its CoinGecko plugin. Whether you're looking for historical coin data, OHLC candlestick data, trending coins, or top gainers and losers, this guide provides step-by-step instructions for each feature. The GOATSDK simplifies interactions with the CoinGecko API, making it easy to integrate these functionalities into your projects.
Before getting started, ensure you have the following ready:
-
CoinGecko Pro API Key:
- A Pro API key is required for advanced features like top gainers and losers.
- Obtain one from the CoinGecko API Pricing page.
- The free API plan is sufficient for basic features like historical data and OHLC data.
-
Node.js:
- Install Node.js (v16 or later) from the official Node.js website.
-
TypeScript:
- Install TypeScript globally using:
npm install -g typescript
- Install TypeScript globally using:
-
Dependencies:
- Install the required dependencies using:
npm install @goat-sdk/core @goat-sdk/plugin-coingecko dotenv inquirer npm install --save-dev typescript @types/node
- Install the required dependencies using:
-
Environment Variables:
- Create a
.envfile in the root directory and add your CoinGecko API key:COINGECKO_API_KEY=your_pro_api_key_here
- Create a
-
Initialize the Project:
Create a new project directory and initialize it:mkdir goat-gecko-tool cd goat-gecko-tool npm init -y -
Install Dependencies:
Install the required libraries:npm install @goat-sdk/core @goat-sdk/plugin-coingecko dotenv inquirer npm install --save-dev typescript @types/node
-
Set Up TypeScript:
Initialize TypeScript and configure thetsconfig.jsonfile:npx tsc --init
Update the
tsconfig.jsonfile to include:{ "compilerOptions": { "target": "ES2020", "module": "ESNext", "moduleResolution": "Node", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "outDir": "./dist" }, "include": ["src/**/*"] } -
Set Up Environment Variables:
Create a.envfile in the root directory and add your CoinGecko API key:COINGECKO_API_KEY=your_pro_api_key_here -
Compile the Code:
Use the TypeScript compiler to transpile the code into JavaScript:npx tsc
To fetch historical data, use the GetHistoricalData tool exposed by the SDK. You'll need to provide:
- The coin's ID (e.g.,
"bitcoin") - The date you want the data for (in
DD-MM-YYYYformat) - Optionally, whether you want the data localized (e.g., translated descriptions)
const tools = await getTools({
wallet,
plugins: [
coingecko({
apiKey: process.env.COINGECKO_API_KEY || "",
isPro: true,
}),
],
});
const getHistoricalData = tools.find(t => t.name === "coingecko_get_historical_data");
const result = await getHistoricalData.execute({
id: "bitcoin",
date: "30-12-2021",
localization: false,
});
console.log(result);{
"id": "bitcoin",
"symbol": "btc",
"name": "Bitcoin",
"market_data": {
"current_price": { "usd": 47000 },
"market_cap": { "usd": 880000000000 },
"total_volume": { "usd": 35000000000 }
}
}The response includes the price, market cap, total volume, and coin metadata for the given date. This allows you to programmatically analyze how a coin was performing at a specific point in time.
To fetch OHLC (Open, High, Low, Close) data, use the GetOHLCData tool. You'll need to provide:
- The coin's ID (e.g.,
"bitcoin") - The currency to compare against (e.g.,
"usd") - The number of days for which you want the data (e.g.,
7)
const getOHLCData = tools.find(t => t.name === "coingecko_get_ohlc_data");
const result = await getOHLCData.execute({
id: "bitcoin",
vsCurrency: "usd",
days: 7,
});
console.log(result);[
[1638316800000, 57000, 58000, 56000, 57500],
[1638403200000, 57500, 59000, 57000, 58000],
[1638489600000, 58000, 58500, 57500, 58000]
]Each array represents a candlestick with the following values:
- Timestamp
- Open price
- High price
- Low price
- Close price
This data is useful for visualizing price movements over time.
To fetch trending coins, use the GetTrendingCoins tool. This tool provides a list of coins that are currently popular based on CoinGecko's trending algorithm.
const getTrendingCoins = tools.find(t => t.name === "coingecko_get_trending_coins");
const result = await getTrendingCoins.execute({});
console.log(result);{
"coins": [
{ "id": "bitcoin", "symbol": "btc", "name": "Bitcoin" },
{ "id": "ethereum", "symbol": "eth", "name": "Ethereum" },
{ "id": "solana", "symbol": "sol", "name": "Solana" }
]
}The response includes a list of trending coins, which can be useful for identifying market interest and emerging trends.
To fetch the top gainers and losers, use the GetTopGainersLosers tool. You'll need to provide:
- The currency to compare against (e.g.,
"usd") - The number of results per page (e.g.,
10) - The page number (e.g.,
1)
const getTopGainersLosers = tools.find(t => t.name === "coingecko_get_top_gainers_losers");
const result = await getTopGainersLosers.execute({
vsCurrency: "usd",
perPage: 10,
page: 1,
});
console.log(result);{
"gainers": [
{ "id": "solana", "symbol": "sol", "price_change_percentage_24h": 15.2 },
{ "id": "avalanche", "symbol": "avax", "price_change_percentage_24h": 12.8 }
],
"losers": [
{ "id": "dogecoin", "symbol": "doge", "price_change_percentage_24h": -8.5 },
{ "id": "shiba-inu", "symbol": "shib", "price_change_percentage_24h": -7.3 }
]
}The response includes a list of cryptocurrencies with the highest and lowest price changes over a specific period. This data is useful for identifying market trends and opportunities.
To fetch coin categories, use the GetCoinCategories tool. This tool provides a list of cryptocurrency categories and their market data.
const getCoinCategories = tools.find(t => t.name === "coingecko_get_coin_categories");
const result = await getCoinCategories.execute({});
console.log(result);[
{ "category": "DeFi", "market_cap": 120000000000, "volume_24h": 5000000000 },
{ "category": "NFT", "market_cap": 80000000000, "volume_24h": 3000000000 }
]The response includes categories like DeFi, NFT, and others, along with their market cap and 24-hour trading volume.
This guide demonstrates how to fetch cryptocurrency market data using the GOATSDK and CoinGecko plugin. By leveraging tools like GetHistoricalData, GetOHLCData, GetTrendingCoins, GetTopGainersLosers, and GetCoinCategories, you can easily integrate CoinGecko's extensive API capabilities into your projects. The modular architecture of the GOATSDK ensures scalability and flexibility, allowing you to add more tools or customize the functionality as needed. For more information, refer to the CoinGecko API Documentation and the GOATSDK Documentation.
If you find this project helpful, consider supporting it by donating cryptocurrency. Your support helps keep the project alive and maintained!
- Bitcoin (BTC):
34rqfUFenX2EMvDfpn7phgiKdHbxTJ5Wuw - Ethereum (ETH):
0x0bB11fD9a3B8EfBd899325c2EA574e28E6E87cB2 - USDT (ERC-20):
0x679c5733F4109283B46158AaD3a2C8981425c951
Thank you for your support! 🙏