Skip to main content
This walkthrough builds a minimal but complete futures agent. It signs in with its own wallet, reads the market, sizes an order with a preview, submits it, listens for fills on the real-time stream, and holds a kill switch.
All traffic goes through the OCX API gateway at https://api.ocx.global, written below as $API_BASE / API_BASE. Prices and quantities are JSON strings — keep them as strings end to end. Quantities are whole contracts. Market ids change as contracts expire; read them from /fut/markets.

Step 1 — Sign in and get a session token

The agent signs a SIWE message with a wallet key it controls and keeps the session token from the astra_session cookie. Authentication lists the required message fields and has a complete Node sign-in example (viem + siwe); wrap it in a signIn() function that returns the token.
JavaScript
The wallet key and the session token both control the account. Use a dedicated wallet for the agent and load its key and token from a secret manager, never from source, prompts, or logs.

Step 2 — Read the market

Market data needs no auth. Discover markets, then read the book and stats for the one you want to trade.
For anything more than a one-shot decision, subscribe to GET /fut/book-stream instead of polling — it pushes the book each time it changes and is not rate-limited.

Step 3 — Size the order with a preview

Always dry-run before you commit. preview returns the notional, the estimated fee, and — critically — whether the order would pass the server-side margin check. It has no side effects.
boolean
Whether the order would pass the margin check. If false, read reason and resize or fund — do not retry the same order.
string
Margin the order needs.
string
Estimated fee for this order, with feePerLot and feeRole (maker for post-only orders, otherwise taker).
string
quantity × contractSize × price.

Step 4 — Submit the order

With an approved preview, place the order. Set a clientOrderId so a network retry can never create a duplicate.
Order controls you’ll reach for:
gtc rests until filled or cancelled; ioc fills what it can immediately then cancels the rest; fok fills fully or not at all.
postOnly rejects an order that would take liquidity (keeps you on the maker side). reduceOnly prevents an order from ever increasing your position — use it for exits.
stopPrice, stopLossPrice, or takeProfitPrice make the order conditional: it is held and submitted once the OCX book mid reaches the trigger. See Place an order for the trigger directions.

Step 5 — Monitor fills over the stream

Open one connection to the futures account stream. On connect you receive a snapshot of your recent trades and positions across all futures markets; after that, delta events push new fills and position upserts as they happen. A fill shows up as a trade plus a position delta — that’s your signal to rebalance.
cURL
EventSource cannot send an Authorization header, so read the stream with fetch — the Fill stream guide has a complete reader with gap detection.
Streams can drop on network blips. Apply the initial snapshot, fold in each delta, check that eventSeq increases by exactly one, and reconnect (re-snapshotting) on disconnect or on a gap.
Between stream events, reconcile with the pull endpoints when you need certainty:

Step 6 — Manage risk and shut down cleanly

Risk is enforced server-side, but your agent still owns its limits. Track NLV and margin headroom, de-risk proactively, and always have a kill path.
cURL
JavaScript
Handle INSUFFICIENT_MARGIN by resizing or funding, not by retrying. Back off on 429 and RATE_LIMITED. On 401, sign in again and re-snapshot.

The full loop

1

Provision

Create a dedicated wallet for the agent, fund it, and store its key in a secret manager.
2

Sign in & subscribe

Sign in with SIWE, cache the market list and fee schedule, and open the futures account stream.
3

Decide & size

Compute a signal, then preview to confirm margin, fee, and approval.
4

Execute

Place via /fut/orders, always with a clientOrderId.
5

React

Fold in fills and position deltas from the stream; adjust inventory.
6

Guard

Enforce your own limits; cancel-all or positions/close on breach, disconnect, or shutdown; renew the session before it expires.

Back to AI agents overview

Revisit the concepts: the agent loop, authentication, and server-side risk.