This tutorial shows how to query onchain data from the Base network using the Coinbase Developer Platform (CDP) SQL API from a simple Node.js app. You will:
- Configure your CDP API key
- Run a query to fetch the latest block on Base
- Run a whale-watching query to find the largest recent USDC transfers
- Node.js v16+
- A CDP API key with access to the SQL API
- Install dependencies:
npm install- Create a
.envfile and set your key:
echo "CDP_CLIENT_API_KEY=YOUR_API_KEY_HERE" > .envIf this repo includes a .env.example, you can also copy it and fill in values:
cp .env.example .env- Run the demo:
# Runs both demos: latest block + whale watching (10k sample)
npm start
# Latest block only
npm run latest-block
# Whale watching (top 5 USDC transfers from recent sample)
npm run whales
# Optional: smaller or larger sample window
npm run whales:1k
npm run whales:10kThe app sends SQL queries to the CDP SQL API endpoint:
index.jsloads theCDP_CLIENT_API_KEYfrom.envrunLatestBlockDemo()queries the latest block frombase.blocksrunWhaleWatchingDemo()selects recentTransferevents for USDC on Base frombase.events, sorts by value, and prints the top 5
Key files:
index.js: Main CLI entry with two demospackage.json: Helpful scripts for running demos
- Change the token by editing
DEFAULT_USDC_BASE_ADDRESSinindex.js - Adjust the sample size with
npm run whales -- <limit>or usewhales:1k/whales:10k
- USDC has 6 decimals. The demo prints both an exact
usdc_exactstring and a human-friendlyusdc_formattedapproximation. - Results are derived from a recent sample (
LIMITclause). Increase the limit to widen the sample.
- "Please set CDP_CLIENT_API_KEY": Ensure
.envhasCDP_CLIENT_API_KEYand you restarted the process after changes. - 401/403 errors: Check that your API key is valid and has required permissions.