Coinbase API in Node.js: App, Advanced Trade and Exchange APIs with the Coinbase SDK
"The Coinbase API" is really several APIs with different hosts, keys and response shapes. Three of them serve public market data without a key, and picking the right one first saves a lot of rework. This guide calls all three from Node.js with coinbase-api, an open-source Coinbase SDK for Node.js and TypeScript that has one client class per Coinbase API.
Which Coinbase API to use
| API | Base URL | Good for | SDK client |
|---|---|---|---|
| Coinbase App API (v2) | https://api.coinbase.com/v2 | Indicative spot, buy and sell prices, exchange rates, and the accounts of a Coinbase app user | CBAppClient |
| Advanced Trade API (v3) | https://api.coinbase.com/api/v3/brokerage | Trading on a Coinbase account: products, order book, candles, orders, fills and portfolios | CBAdvancedTradeClient |
| Exchange API | https://api.exchange.coinbase.com | Coinbase Exchange (the former Coinbase Pro venue) for institutional accounts: ticker, 24 hour stats, trades and order book | CBExchangeClient |
The same package also has clients for the International Exchange, Prime and Commerce APIs. For a retail Coinbase account that places orders from code, Advanced Trade is the one to build on.
Install
npm install coinbase-api
Node.js 22 or newer works for these examples. The package ships ES modules, CommonJS and TypeScript declarations.
Coinbase API market data in one script
Save this as coinbase-api.mjs. None of these calls
needs an API key:
import { CBAppClient, CBAdvancedTradeClient, CBExchangeClient } from 'coinbase-api';
// Coinbase App API (api.coinbase.com/v2): indicative spot price
const app = new CBAppClient();
const { data: spot } = await app.getSpotPrice({ currencyPair: 'BTC-USD' });
console.log('app spot price:', spot.base, spot.currency, spot.amount);
// Advanced Trade API (api.coinbase.com/api/v3): product rules and last price
const advanced = new CBAdvancedTradeClient();
const product = await advanced.getPublicProduct({ product_id: 'BTC-USD' });
console.log('advanced trade:', product.product_id, 'price:', product.price, 'status:', product.status, 'base_increment:', product.base_increment, 'quote_increment:', product.quote_increment, 'quote_min_size:', product.quote_min_size);
// Advanced Trade candles: start and end are UNIX seconds, as strings
const end = Math.floor(Date.now() / 1000);
const { candles } = await advanced.getPublicProductCandles({
product_id: 'BTC-USD',
start: String(end - 3 * 3600),
end: String(end),
granularity: 'ONE_HOUR',
});
console.log('latest 1h candle:', candles[0]);
// Exchange API (api.exchange.coinbase.com): 24 hour stats and best bid/ask
const exchange = new CBExchangeClient();
const stats = await exchange.getProductStats({ product_id: 'BTC-USD' });
console.log('exchange 24h open:', stats.open, 'high:', stats.high, 'low:', stats.low, 'last:', stats.last, 'volume:', stats.volume);
const ticker = await exchange.getProductTicker({ product_id: 'BTC-USD' });
console.log('exchange ticker bid:', ticker.bid, 'ask:', ticker.ask, 'last trade:', ticker.price, ticker.time);
Run it with node coinbase-api.mjs.
Example output:
app spot price: BTC USD 52414.415
advanced trade: BTC-USD price: 52342.5 status: online base_increment: 0.00000001 quote_increment: 0.01 quote_min_size: 1
latest 1h candle: {
start: '1796389200',
low: '51667.37',
high: '52686.05',
open: '51893.39',
close: '52360.27',
volume: '958.32613538'
}
exchange 24h open: 52388.9 high: 55003.61 low: 50001 last: 52499.79 volume: 56921.46346865
exchange ticker bid: 52348.34 ask: 52348.35 last trade: 52348.34 2026-12-04T13:39:28.563736135Z
Prices will differ on each run.
Coinbase API response shapes and number formats
- Each API has its own shape. The App API wraps
results in
{ data }. Advanced Trade returns named objects such as{ candles: [...] }. The Exchange API returns the object itself. Read the field names per API rather than sharing one parser. - Prices are strings. All three APIs return prices and sizes as decimal strings. Keep them as strings, or use a decimal library, until a calculation needs a number.
- The App API price is indicative. The v2 spot price is a reference rate for display, not a live order book price. For trading logic, read the Advanced Trade product or order book.
- Order sizes follow the product rules. Round sizes
to
base_incrementand prices toquote_incrementfromgetPublicProduct(), and keep each order's value abovequote_min_size. - Candles need a time window. Advanced Trade
getPublicProductCandles()requiresstart,end(UNIX seconds, as strings) and agranularitysuch asONE_HOUR, and returns at most 350 candles, newest first, as objects with named fields.
Authenticated Coinbase API calls
The private APIs sign requests in different ways, and the SDK handles each one:
- Advanced Trade and App API: a Coinbase Developer
Platform (CDP) API key. Every request carries a short-lived JWT signed
with the key's private key (ES256 for EC keys, EdDSA for Ed25519 keys).
Pass the key name as
apiKeyand the private key asapiSecrettoCBAdvancedTradeClientorCBAppClient, and the SDK creates a fresh JWT for each call. - Exchange API: an Exchange key with a secret and
passphrase, sent as the
CB-ACCESS-KEY,CB-ACCESS-SIGN,CB-ACCESS-TIMESTAMPandCB-ACCESS-PASSPHRASEheaders. PassapiKey,apiSecretandapiPassphrasetoCBExchangeClient.
Keep keys in environment variables on a server you control and give each key only the permissions it needs.
More Coinbase API examples
- Coinbase WebSocket in Node.js on this site: coinbase-websocket-nodejs
- Coinbase JavaScript SDK overview, tutorial and examples: https://siebly.io/sdk/coinbase/javascript
- Coinbase SDK documentation: https://sieblyio.github.io/docs/coinbase/
- npm package: https://www.npmjs.com/package/coinbase-api
- OKX API in Node.js on this site: okx-api-nodejs