Coinbase 原始 API 类型Script SDK
折旧通知: 这个SDK的开发已经转移到了 GitHub组织启用 硬币基础/硬币 用于当前来源、问题和释放。 这个
coinbase-samples储存库仍供历史参考;新工作应针对库因巴克沃尔克储存库.
概览
欢迎使用 Coinbase Prime API TypeScript SDK. 此 TypeScript 项目提供了一个全面、 类型安全的接口 核心主 API 并优化了多种消费模式,以应对不同的使用案例.
许可证
这个 主打字符 SDK 样本库是免费和开源的,并在 Apache 许可证,2.0版本。 。 。 .
应用程序和代码仅供演示之用.
QQ 快速启动
安装
npm install @coinbase-sample/prime-sdk-ts
基本用途(建议)
SDK提供了一个 模块客户端 提供懒惰的服务,以获得最好的开发者经验:
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' });
环境设置
复制 env.example 改为 .env 并包含您的值 :
cp env.example .env
然后编辑 .env 以你的实际全权证书:
# 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
使用实例
列表组合
const portfolios = await client.portfolios.listPortfolios();
console.log(portfolios.portfolios);
获取资产
const assets = await client.assets.listAssets({
entityId: portfolioId
});
console.log(assets.assets);
创建顺序
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);
关于许多现有服务和终点的更有力实例,请参见示例文件夹.
QQ 配置选项
缓冲控制
SDK在客户端和请求层都提供了强大的Pagination控制:
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 }
);
高级植入法
所有分解反应都包括强有力的人工分解控制方法:
// 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();
}
接地方法
| 方法 | 说明 | 回返 |
|---|---|---|
hasNext() | 检查是否有更多页面可用 | boolean |
next() | 获取下一页 | Promise<Response | null> |
fetchAll() | 获取所有剩余页面并合并数据 | Promise<DataArray[]> |
getNextCursor() | 获取下一页光标 | string | undefined |
• 替代导入模式
虽然 模块客户端 (单位:千美元)CoinbasePrimeClientWithServices在大多数使用情况下,建议采用SDK,为特定情况提供最佳的额外进口模式:
QQ 为 Bundle 大小优化
手动客户端 (单位:千美元)@coinbase-sample/prime-sdk-ts/manual(中文(简体) )
当您需要完全控制服务即时:
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
超小型客户端 (单位:千美元)@coinbase-sample/prime-sdk-ts/client(中文(简体) )
对于只服务模式(97%的捆绑):
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)
QQ 套装大小比较
| 导入图案 | 套装大小 | 使用大小写 |
|---|---|---|
| 模块客户端 (建议) | ~30kb 时 | 最佳开发者经验 |
| 手动客户端 | ~90kb 时 | 全面控制,所有可用的服务 |
| 服务+客户端 | ~6kb 时 | 自定义执行 |
| 个人服务 | ~3kb 时 | 微服务,Lambda功能 |
QQ 只导入类型
对于共享库或类型定义:
import type { CreateOrderRequest, OrderSide } from '@coinbase-sample/prime-sdk-ts/types';
// 0kb runtime - perfect for shared type libraries
什么时候用哪一个?
- QQ 模块客户端: 默认选择 - 最佳 DX, 良好性能
- QQ 手动客户端:需要所有服务,需要明确的控制
- 仅服务:微服务,自定义客户端,最小捆绑
- 个人服务: Lambda 函数,单用途应用程序
- 仅限类型: 共享库,类型定义
发展
安装
git clone https://github.com/coinbase-samples/prime-sdk-ts.git
cd prime-sdk-ts
npm install
构建
npm run build
运行示例
SDK 将综合实例纳入 example/ 目录 :
# 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
QQ 类型脚本支持
SDK是用TypeScript来构建的,并提供了完整的类型安全:
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