@coinbase-sample/prime-sdk-ts

中文文档
0.10.1发布于 2026/06/02ISCTypeScriptESM + CJS近 30 天下载 7505

Deprecation notice: Development of this SDK has moved to the Coinbase GitHub organization. Use coinbase/prime-sdk-ts for the current source, issues, and releases

原始使用文档
文档目录23 节
  1. Overview
  2. License
  3. 🚀 Quick Start
  4. Installation
  5. Basic Usage (Recommended)
  6. Environment Setup
  7. 📚 Usage Examples
  8. List Portfolios
  9. Get Assets
  10. Create Order
  11. 🔧 Configuration Options
  12. Pagination Control
  13. Advanced Pagination Methods
  14. 📦 Alternative Import Patterns
  15. 🎯 For Bundle Size Optimization
  16. 📊 Bundle Size Comparison
  17. 🎨 Type-Only Imports
  18. 🤔 When to Use Which?
  19. 🛠️ Development
  20. Installation
  21. Build
  22. Run Examples
  23. 🧪 TypeScript Support

Coinbase Prime API TypeScript SDK

Deprecation notice: Development of this SDK has moved to the Coinbase GitHub organization. Use coinbase/prime-sdk-ts for the current source, issues, and releases. This coinbase-samples repository remains for historical reference; new work should target the Coinbase org repository.

npm version License

Overview

Welcome to the Coinbase Prime API TypeScript SDK. This TypeScript project provides a comprehensive, type-safe interface to the Coinbase Prime API with multiple consumption patterns optimized for different use cases.

License

The Prime Typescript SDK sample library is free and open source and released under the Apache License, Version 2.0.

The application and code are only available for demonstration purposes.

🚀 Quick Start

Installation

npm install @coinbase-sample/prime-sdk-ts

The SDK provides a modular client with lazy-loaded services for the best developer experience:

import { CoinbasePrimeClientWithServices } from '@coinbase-sample/prime-sdk-ts';

// Create client from environment variables
const client = CoinbasePrimeClientWithServices.fromEnv();

// Access services directly - no manual instantiation needed!
const portfolios = await client.portfolios.listPortfolios();
const orders = await client.orders.listPortfolioOrders({ portfolioId: 'your-id' });
const wallets = await client.wallets.listWallets({ portfolioId: 'your-id' });

Environment Setup

Copy env.example to .env and populate with your values:

cp env.example .env

Then edit .env with your actual credentials:

# API Credentials (JSON format)
PRIME_CREDENTIALS={"AccessKey":"your-access-key","SecretKey":"your-secret-key","Passphrase":"your-passphrase"}

# Required IDs
ENTITY_ID=your-entity-id
PORTFOLIO_ID=your-portfolio-id
WALLET_ID=your-wallet-id

📚 Usage Examples

List Portfolios

const portfolios = await client.portfolios.listPortfolios();
console.log(portfolios.portfolios);

Get Assets

const assets = await client.assets.listAssets({ 
  entityId: portfolioId 
});
console.log(assets.assets);

Create Order

import { OrderSide, OrderType } from '@coinbase-sample/prime-sdk-ts';

const order = await client.orders.createOrder({
  portfolioId: 'your-portfolio-id',
  productId: 'BTC-USD',
  side: OrderSide.BUY,
  type: OrderType.MARKET,
  baseQuantity: '0.001'
});
console.log(order.orderId);

See the example folder for more robust examples for many of the available services and endpoints.

🔧 Configuration Options

Pagination Control

The SDK provides powerful pagination controls at both the client and request level:

const client = CoinbasePrimeClientWithServices.fromEnv(undefined, {
  maxPages: 5,        // Max pages to fetch automatically
  maxItems: 1000,     // Max total items across all pages
  defaultLimit: 100   // Items per page
});

// Or per-request
const response = await client.transactions.listPortfolioTransactions(
  { portfolioId, limit: 50 },
  { maxPages: 10, maxItems: 500 }
);

Advanced Pagination Methods

All paginated responses include powerful methods for manual pagination control:

// Get the first page
const firstPage = await client.transactions.listPortfolioTransactions({ portfolioId });

// Check if there are more pages
if (firstPage.hasNext()) {
  console.log('More data available');
}

// Get the next page manually
const secondPage = await firstPage.next();

// Fetch ALL remaining pages and combine the data
const allTransactions = await firstPage.fetchAll(
  undefined,  // options
  (page, totalItems) => console.log(`Fetched page ${page}, total items: ${totalItems}`)
);

// Example: Manual pagination loop
let currentPage = firstPage;
while (currentPage.hasNext()) {
  console.log(`Processing ${currentPage.transactions.length} transactions`);
  currentPage = await currentPage.next();
}

