Private account updates
Use the Advanced Trade user channel to track orders and execution state. Subscribing does not place orders, and the feed does not cover every balance or position change.
Subscribe to user orders
Configure Advanced Trade credentials, save this as private-stream.ts, then run node --env-file=.env private-stream.ts with Node.js 24:
import { WebsocketClient, WS_KEY_MAP } from 'coinbase-api';
function requiredEnvVar(name: string) {
const value = process.env[name];
if (!value) {
throw new Error(`Set ${name}.`);
}
return value;
}
const ws = new WebsocketClient({
apiKey: requiredEnvVar('COINBASE_API_KEY'),
apiSecret: requiredEnvVar('COINBASE_API_SECRET').replace(/\\n/g, '\n'),
});
ws.on('update', (event) => {
console.log(event);
});
ws.on('response', (event) => {
console.log('Subscription response:', event);
});
ws.on('exception', (error) => {
console.error('WebSocket exception:', error);
});
ws.on('reconnect', (event) => {
console.log('Reconnecting:', event);
});
ws.on('reconnected', (event) => {
console.log('Reconnected:', event);
});
// Observe all available user orders; this example does not submit orders.
ws.subscribe(['user', 'heartbeats'], WS_KEY_MAP.advTradeUserData);
function stop() {
ws.closeAll();
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);The example runs until you stop it. Omitting product IDs requests user updates across available products. The SDK signs private subscription messages automatically. No manual JWT refresh loop is needed for this feed.
Maintain local order state
The channel sends an initial order snapshot followed by updates. Initial open orders may arrive in batches; do not mark the snapshot complete after the first message. Follow the user channel schema for batching, order status and sequence fields.
Maintain local order state keyed by order ID and account context. Use status and cumulative filled quantities to update execution state without counting the same fill twice. An order acknowledgement is not a fill, and a subscription reply is not an order acknowledgement.
Load account balances and any positions or order history the stream does not supply through REST. Apply stream events received during that load. Refresh fields the feed does not cover when your application needs them or when repairing a known gap. The user channel alone cannot keep all balances and positions current.
After initialization, update the covered state from stream events. Do not query REST after every order or add a polling loop to check for fills. See Account State.
Recover after a gap
The SDK restores subscriptions automatically after reconnecting; do not subscribe again. On reconnect, mark affected state stale. After the stream resumes, process fresh snapshots; use getOrders(), getFills() and getAccounts() to repair missing state where needed. Follow pagination and account for events arriving during those reads. Resume trading decisions only when the state they depend on is current.
These examples print events for inspection; they do not implement a complete account-state database. Protect private event logs as account data.