Order management
Preview an Advanced Trade order, submit it when ready, and track changes through the private WebSocket stream. The runnable example only previews an order. Submission and cancellation make live requests.
Check account access and balances
Configure Advanced Trade credentials and call getApiKeyPermissions() to inspect access. Use getAccounts() to read accounts and their available and held balances, following cursor while has_next is true. Each account has its own UUID; use getAccount({ account_id }) for a targeted read. Check product rules, permissions and margin requirements as well as balances.
import { client } from './authenticated-client.js';
async function main() {
const permissions = await client.getApiKeyPermissions();
console.log(permissions);
const accounts = await client.getAccounts({ limit: 100 });
for (const account of accounts.accounts) {
console.log(account.currency, {
available: account.available_balance.value,
held: account.hold.value,
});
}
console.log('More pages:', accounts.has_next);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});1. Prepare and preview
Read current product increments, minimums and trading restrictions. Keep amounts as decimal strings. Save this as preview-order.ts alongside the credential module, then use that guide's compile and run commands:
import type { PreviewAdvTradeOrderRequest } from 'coinbase-api';
import { client } from './authenticated-client.js';
async function main() {
const request: PreviewAdvTradeOrderRequest = {
product_id: 'BTC-USD',
side: 'BUY',
order_configuration: {
market_market_ioc: { quote_size: '10' },
},
};
const preview = await client.previewOrder(request);
console.log(preview);
if (preview.errs.length > 0) {
throw new Error(`Preview rejected: ${preview.errs.join(', ')}`);
}
console.log('Preview only. No order was submitted.');
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});The example previews a market buy with a USD quote size of 10. This is an illustrative amount, not a recommendation or a guarantee that the order meets current minimums. Inspect errs, warning, commissions and totals. A successful preview does not reserve funds or guarantee later execution.
2. Submit an order
For a real order, call submitOrder() with the reviewed product_id, side and order_configuration, plus preview_id when using the preview result. This sends a live trading request with the default mainnet client.
Generate a client order ID with client.generateNewOrderId() and persist it before submission. Keep the exchange's returned order_id and client_order_id for recovery. Inspect success: a resolved HTTP request can contain success: false and error_response. On success, read success_response and save its identifiers.
Connect and confirm the private user stream before submitting. Use subsequent order events to track fills and terminal states; do not query REST simply because submission returned.
Application functions
These functions send live requests when called; loading the module does not place or cancel an order. Generate and persist the client order ID before passing the request to submitReviewedOrder(). Catch errors when calling these functions. If the connection fails, resolve the order outcome before retrying.
import type { SubmitAdvTradeOrderRequest } from 'coinbase-api';
import { client } from './authenticated-client.js';
// These functions send live requests only when your application calls them.
// Persist request.client_order_id before calling submitReviewedOrder().
export async function submitReviewedOrder(request: SubmitAdvTradeOrderRequest) {
const response = await client.submitOrder(request);
if (!response.success || !response.success_response) {
throw new Error(`Order rejected: ${JSON.stringify(response.error_response)}`);
}
const { order_id, client_order_id } = response.success_response;
return { order_id, client_order_id };
}
// Use this to resolve a specific gap, not after every healthy stream update.
export async function checkUnresolvedOrder(orderId: string) {
const { order } = await client.getOrder({ order_id: orderId });
return order;
}
export async function cancelOrder(orderId: string) {
const response = await client.cancelOrders({ order_ids: [orderId] });
return response.results;
}3. Check an unresolved outcome
Use getOrder({ order_id }) when the response or stream leaves a specific order unresolved. getOrders() and getFills() help reconcile gaps or discover the matching order when no exchange ID was received. Match saved client ID, product and request context, and follow pagination.
After a request times out, its outcome is unknown. Do not submit a new order with a new ID simply because the first reply was lost. Resolve the original request before deciding whether another order is needed.
4. Cancel when needed
cancelOrders({ order_ids: [...] }) initiates cancellation for up to 100 IDs per request. Inspect every item in results: each has its own success and failure_reason. A successful cancellation request does not rule out a fill racing with cancellation. Continue applying private order events and reconcile through REST only if the final state remains unclear.
updateOrderPreview() previews an edit; updateOrder() changes the live order. Changes can affect queue priority. Refer to the method and exchange rules before enabling amendments.