Pagination Methods

MethodDescriptionReturns
hasNext()Check if more pages are availableboolean
next()Fetch the next pagePromise<Response | null>
fetchAll()Fetch all remaining pages and combine dataPromise<DataArray[]>
getNextCursor()Get the next page cursorstring | undefined

📦 Alternative Import Patterns

While the modular client (CoinbasePrimeClientWithServices) is recommended for most use cases, the SDK provides additional import patterns optimized for specific scenarios:

🎯 For Bundle Size Optimization

Manual Client (@coinbase-sample/prime-sdk-ts/manual)

When you need full control over service instantiation:

import { CoinbasePrimeClient, OrdersService, WalletsService } from '@coinbase-sample/prime-sdk-ts/manual';

const client = CoinbasePrimeClient.fromEnv();
const orders = new OrdersService(client);
const wallets = new WalletsService(client);

// Only the services you import are included in your bundle

Ultra-Minimal Client (@coinbase-sample/prime-sdk-ts/client)

For services-only patterns (97% smaller bundles):

import { CoinbasePrimeClient } from '@coinbase-sample/prime-sdk-ts/client';
import { OrdersService } from '@coinbase-sample/prime-sdk-ts/services';

const client = CoinbasePrimeClient.fromEnv();
const orders = new OrdersService(client);
// Total bundle: ~3kb (perfect for microservices)

📊 Bundle Size Comparison

Import PatternBundle SizeUse Case
Modular Client (recommended)~30kbBest developer experience
Manual Client~90kbFull control, all services available
Services + Client~6kbCustom implementations
Individual Service~3kbMicroservices, Lambda functions

🎨 Type-Only Imports

For shared libraries or type definitions:

import type { CreateOrderRequest, OrderSide } from '@coinbase-sample/prime-sdk-ts/types';
// 0kb runtime - perfect for shared type libraries

🤔 When to Use Which?

  • 🏆 Modular Client: Default choice - best DX, good performance
  • ⚡ Manual Client: Need all services, want explicit control
  • 🎯 Services-Only: Microservices, custom clients, minimal bundles
  • 📦 Individual Services: Lambda functions, single-purpose apps
  • 📝 Types-Only: Shared libraries, type definitions

🛠️ Development

Installation

git clone https://github.com/coinbase-samples/prime-sdk-ts.git
cd prime-sdk-ts
npm install

Build

npm run build

Run Examples

The SDK includes comprehensive examples in the example/ directory:

# Set up environment (copy env.example to .env first)
cp env.example .env
# Edit .env with your actual entityId, portfolioId, and walletId

# Run examples
node example/listPortfolios.js
node example/createOrder.js
node example/listWallets.js TRADING

🧪 TypeScript Support

The SDK is built with TypeScript and provides full type safety:

import { 
  CoinbasePrimeClientWithServices, 
  OrderSide, 
  OrderType,
  CreateOrderRequest 
} from '@coinbase-sample/prime-sdk-ts';

const client = CoinbasePrimeClientWithServices.fromEnv();

// Full IntelliSense and type checking
const request: CreateOrderRequest = {
  portfolioId: 'your-id',
  productId: 'BTC-USD',
  side: OrderSide.BUY,  // Enum with autocomplete
  type: OrderType.MARKET,
  baseQuantity: '0.001'
};

const order = await client.orders.createOrder(request);
// Response is fully typed - no manual type assertions needed

版本与发布记录

@coinbase-sample/prime-sdk-ts 共发布 21 个版本,首次发布于 2025/01/24;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.10.1latest2026/06/025 个月前
0.10.02026/05/185 个月前
0.9.02026/04/027 个月前
0.8.22026/02/118 个月前
0.8.12026/01/0710 个月前
0.8.02025/12/1310 个月前

关于 @coinbase-sample/prime-sdk-ts 的常见问题

@coinbase-sample/prime-sdk-ts 是什么?

Deprecation notice: Development of this SDK has moved to the Coinbase GitHub organization. Use coinbase/prime-sdk-ts for the current source, issues, and releases

如何安装 @coinbase-sample/prime-sdk-ts?

在项目目录运行 npm install @coinbase-sample/prime-sdk-ts,或使用 pnpm add / yarn add / bun add。

@coinbase-sample/prime-sdk-ts 支持 TypeScript 吗?

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

@coinbase-sample/prime-sdk-ts 支持 ESM 还是 CommonJS?

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

@coinbase-sample/prime-sdk-ts 可以商用吗?

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

@coinbase-sample/prime-sdk-ts 的下载量有多少?

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

@coinbase-sample/prime-sdk-ts 还在维护吗?

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