@coinbase/coinbase-sdk

中文文档
0.25.0发布于 2025/06/05ISCTypeScriptCommonJS构建来源已验证近 30 天下载 3.5万

Coinbase Platform SDK

原始使用文档
文档目录10 节
  1. Documentation
  2. Requirements
  3. Installation
  4. Usage
  5. Initialization
  6. Wallets, Addresses, and Transfers
  7. Gasless USDC Transfers
  8. Trading Funds
  9. Re-Instantiating Wallets
  10. Acknowledgments

Coinbase Node.js SDK

npm version npm weekly downloads

The Coinbase Node.js SDK enables the simple integration of crypto into your app. By calling Coinbase's Platform APIs, the SDK allows you to provision crypto wallets, send crypto into/out of those wallets, track wallet balances, and trade crypto from one asset into another.

The SDK supports various verbs on Developer-custodied Wallets across multiple networks, as documented here.

Note: As the SDK provides new capabilities and improves the developer experience, updates may occasionally include breaking changes. These will be documented in the CHANGELOG.md file.

Documentation

Requirements

The Coinbase server-side SDK requires Node.js version 18 or higher and npm version 9.7.2 or higher. To view your currently installed versions of Node.js, run the following from the command-line:

node -v
npm -v

We recommend installing and managing Node.js and npm versions with nvm. See Installing and Updating in the nvm README for instructions on how to install nvm.

Once nvm has been installed, you can install and use the latest versions of Node.js and npm by running the following commands:

nvm install node # "node" is an alias for the latest version
nvm use node

Installation

Optional: Initialize the npm

This command initializes a new npm project with default settings and configures it to use ES modules by setting the type field to "module" in the package.json file.

npm init -y; npm pkg set type="module"

You can import the SDK as follows

npm install @coinbase/coinbase-sdk

or

yarn add @coinbase/coinbase-sdk

Usage

Initialization

You can import the SDK as follows:

CommonJs:

const { Coinbase, Wallet } = require("@coinbase/coinbase-sdk");

ES modules:

import { Coinbase, Wallet } from "@coinbase/coinbase-sdk";

To start, create a CDP API Key. Then, initialize the Platform SDK by passing your API Key name and API Key's private key via the Coinbase constructor:

const apiKeyName = "Copy your API Key name here.";

const privateKey = "Copy your API Key's private key here.";

Coinbase.configure({ apiKeyName: apiKeyName, privateKey: privateKey });

If you are using a CDP Server-Signer to manage your private keys, enable it with the constuctor option:

Coinbase.configure({ apiKeyName: apiKeyName, privateKey: apiKeyPrivateKey, useServerSigner: true })

Another way to initialize the SDK is by sourcing the API key from the json file that contains your API key, downloaded from CDP portal.

Coinbase.configureFromJson({ filePath: "path/to/your/api-key.json" });

This will allow you to authenticate with the Platform APIs.

CommonJs:

const { Coinbase, Wallet } = require("@coinbase/coinbase-sdk");
Coinbase.configureFromJson("path/to/your/api-key.json");

// List all Wallets for the CDP Project.
Wallet.listWallets().then(resp => {
  console.log(resp.data);
});

Or using ES modules and async/await:

import { Coinbase, Wallet } from "@coinbase/coinbase-sdk";
Coinbase.configureFromJson("path/to/your/api-key.json");

// List all Wallets for the CDP Project.
const resp = await Wallet.listWallets();
console.log(resp.data);

Wallets, Addresses, and Transfers

Now, create a Wallet which will default to the Base Sepolia testnet network (if not specified).

// Create a Wallet with one Address by default.
const wallet = await Wallet.create();

Next, view the default Address of your Wallet. You will need this default Address in order to fund the Wallet for your first Transfer.

// A Wallet has a default Address.
const address = await wallet.getDefaultAddress();
console.log(`Address: ${address}`);

Wallets do not have funds on them to start. In order to fund the Address, you will need to send funds to the Wallet you generated above. If you don't have testnet funds, get funds from a faucet.

For development purposes, we provide a faucet method to fund your address with ETH on Base Sepolia testnet. We allow one faucet claim per address in a 24 hour window.

