A reference Java application that connects to the Coinbase Prime WebSocket API to stream real-time level 2 market data and display the best bid and ask prices for specified cryptocurrency trading pairs.
- Real-time WebSocket streaming from Coinbase Prime
- Automatic reconnection with exponential backoff
- Production-ready logging with SLF4J and Logback
- Graceful shutdown handling
- Java 11 or higher
- Maven 3.6+ for building
You need the following credentials from your Coinbase Prime account:
API_KEY- Your API access keySECRET_KEY- Your API secret keyPASSPHRASE- Your API passphraseSVC_ACCOUNTID- Your service account ID
git clone https://github.com/coinbase-samples/prime-best-bid-ask-java
cd prime-best-bid-ask-javaSet environment variables:
export API_KEY="your_api_key_here"
export SECRET_KEY="your_secret_key_here"
export PASSPHRASE="your_passphrase_here"
export SVC_ACCOUNTID="your_account_id_here"Edit the product IDs in src/main/java/com/coinbase/prime/BestBidAskPrinter.java:
private static final String[] PRODUCT_IDS = {"BTC-USD", "ETH-USD"};Note: Coinbase Prime supports up to 10 products per WebSocket connection.
# Build the application
mvn clean package
# Run the application
java -jar target/prime-best-bid-ask-java-0.1.0.jarmvn clean compile exec:java -Dexec.mainClass="com.coinbase.prime.BestBidAskPrinter"12:24:15.123 - WebSocket connected to wss://ws-feed.prime.coinbase.com
12:24:15.456 - BTC-USD → Best Bid: 43250.12000000 (qty 0.500000) | Best Ask: 43251.50000000 (qty 0.750000)
12:24:15.789 - ETH-USD → Best Bid: 2456.78000000 (qty 2.100000) | Best Ask: 2457.20000000 (qty 1.800000)
Customize reconnection behavior in BestBidAskPrinter.java:
private static final int MAX_RECONNECT_ATTEMPTS = 10;
private static final long INITIAL_RECONNECT_DELAY = 1000; // 1 secondThis project is licensed under the Apache License 2.0 - see the LICENSE file for details.