Skip to content

rollendxavier/goat-sdk-coingecko

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Fetch Crypto Market Data Using GOATSDK

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.


Prerequisites

Before getting started, ensure you have the following ready:

  1. 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.
  2. Node.js:

  3. TypeScript:

    • Install TypeScript globally using:
      npm install -g typescript
  4. Dependencies:

    • Install the required dependencies using:
      npm install @goat-sdk/core @goat-sdk/plugin-coingecko dotenv inquirer
      npm install --save-dev typescript @types/node
  5. Environment Variables:

    • Create a .env file in the root directory and add your CoinGecko API key:
      COINGECKO_API_KEY=your_pro_api_key_here
      

Setting up the Project Environment

  1. Initialize the Project:
    Create a new project directory and initialize it:

    mkdir goat-gecko-tool
    cd goat-gecko-tool
    npm init -y
  2. Install Dependencies:
    Install the required libraries:

    npm install @goat-sdk/core @goat-sdk/plugin-coingecko dotenv inquirer
    npm install --save-dev typescript @types/node
  3. Set Up TypeScript:
    Initialize TypeScript and configure the tsconfig.json file:

    npx tsc --init

    Update the tsconfig.json file to include:

    {
        "compilerOptions": {
            "target": "ES2020",
            "module": "ESNext",
            "moduleResolution": "Node",
            "strict": true,
            "esModuleInterop": true,
            "skipLibCheck": true,
            "outDir": "./dist"
        },
        "include": ["src/**/*"]
    }
  4. Set Up Environment Variables:
    Create a .env file in the root directory and add your CoinGecko API key:

    COINGECKO_API_KEY=your_pro_api_key_here
    
  5. Compile the Code:
    Use the TypeScript compiler to transpile the code into JavaScript:

    npx tsc

How to Get Historical Coin Data

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-YYYY format)
  • Optionally, whether you want the data localized (e.g., translated descriptions)

Example

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);

Sample Output

{
  "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.


How to Get OHLC Candlestick Data

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)

Example

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);

Sample Output

[
  [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.


How to Get Trending Coins

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.

Example

const getTrendingCoins = tools.find(t => t.name === "coingecko_get_trending_coins");

const result = await getTrendingCoins.execute({});

console.log(result);

Sample Output

{
  "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.


How to Get Top Gainers and Losers

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)

Example

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);

Sample Output

{
  "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.


How to Get Coin Categories

To fetch coin categories, use the GetCoinCategories tool. This tool provides a list of cryptocurrency categories and their market data.

Example

const getCoinCategories = tools.find(t => t.name === "coingecko_get_coin_categories");

const result = await getCoinCategories.execute({});

console.log(result);

Sample Output

[
  { "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.


Summary and Conclusion

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.

Support the Project

If you find this project helpful, consider supporting it by donating cryptocurrency. Your support helps keep the project alive and maintained!

Donation Wallets

  • Bitcoin (BTC): 34rqfUFenX2EMvDfpn7phgiKdHbxTJ5Wuw
  • Ethereum (ETH): 0x0bB11fD9a3B8EfBd899325c2EA574e28E6E87cB2
  • USDT (ERC-20): 0x679c5733F4109283B46158AaD3a2C8981425c951

Thank you for your support! 🙏

Releases

Packages

Contributors

Languages