// Create a faucet request that returns you a Faucet transaction that can be used to track the tx hash.
const faucetTransaction = await wallet.faucet();
console.log(`Faucet transaction: ${faucetTransaction}`);
// Create a new Wallet to transfer funds to.
// Then, we can transfer 0.00001 ETH out of the Wallet to another Wallet.
const anotherWallet = await Wallet.create();
let transfer = await wallet.createTransfer({ amount: 0.00001, assetId: Coinbase.assets.Eth, destination: anotherWallet });
transfer = await transfer.wait();

Gasless USDC Transfers

To transfer USDC without needing to hold ETH for gas, you can use the createTransfer method with the gasless option set to true.

let transfer = await wallet.createTransfer({ amount: 0.00001, assetId: Coinbase.assets.Usdc, destination: anotherWallet, gasless: true });
transfer = await transfer.wait();

By default, gasless transfers are batched with other transfers, and might take longer to submit. If you want to opt out of batching, you can set the skipBatching option to true, which will submit the transaction immediately.

let transfer = await wallet.createTransfer({
  amount: 0.00001,
  assetId: Coinbase.assets.Usdc,
  destination: anotherWallet,
  gasless: true,
  skipBatching: true
});
transfer = await transfer.wait();

Trading Funds

// Create a Wallet on `base-mainnet` to trade assets with.
let mainnetWallet = await Wallet.create({ networkId: Coinbase.networks.BaseMainnet });

console.log(`Wallet successfully created: ${mainnetWallet}`);

// Fund your Wallet's default Address with ETH from an external source.

// Trade 0.00001 ETH to USDC
let trade = await wallet.createTrade({ amount: 0.00001, fromAssetId: Coinbase.assets.Eth, toAssetId: Coinbase.assets.Usdc });
trade = await trade.wait();

console.log(`Trade successfully completed: ${trade}`);

Re-Instantiating Wallets

The SDK creates Wallets with developer managed keys, which means you are responsible for securely storing the keys required to re-instantiate Wallets. The below code walks you through how to export a Wallet and store it in a secure location.

// Export the data required to re-instantiate the Wallet.
const data = wallet.export();

In order to persist the data for the Wallet, you will need to implement a store method to store the data export in a secure location. If you do not store the Wallet in a secure location you will lose access to the Wallet and all of the funds on it.

// At this point, you should implement your own "store" method to securely persist
// the data required to re-instantiate the Wallet at a later time.
await store(data);

For convenience during testing, we provide a saveSeed method that stores the wallet's seed in your local file system. This is an insecure method of storing wallet seeds and should only be used for development purposes.

const seedFilePath = "";
wallet.saveSeedToFile(seedFilePath);

To encrypt the saved data, set encrypt to true. Note that your CDP API key also serves as the encryption key for the data persisted locally. To re-instantiate wallets with encrypted data, ensure that your SDK is configured with the same API key when invoking saveSeed and loadSeed.

wallet.saveSeedToFile(seedFilePath, true);

The below code demonstrates how to re-instantiate a Wallet from the data export.

// The Wallet can be re-instantiated using the exported data.
const importedWallet = await Wallet.import(data);

To import Wallets that were persisted to your local file system using saveSeed, use the below code.

const userWallet = await Wallet.fetch(wallet.getId());
await userWallet.loadSeedFromFile(seedFilePath);

Acknowledgments

This project includes code from viem licensed under MIT.

版本与发布记录

@coinbase/coinbase-sdk 共发布 47 个版本,首次发布于 2024/05/29;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.25.0latest2025/06/051.4 年前
0.24.02025/05/081.5 年前
0.23.02025/04/171.6 年前
0.22.02025/04/031.6 年前
0.21.02025/03/011.7 年前
0.20.02025/02/261.7 年前

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

@coinbase/coinbase-sdk 是什么?

Coinbase Platform SDK

如何安装 @coinbase/coinbase-sdk?

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

@coinbase/coinbase-sdk 支持 TypeScript 吗?

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

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

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@coinbase/coinbase-sdk 可以商用吗?

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

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

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

@coinbase/coinbase-sdk 还在维护吗?

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