Integration skill for accessing Coinbase Advanced Trade API with granular L2 orderbook data and real-time market feeds.
Integration skill for working with the Coinbase Advanced Trade API to access granular L2 orderbook data and real-time market information.
This skill provides instructions for integrating with Coinbase's Advanced Trade API, focusing on:
**CRITICAL SECURITY REQUIREMENTS:**
1. **Access Keys Securely**
- API keys MUST be stored in `.env` file
- Access keys via environment variables: `COINBASE_API_KEY` and `COINBASE_SECRET_KEY`
- NEVER log, display, or expose the actual key values
- NEVER modify the keys programmatically
- NEVER commit keys to version control
2. **Environment Variable Usage**
```javascript
// Correct way to access keys
const apiKey = process.env.COINBASE_API_KEY;
const secretKey = process.env.COINBASE_SECRET_KEY;
```
**Endpoint:** `wss://advanced-trade-ws.coinbase.com`
1. **level2** - Full orderbook updates
- Provides complete L2 orderbook state
- Includes bid/ask price levels with quantities
- Updates sent incrementally
2. **market_trades** - Real-time Time & Sales
- Live trade execution data
- Includes price, size, time, and side (buy/sell)
- Useful for tracking market activity
```javascript
const ws = new WebSocket('wss://advanced-trade-ws.coinbase.com');
ws.on('open', () => {
// Subscribe to channels
ws.send(JSON.stringify({
type: 'subscribe',
product_ids: ['BTC-USD'],
channels: ['level2', 'market_trades']
}));
});
ws.on('message', (data) => {
const message = JSON.parse(data);
// Process snapshot or updates
});
```
Follow this three-phase approach to build and maintain an accurate orderbook:
1. Upon subscription, receive initial `snapshot` message
2. Parse the complete orderbook state:
- `bids`: Array of [price, quantity] tuples
- `asks`: Array of [price, quantity] tuples
3. Store the full orderbook state in memory
4. Index by price level for efficient lookups
1. Process incoming `l2update` messages
2. Each update contains:
- `side`: "buy" or "sell"
- `price_level`: The price point being modified
- `new_quantity`: Updated quantity at that level
3. Apply updates to the in-memory orderbook:
- If `new_quantity > 0`: Update the level
- If `new_quantity == 0`: Remove the level (no orders remain)
4. Maintain sorting (bids descending, asks ascending)
**Backend Responsibilities:**
**Frontend Optimization:**
```javascript
// Backend: Full book maintenance
const fullOrderbook = {
bids: new Map(), // All bid levels
asks: new Map() // All ask levels
};
// Frontend: Sliced view
function getTop20() {
return {
bids: Array.from(fullOrderbook.bids.entries())
.sort((a, b) => b[0] - a[0]) // Descending price
.slice(0, 20),
asks: Array.from(fullOrderbook.asks.entries())
.sort((a, b) => a[0] - b[0]) // Ascending price
.slice(0, 20)
};
}
```
1. **Error Handling**
- Implement reconnection logic for WebSocket disconnections
- Handle malformed messages gracefully
- Log errors without exposing sensitive data
2. **Data Validation**
- Validate message types before processing
- Ensure price and quantity values are numeric
- Check for sequence numbers if provided (to detect gaps)
3. **Performance Optimization**
- Use efficient data structures (Map/Set for price levels)
- Batch frontend updates to avoid excessive re-renders
- Consider throttling updates during high-volume periods
4. **Testing**
- Test with multiple product IDs simultaneously
- Verify orderbook accuracy against REST API snapshots
- Load test with high-frequency update scenarios
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/coinbase-advanced-trade-api/raw