The cmc-api is an generic api wrapper for CoinMarketCap. Supports endpoints cryptocurrency, exchanges (CEX), decentralized exchange (DEX), global metrics, community content and trends, tools and others.
Important
This library is based on the Official RestFul Json Api of CoinMarketCap. Response data may change at any time, currently the sandbox and production responses may differ. If you are using TypeScript, consider using your own generics type implementation in case there are any differences in response.
CoinMarketCap Note: Making HTTP requests on the client side with Javascript is currently prohibited through CORS configuration. This is to protect your API Key which should not be visible to users of your application so your API Key is not stolen. Secure your API Key by routing calls through your own backend service.
Installing with npm:
npm install cmc-api
Installing with Yarn:
yarn add cmc-api
Installing with pnpm:
pnpm add cmc-api
import CoinMarketCapApi from 'cmc-api';
// or
import { CoinMarketCapApi } from 'cmc-api';
const CoinMarketCapApi = require('cmc-api');
// or
const { CoinMarketCapApi } = require('cmc-api');
import CoinMarketCapApi from 'cmc-api';
const cmc = CoinMarketCapApi.sandbox();
import CoinMarketCapApi from 'cmc-api';
const CMC_APIKEY = 'YOUR_COINMARKETCAP_APIKEY'; // process.env.COINMARKETCAP_APIKEY
const cmc = new CoinMarketCapApi(CMC_APIKEY);
const crypto = await cmc.crypto.list('active', 'id', 10, 1, ['BTC', 'ETH']);
const btc = crypto.find((crypto): boolean => crypto.symbol === 'BTC');
const eth = crypto.find((crypto): boolean => crypto.symbol === 'ETH');
const crypto = await cmc.crypto.metadata<'BTC' | 'ETH'>({ symbol: ['BTC', 'ETH'] });
console.log({ btc: crypto.BTC.at(0), eth: crypto.ETH.at(1) });
const crypto = await cmc.crypto.metadata<'1' | '1027'>({ id: [1, 1027] });
console.log({ btc: crypto[1], eth: crypto[1027] });
const crypto = await cmc.crypto.metadata<'1' | '1027'>({ slug: ['bitcoin', 'ethereum'] });
console.log({ btc: crypto[1], eth: crypto[1027] });
const crypto = await cmc.crypto.metadata<'1027'>({ contract: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' });
console.log(crypto[1027]); // Ethereum
const crypto = await cmc.crypto.listing<'USD' | 'EUR'>(10, 1, 'all', 'market_cap', 'desc', 'all', undefined, ['USD', 'EUR']);
console.log(crypto.at(0), crypto.at(0).quote.USD, crypto.at(0).quote.EUR);
const crypto = await cmc.crypto.listingNew<'BTC' | 'ETH'>(10, 1, 'desc', ['BTC', 'ETH']);
console.log(crypto, crypto.at(0).quote.BTC, crypto.at(0).quote.ETH);
const date = new Date()
const crypto = await cmc.crypto.listingHistory<'BTC' | 'ETH' | 'USD'>(date, 10, 1, 'cmc_rank', 'desc', 'all', ['BTC', 'ETH', 'USD']);
console.log(crypto, crypto.at(0).quote.BTC, crypto.at(0).quote.ETH, crypto.at(0).quote.USD);
const quotes = await cmc.crypto.quotes<'bitcoin' | 'ethereum', 'USD' | 'EUR'>({ slug: ['bitcoin', 'ethereum'] }, ['USD', 'EUR']);
console.log({ btc: quotes.bitcoin, eth: quotes.ethereum, btcUsd: quotes.bitcoin.quote.USD, ethEur: quotes.ethereum.quote.EUR });
const start = new Date('2024-12-01T00:00:00Z');
const end = new Date('2025-01-01T00:00:00Z');
const quotesHistory = await cmc.crypto.quotesHistory<'1' | '1027', 'USD' | 'EUR'>({ id: [1, 1027] }, start, end);
const btc = quotesHistory['1'];
const eth = quotesHistory['1027'];
console.log({ btcQuotes: btc.quotes, ethQuotes: eth.quotes });
console.log({ btcQuoteFirst: btc.quotes?.at(0)?.quote, ethQuoteFirst: eth.quotes?.at(0)?.quote });
const ethereum = await cmc.crypto.marketPairs<'USD' | 'BTC'>({ slug: 'ethereum' }, { symbol: ['USD', 'BTC'] });
console.log({ ethMarketPairs: ethereum.market_pairs });
const ohlcv = await cmc.crypto.ohlcv<'1' | '1027', 'USD' | 'EUR'>({ id: [1, 1027] }, ['USD', 'EUR']);
const btcOhlcv = ohlcv['1'];
const ethOhlcv = ohlcv['1027'];
console.log({ btcOhlcv, ethOhlcv });
console.log({ btcOhlcvUsd: btcOhlcv.quote.USD, ethOhlcvEur: ethOhlcv.quote.EUR });
const start = new Date('2024-12-01T00:00:00Z');
const end = new Date('2025-01-01T00:00:00Z');
const ohlcv = await cmc.crypto.ohlcvHistory<'1' | '1027', 'USD'>({ id: [1, 1027] }, start, end, 'daily');
const btcOhlcv = ohlcv['1'];
const ethOhlcv = ohlcv['1027'];
console.log({ btcOhlcv, ethOhlcv });
console.log({ btcOhlcvHistory: btcOhlcv.quotes, ethOhlcvHistory: ethOhlcv.quotes });
const performance = await cmc.crypto.performance<'bitcoin' | 'ethereum', 'EUR' | 'USD'>({ slug: ['bitcoin', 'ethereum'] }, '24h', ['EUR', 'USD']);
console.log({ btcPerformance: performance.bitcoin, ethPerformance: performance.ethereum });
console.log({ btcPerformanceQuote: performance.bitcoin.quote.USD, ethPerformanceQuote: performance.ethereum.quote.EUR });
import type { CryptoCategoryResponse } from 'cmc-api';
type Category = Pick<CryptoCategoryResponse, 'title'>;
const categories = await cmc.crypto.categories<Category[]>(10, 1);
for (const category of categories) console.log(category.title);
const categoryId = '644620a2b124ea0434395dd1';
const zkSyncCategory = await cmc.crypto.category<'USD' | 'EUR'>(categoryId, 10, 1, ['USD', 'EUR']);
console.log(zkSyncCategory.market_cap);
const eth = zkSyncCategory.coins.filter((coin): boolean => coin.slug === 'ethereum')?.at(0);
console.log(eth.quote.USD);
import type { CryptoAirdropsResponse } from 'cmc-api';
type Airdrops = CryptoAirdropsResponse;
const airdrops = await cmc.crypto.airdrops<Airdrops>();
for (const airdrop of airdrops) console.log(airdrop.project_name);
const airdropId = 'bqdxotizdao';
const airdrop = await cmc.crypto.airdrop(airdropId);
console.log(airdrop);
console.log(airdrop.coin);
import type { CryptoTrendingResponse } from 'cmc-api';
type QuoteKey = 'USD' | 'EUR';
type Trending = CryptoTrendingResponse<QuoteKey>;
const trendings = await cmc.crypto.trending<QuoteKey, Trending[]>('24h', 10, 1, ['EUR', 'USD']);
for (const trending of trendings) {
console.log(trending.name, trending.symbol, trending.slug);
console.log(trending.quote.EUR, trending.quote.USD);
}
import type { CryptoTrendingResponse } from 'cmc-api';
type QuoteKey = 'USD' | 'EUR';
type Trending = CryptoTrendingResponse<QuoteKey>;
const trendings = await cmc.crypto.mostVisited<QuoteKey, Trending[]>('24h', 10, 1, ['EUR', 'USD']);
for (const trending of trendings) {
console.log(trending.name, trending.symbol, trending.slug);
console.log(trending.quote.EUR, trending.quote.USD);
}
get the top 10 gainers and losers for the last 24 hours and log the name, symbol, and quotes for EUR and USD
import type { CryptoTrendingResponse } from 'cmc-api';
type QuoteKey = 'USD' | 'EUR';
type Trending = CryptoTrendingResponse<QuoteKey>;
const trendings = await cmc.crypto.gainersLosers<QuoteKey, Trending[]>('24h', 10, 1, ['EUR', 'USD']);
for (const trending of trendings) {
console.log(trending.name, trending.symbol, trending.slug);
console.log(trending.quote.EUR, trending.quote.USD);
}
import type { CexIdMapResponses } from "cmc-api";
const exchanges = await cmc.cex.list<CexIdMapResponses>();
for (const exchange of exchanges) {
console.log(exchange.id, exchange.name, exchange.is_active);
}
import type { CexIdMapResponse } from "cmc-api";
const exchanges = await cmc.cex.list<CexIdMapResponse<"binance">>("active", { cexSlug: "binance" });
console.log(exchanges.binance);
const metadata = await cmc.cex.metadata<"3673">({ id: 3673 });
console.log(metadata["3673"]);
const metadata = await cmc.cex.metadata<"binance" | "okx">({ slug: ["binance", "okx"] });
console.log(metadata.binance);
console.log(metadata.okx);
const assets = await cmc.cex.assets<"3673">(3673);
for (const asset of assets["3673"]) {
console.log(asset.balance, asset.wallet_address);
}
const listings = await cmc.cex.listing<"EUR" | "USD">(10, 1, "all", "all", "exchange_score", "desc", ["EUR", "USD"]);
for (const listing of listings) {
console.log(listing.id, listing.name, listing.quote.EUR.volume_24h, listing.quote.USD.volume_24h);
}
const marketPairs = await cmc.cex.marketPairs<"3673">({ id: 3673 });
console.log(marketPairs["3673"]);
const marketPairs = await cmc.cex.marketPairs<"binance">({ slug: "binance" });
console.log(marketPairs.binance);
const quotes = await cmc.cex.quotes<"3673", "BTC" | "ETH">({ id: 3673 }, ["BTC", "ETH"]);
console.log(quotes["3673"].quote.BTC.volume_24h, quotes["3673"].quote.ETH.volume_24h);
const quotes = await cmc.cex.quotes<"binance", "BTC" | "ETH">({ slug: "binance" }, ["BTC", "ETH"]);
console.log(quotes.binance.quote.BTC.volume_24h, quotes.binance.quote.ETH.volume_24h);
const start = new Date("2024-12-01T00:00:00Z");
const end = new Date("2025-01-01T00:00:00Z");
const quotesHistorical = await cmc.cex.quotesHistory<"binance", "BTC" | "ETH">({ slug: "binance" }, start, end, 10, "4h", ["BTC", "ETH"]);
for (const binanceQuote of quotesHistorical.binance.quotes) {
console.log(
binanceQuote.timestamp,
binanceQuote.num_market_pairs,
binanceQuote.quote.BTC.volume_24h,
binanceQuote.quote.ETH.volume_24h,
);
}
(soon)
(soon)
(soon)
(soon)
See Releases for more information what has changed recently.
Contributions of any kind are welcome! See here
Licensed under the terms of the MIT license.