@nirholas/crypto-market-data

Comprehensive cryptocurrency market data service with built-in caching, rate limiting, and Edge Runtime compatibility.
Features
- 🚀 Edge Runtime Compatible - Works in Cloudflare Workers, Vercel Edge, Node.js, and browsers
- 📊 Multiple Data Sources - CoinGecko, DeFiLlama, and Alternative.me (Fear & Greed)
- ⚡ Smart Caching - Stale-while-revalidate pattern with configurable TTLs
- 🛡️ Rate Limiting - Built-in protection for CoinGecko's free tier (25 req/min)
- 📦 Zero Dependencies - Uses native
fetch only
- 🔷 Full TypeScript - Complete type definitions included
- 🔄 Backwards Compatible - Both class-based and function exports
Installation
npm install @nirholas/crypto-market-data
# or
yarn add @nirholas/crypto-market-data
# or
pnpm add @nirholas/crypto-market-data
Quick Start
import { MarketDataClient } from '@nirholas/crypto-market-data';
const client = new MarketDataClient();
// Get market overview
const overview = await client.getMarketOverview();
console.log(`BTC: $${overview.btcPrice}`);
console.log(`ETH: $${overview.ethPrice}`);
console.log(`Fear & Greed: ${overview.fearGreed?.value} (${overview.fearGreed?.value_classification})`);
// Get top coins
const coins = await client.getTopCoins(10);
coins.forEach(coin => {
console.log(`${coin.name}: $${coin.current_price} (${coin.price_change_percentage_24h}%)`);
});
API Reference
Initialization
import { MarketDataClient, type MarketDataConfig } from '@nirholas/crypto-market-data';
const config: MarketDataConfig = {
rateLimitWindow: 60000, // Rate limit window in ms (default: 60000)
maxRequestsPerWindow: 25, // Max requests per window (default: 25)
timeout: 10000, // Request timeout in ms (default: 10000)
userAgent: 'MyApp/1.0', // Custom User-Agent
};
const client = new MarketDataClient(config);
Available Methods
Prices & Market Data
| Method | Description |
|---|
getSimplePrices() | Get BTC, ETH, SOL prices |
getTopCoins(limit?) | Get top coins by market cap |
getTrending() | Get trending coins |
getGlobalMarketData() | Get global market statistics |
getCoinDetails(coinId) | Get detailed coin information |
getMarketOverview() | Get combined market overview |
Fear & Greed Index
| Method | Description |
|---|
getFearGreedIndex() | Get current Fear & Greed Index |
DeFi (DeFiLlama)
| Method | Description |
|---|
getTopProtocols(limit?) | Get top DeFi protocols by TVL |
getTopChains(limit?) | Get top chains by TVL |
getGlobalDeFiData() | Get global DeFi statistics |
Historical Data
| Method | Description |
|---|
getHistoricalPrices(coinId, days, interval?) | Get historical price data |
getOHLC(coinId, days) | Get OHLC candlestick data |
getHistoricalPrice(coinId, date) | Get price at specific date |
Exchanges & Tickers
| Method | Description |
|---|
getCoinTickers(coinId, page?) | Get trading pairs for a coin |
getExchanges(perPage?, page?) | Get list of exchanges |
getExchangeDetails(exchangeId) | Get exchange details |
Categories
| Method | Description |
|---|
getCategories() | Get all categories |
getCategoryCoins(categoryId, perPage?, page?) | Get coins in a category |
Search & Compare
| Method | Description |
|---|
searchCoins(query) | Search coins, exchanges, NFTs |
compareCoins(coinIds) | Compare multiple coins |
getCoinsList() | Get all coins for autocomplete |
Social & Developer
| Method | Description |
|---|
getCoinDeveloperData(coinId) | Get GitHub/developer stats |
getCoinCommunityData(coinId) | Get social/community stats |
Derivatives
| Method | Description |
|---|
getDerivativesTickers() | Get derivatives market tickers |
Utility Functions
import {
formatPrice,
formatNumber,
formatPercent,
getFearGreedColor,
getFearGreedBgColor
} from '@nirholas/crypto-market-data';
formatPrice(45123.45); // "$45,123"
formatPrice(0.00001234); // "$0.0000"
formatNumber(1234567890); // "1.23B"
formatPercent(5.67); // "+5.67%"
formatPercent(-3.21); // "-3.21%"
// Tailwind CSS colors for Fear & Greed Index
getFearGreedColor(25); // "text-red-500" (Extreme Fear)
getFearGreedColor(75); // "text-lime-500" (Greed)
getFearGreedBgColor(50); // "bg-yellow-500" (Neutral)
Legacy Function Exports