Decide in 30 seconds
Which call needs which header: Base URL, app IDs & API keys.
A bot is one user. It signs a SIWE message with its own private key, gets a user access token, and
trades from that user’s managed balance. No browser, no API key.
You’ll need:
- An app ID from the admin dashboard (
https://admin.agg.market), under API. - One allowed origin on that app (Domains), for example
https://bot.example.com. It never has to serve a page. Wallet sign-in names it, and an app with none cannot complete sign-in. - Node 18 or later,
@agg-build/sdk, andviem. - A private key for the bot. Keep it out of source control.
terminal is true, then the bot’s positions.AGG_LIVE=1.
0. Set up
1. Sign in with the bot’s key
Get a nonce, build the SIWE message, sign it locally, and verify it.verify() keeps the access
and refresh tokens in the client and sends them from then on.
Keeping the session alive
- The SDK keeps tokens in memory only. A restarted process must call
signIn()again. Each sign-in uses a fresh nonce. - The access token lasts 15 minutes and the refresh token 7 days. When a call returns
401, the SDK refreshes once with the refresh token, stores the new pair, and retries the call. You write no refresh code. - If that refresh fails (the refresh token expired or was revoked), the SDK clears the session and
throws the
401. CallsignIn()again. - With cURL, refresh yourself:
POST /auth/token/refreshwith{ "refreshToken": "..." }andx-app-id, then use the new pair. See Refresh access token. - In Node, a client holds the process open. Call
client.destroy()when the bot is done.
2. Fund the bot
Live mode only. In paper mode, skip to the balance check: the paper account starts with a simulatedPAPER_USD balance.
availableRaw is an integer string in the token’s decimals: "5000000" with 6 decimals is 5 USDC.
3. Find an outcome
You trade an outcome, not a market. Take avenueMarketOutcomes[].id.
4. Quote
The bot’s token makes the quote executable. Fill only whenstatus is "ok".
5. Fill
quoteId or poll status. Resending a quoteId never trades twice. See
Errors, retries & idempotency.
6. Poll to a final state
overallState ends as filled, partially_filled, failed, cancelled, or expired.
7. Read positions
Paper mode
mode: "paper" on the quote, the fill, the status call, and the balance and position reads.
Paper fills price against live books but never reach a venue, so no deposit is needed. A paper
quote must be filled in paper mode. See Paper trading.
To trade live, set AGG_LIVE=1, fund the deposit address from step 2, and drop mode=paper from
the cURL calls.
Failures a bot hits
The SDK throws
AggApiError with status, code, and retryAfterMs; check it with
isAggApiError(err). Every code: Errors, retries & idempotency.
Full script
Save asbot.ts and run with AGG_APP_ID=... BOT_PRIVATE_KEY=0x... npx tsx bot.ts.
SDK
Next
Direct orders
One call per trade with your own idempotent
externalId. Live only.Limit orders
Rest an order at your price on one venue.
Order book stream
Live books over WebSocket instead of polling.
Self-custody
Keep funds in the bot’s own wallet; the bot signs each step.