@coinmarketcap/sdk

中文文档
5.0.1发布于 2026/10/30MITTypeScriptESM + CJS零依赖近 30 天下载 4651

Official CoinMarketCap Pro API TypeScript/JavaScript SDK

原始使用文档
文档目录18 节
  1. Installation
  2. Quick Start
  3. Usage
  4. Namespace API (Recommended)
  5. Bare Functions (Explicit Client)
  6. Global Mode (init)
  7. Configuration Options
  8. Public (Keyless) Mode
  9. Error Handling
  10. Error Types
  11. Throw on Error
  12. Automatic Retries
  13. Timeout
  14. Cancellation
  15. Available Endpoints
  16. Versioning
  17. Requirements
  18. License

@coinmarketcap/sdk

npm version node

The official TypeScript/JavaScript SDK for the CoinMarketCap Pro API.

Installation

npm install @coinmarketcap/sdk
# or
yarn add @coinmarketcap/sdk
# or
pnpm add @coinmarketcap/sdk

Quick Start

import { CoinMarketCap } from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ apiKey: process.env.CMC_PRO_API_KEY! });

// Use the namespace API — discover endpoints by category with autocomplete
const { data, error } = await cmc.api.cryptocurrency.quotesLatest({
  query: { id: "1,1027" }, // 1 = BTC, 1027 = ETH
});

if (data) {
  console.log(data);
}

Usage

All endpoints are grouped by category on cmc.api:

import { CoinMarketCap } from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ apiKey: "your-key" });

await cmc.api.cryptocurrency.quotesLatest({ query: { id: "1" } });
await cmc.api.cryptocurrency.listingsLatest({ query: { limit: 10 } });
await cmc.api.globalMetrics.quotesLatest({});
await cmc.api.exchange.info({ query: { id: "270" } });

Bare Functions (Explicit Client)

You can also import individual functions and pass the client explicitly:

import {
  CoinMarketCap,
  getV1CryptocurrencyQuotesLatest,
} from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ apiKey: "your-key" });

const { data } = await getV1CryptocurrencyQuotesLatest({
  client: cmc.client,
  query: { id: "1", convert: "USD" },
});

Global Mode (init)

For simple scripts, init() configures the global client so bare functions work without passing client:

import { init, getV1CryptocurrencyQuotesLatest } from "@coinmarketcap/sdk";

const cmc = init({ apiKey: "your-key" });

// Namespace works on the returned instance
await cmc.api.cryptocurrency.quotesLatest({ query: { id: "1" } });

// Bare functions also work (use the global client)
await getV1CryptocurrencyQuotesLatest({ query: { id: "1" } });

Configuration Options

const cmc = new CoinMarketCap({
  // Required for 'pro' mode
  apiKey: "your-api-key",

  // Optional
  environment: "pro", // 'pro' (default) or 'public'
  baseUrl: undefined, // Override base URL entirely
  timeout: 30000, // Request timeout in ms (default: 30s)
  maxRetries: 2, // Auto-retry count (default: 2)
  fetch: customFetch, // Custom fetch implementation
});

Public (Keyless) Mode

Use the public API without an API key for publicly available endpoints:

import { CoinMarketCap } from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ environment: "public" });

await cmc.api.cryptocurrency.listingsLatest({ query: { limit: 10 } });

Error Handling

The SDK provides typed error classes for common HTTP failures:

import {
  CoinMarketCap,
  CMCError,
  RateLimitError,
  AuthenticationError,
} from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ apiKey: "your-key" });

const { data, error } = await cmc.api.cryptocurrency.quotesLatest({
  query: { id: "1" },
});

if (error) {
  if (error instanceof RateLimitError) {
    console.log("Rate limited, retry after:", error.headers.get("Retry-After"));
  } else if (error instanceof AuthenticationError) {
    console.log("Invalid API key");
  } else if (error instanceof CMCError) {
    console.log(`API error ${error.status}:`, error.message);
  }
}

Error Types

Status CodeError Class
400BadRequestError
401AuthenticationError
403ForbiddenError
404NotFoundError
429RateLimitError
5xxInternalServerError
NetworkAPIConnectionError
TimeoutAPITimeoutError

Throw on Error

Alternatively, you can throw errors instead of returning them:

import { CoinMarketCap, RateLimitError } from "@coinmarketcap/sdk";

const cmc = new CoinMarketCap({ apiKey: "your-key" });

try {
  const { data } = await cmc.api.cryptocurrency.quotesLatest({
    query: { id: "1" },
    throwOnError: true,
  });
  console.log(data);
} catch (err) {
  if (err instanceof RateLimitError) {
    // handle rate limit
  }
}

Automatic Retries

Requests that fail with retryable status codes are automatically retried with exponential backoff:

  • Retryable: 408, 409, 429, 500, 502, 503, 504, network errors, and timeouts
  • Default: 2 retries with 500ms initial delay, up to 8s max
  • 429 responses: Respects the Retry-After header, capped at the max delay (8s) so a large value can't stall the call
  • Body-safe: Request bodies are replayed correctly on each retry (POST endpoints included)

Disable retries:

const cmc = new CoinMarketCap({
  apiKey: "your-key",
  maxRetries: 0,
});

Timeout

Requests time out after 30 seconds by default:

const cmc = new CoinMarketCap({
  apiKey: "your-key",
  timeout: 10_000, // 10 seconds
});

A timeout raises APITimeoutError.

Cancellation

Pass an AbortSignal to cancel an in-flight request. Caller cancellation is honoured immediately and is never retried:

const controller = new AbortController();
const promise = cmc.api.cryptocurrency.quotesLatest({
  query: { symbol: "BTC" },
  signal: controller.signal,
});
controller.abort(); // rejects with an AbortError

Available Endpoints

All endpoints are available via cmc.api.<category>.<method>(). See api.md for the full reference.

Categories include:

  • cmc.api.cryptocurrency — Quotes, listings, market pairs, OHLCV, categories
  • cmc.api.exchange — Exchange info, listings, market pairs
  • cmc.api.globalMetrics — Global stats, fear & greed index
  • cmc.api.content — News, posts
  • cmc.api.community — Trending tokens, topics
  • cmc.api.token — DEX token data, pools, trades
  • cmc.api.derivatives — Derivatives market data
  • cmc.api.cmcIndex — CMC 20/100 index

You can also import bare functions directly:

import {
  getV1CryptocurrencyListingsLatest,
  getV1CryptocurrencyInfo,
  getV1CryptocurrencyMap,
  // ... 100+ more endpoints
} from "@coinmarketcap/sdk";

Versioning

The SDK follows Semantic Versioning. A major version removes or renames methods, or changes return types or response fields in a way that can break existing code. A minor version adds endpoints or fields. A patch version fixes bugs.

The TypeScript (@coinmarketcap/sdk) and Python (coinmarketcap-sdk) SDKs are released together with the same version number. The SDK version is separate from the API's endpoint versions (/v1, /v2, /v3). Each CHANGELOG entry names the OpenAPI spec the release was built from and lists its breaking changes.

Requirements

  • Node.js >= 18
  • TypeScript >= 5.0 (for TypeScript users)

License

MIT

版本与发布记录

@coinmarketcap/sdk 共发布 6 个版本,首次发布于 2026/08/07;过去 12 个月发布了 6 个版本。

版本发布日期距今
5.0.1latest2026/10/3011 天前
5.0.02026/10/2318 天前
4.0.32026/09/181 个月前
4.0.22026/08/112 个月前
4.0.12026/08/073 个月前
4.0.02026/08/063 个月前

关于 @coinmarketcap/sdk 的常见问题

@coinmarketcap/sdk 是什么?

Official CoinMarketCap Pro API TypeScript/JavaScript SDK

如何安装 @coinmarketcap/sdk?

在项目目录运行 npm install @coinmarketcap/sdk,或使用 pnpm add / yarn add / bun add。

@coinmarketcap/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@coinmarketcap/sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@coinmarketcap/sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@coinmarketcap/sdk 可以商用吗?

@coinmarketcap/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@coinmarketcap/sdk 的下载量有多少?

根据 npm 官方下载统计,@coinmarketcap/sdk 在 2026/10/11 – 2026/11/09 这 30 天内被下载 4,651 次,日均约 155 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@coinmarketcap/sdk 还在维护吗?

最新版本 5.0.1 发布于 2026/10/30(11 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/08/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。