Skip to main content
Two runnable walkthroughs with @0xmonaco/core: a spot flow (authenticate, deposit, withdraw, place an order) and a perps flow (fund a parent margin account, open a 5x long, attach TP/SL, close). For a React app, see Build a Trading App.

Installation

Spot flow

Authentication
  • Use sdk.login(clientId) - it registers a local session key and signs every authenticated request (public market reads are unsigned); there are no tokens to copy or refresh
  • The WebSocket is NOT auto-connected by default — pass sdk.login(clientId, { connectWebSocket: true }) (or connect explicitly) before using sdk.ws.orders() to subscribe
  • Handle 401/403 status codes for authentication errors
Deposits & Withdrawals
  • Get assetId from trading pairs via sdk.market.getTradingPairBySymbol() - use quoteAssetId or baseAssetId
  • assetId (UUID) is used for vault operations: approve(), deposit(), withdraw(), getAllowance(), and needsApproval()
  • Check balances using sdk.profile.getUserBalanceByAssetId(assetId) for detailed balance info (available, locked, total)
  • Always check needsApproval() before depositing
  • Use parseUnits() with correct decimals for token amounts
  • Vault operations require authentication
Trading
  • All amounts are strings (e.g., '1.5', '2000.50')
  • Trading operations require authentication
  • Orders return an orderId for tracking
  • Time-In-Force (TIF) options: 'GTC' (default), 'IOC', or 'FOK'
    • GTC: Order stays active until filled or canceled
    • IOC: Fill immediately, cancel unfilled portion
    • FOK: Fill completely or reject entirely
Error Handling
  • Check error.statusCode for HTTP errors (401, 403)
  • All operations can throw APIError, ContractError, etc.

Perps flow

For concepts, see Margin, Account Vault, and Positions.

What the perps flow covers

  1. Deposit — using the standard spot vault flow; collateral has to land on Monaco before it can be transferred to a margin account.
  2. Trading pair lookup — perps are filtered by marketType === "MARGIN".
  3. Parent margin account funding — one collateral asset per account; USDC is the default.
  4. Preflight via simulateRiskBucketOrderRisk — never place a non-trivial perp order without checking acceptance and projected liquidation.
  5. Order placement with perp options — tradingMode: "MARGIN" and leverage are the key perp options; side sets the one-way direction.
  6. Position lookup after fill — order events stream over userOrders; the position itself appears in listPositions shortly after.
  7. Live risk inspection — getPositionRisk is the right call for live UI updates (cheaper than getPosition).
  8. TP/SL via attachPositionTpSl — pass oco: true so the two legs cancel each other on trigger.
  9. Conditional order lifecycle subscription — sdk.ws.conditionalOrders streams TP/SL state changes.
  10. Close — partial or full, MARKET or LIMIT.

Common variations

  • Reduce-only manual close. Instead of closePosition, use placeMarketOrder with reduceOnly: true — useful when you want to apply slippage protection or specific TIF.
  • Withdraw collateral. After closing all positions, use transferCollateralFromParentMarginAccount to move USDC back to the wallet, then sdk.vault.withdraw(...) to leave Monaco entirely.