Coinbase CLI
Headless access to Coinbase Advanced Trade. Install once, authenticate with a CDP API key, trade crypto with JSON output.Installation
npm i -g @coinbase/coinbase-cli again.
Authentication
Create a CDP API key and configure the CLI:- Sign in to https://portal.cdp.coinbase.com/projects/api-keys (a project is auto-created on first sign-in)
- Click Create API Key
- Give it a name (e.g.,
my-trading-agent) - Click API restrictions and enable Trade and Transfer (View is enabled by default)
- Click Advanced settings and change the key type to ECDSA (brokerage rejects Ed25519)
- Click Create & Download — save the JSON key file
Core Commands
Market Data
| Command | What it does |
|---|---|
coinbase products get <product_id> | Price, 24h volume/change, size limits |
coinbase products list | All tradeable products (900+ results — use symbol or --jq to filter) |
coinbase products list symbol==USD | Products with USD as base or quote currency |
coinbase products ticker <product_id> | Recent trades with best bid/ask (default: 100 trades) |
coinbase products book <product_id> | Full order book: bids + asks |
coinbase products candles <product_id> granularity==1h | OHLCV price history (default: 60 candles at 1h granularity) |
coinbase products best-bid-ask product_ids=BTC-USD | Current best bid/ask |
Orders
| Command | What it does |
|---|---|
coinbase orders preview product_id=BTC-USD side=BUY type=market quote_size=100 | Dry-run: returns fill estimate, fees, slippage |
coinbase orders create product_id=BTC-USD side=BUY type=market quote_size=100 | Execute a market buy for $100 of BTC |
coinbase orders create product_id=BTC-USD side=BUY type=limit base_size=0.001 limit_price=50000 | Limit buy 0.001 BTC at $50,000 |
coinbase orders create ... client_order_id=<unique-id> | Idempotent order — retries with same ID return existing order instead of creating duplicate. Always use for retry safety. |
coinbase orders list | All orders with status, fill %, fees |
coinbase orders get <order_id> | Single order detail: fill status, average price, fees |
coinbase orders fills | List all trade fills with price, size, commission, liquidity indicator |
coinbase orders edit <order_id> | Modify an existing order |
coinbase orders cancel order_ids:='["<id1>","<id2>"]' | Batch cancel orders |
coinbase orders close-position product_id=<id> size=<size> | Close an open position |
Portfolios & Balances
| Command | What it does |
|---|---|
coinbase balance | All account balances (crypto + fiat) |
coinbase portfolios list | All portfolios with UUID, name, type |
coinbase portfolios get <portfolio_id> | Portfolio breakdown: balances, positions, allocation %, unrealized PnL |
coinbase portfolios create name=<name> | Create a new portfolio |
coinbase portfolios edit <portfolio_id> name=<new-name> | Rename a portfolio |
coinbase portfolios delete <portfolio_id> | Delete a portfolio (must be empty) |
coinbase transfer amount=100 currency=USD from=<src_uuid> to=<dst_uuid> | Move funds between portfolios |
Conversions
| Command | What it does |
|---|---|
coinbase convert quote from=USDC to=USD amount=100 | Get conversion quote (rate + fee) |
coinbase convert execute <quote_id> from=USDC to=USD | Execute a quoted conversion |
coinbase convert get <quote_id> | Check conversion status |
Info & Session
| Command | What it does |
|---|---|
coinbase fees | Fee tier (maker/taker rates), 30-day volume |
coinbase env | View/manage credentials across environments |
coinbase mcp | Start MCP server (stdio) — exposes all commands as tools |
Agent-Critical Flags
Use these flags on any command. They are the most important features for safe, efficient agent operation.| Flag | What it does | When to use |
|---|---|---|
--template | Print the expected JSON request body, don’t send | Before every first call to a command — discover exact field names instead of guessing |
--dry-run | Assemble the full request and print it, don’t send | Before executing any write operation (orders, transfers) to verify what will be sent |
--jq <expr> | Filter JSON response with a jq expression | Extract specific fields to save context: --jq '.price', --jq '.accounts[].currency' |
-e <env> | Override the active environment | Switch between live, development, staging, production |
Field syntax
| Syntax | Meaning | Example |
|---|---|---|
key=value | String body field | product_id=BTC-USD |
key:=value | Raw JSON body field (arrays, numbers, booleans) | order_ids:='["abc","def"]' |
key==value | Query parameter | product_type==SPOT |
@file.json | Load body from JSON file | @order.json |
Workflows
1. Check price and preview a trade
2. Sell an asset
3. Transfer funds between portfolios
4. Convert USDC to USD (or vice versa)
5. Discover field names for any command
6. Filter large result sets
MCP Server Setup
The CLI includes an MCP server that exposes every command as a typed tool. If using MCP, the tool schemas are your primary reference — they include full field descriptions and enums. Claude Code:Gotchas
- ECDSA keys only. Brokerage rejects Ed25519. If you get HTTP 401, check key type in CDP portal.
- Products list returns 900+ items. Use
symbol==USDto filter by currency, or--jqfor custom filters. client_order_idprevents duplicate orders. If your connection drops mid-request, retrying with the sameclient_order_idreturns the existing order instead of creating a new one. Always generate one per order attempt.- nvm/fnm users: Upgrading Node versions requires reinstalling global packages. After upgrading, open a new terminal and run
npm i -g @coinbase/coinbase-cliagain.
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
HTTP 401 | Ed25519 key or expired credentials | Create new ECDSA key in CDP portal |
HTTP 403 Missing required scopes | API key lacks View/Trade/Transfer | Check permissions in CDP portal |
insufficient fund (on preview or order) | Account balance too low | Run coinbase balance to check available funds |
PERMISSION_DENIED on portfolio ops | API key not scoped to that portfolio | Create a key scoped to the target portfolio |
coinbase: command not found | CLI not in PATH | Ensure npm global bin is in PATH; reinstall if upgraded Node |
MISSING_FIELDS | Required fields not provided | Run coinbase <command> --template to see expected fields |
INVALID_VALUE | Enum field has wrong value | Check the error message for accepted values |
INVALID_FORMAT | Date/time format wrong | Orders use RFC 3339 with timezone (e.g. 2024-01-01T00:00:00Z) |
Windows: coinbase not found | npm global bin not in PATH | Add %APPDATA%\npm to PATH |