Market Order
Execute a buy or sell market order.tokenId(string): Market token ID for the positionsize(number): Order size (meaning differs by side)side(string): “BUY” or “SELL”idempotencyKey(string, optional): Durable retry key for auto-mode market orders. SDKs generate one automatically; provide a stable key only when retrying the same logical order after a process restart.expiresAt(string | null, optional): ISO 8601 timestamp. In manual mode, the suggestion expires at this time. If omitted, no time-based expiry is set (suggestions are auto-cleared at eachrunstart).
- BUY:
sizeis the USD amount to spend (e.g.,10= $10 worth of shares) - SELL:
sizeis the number of shares to sell (e.g.,10= 10 shares)
idempotencyKey within the current agent run. Duplicate active or already-submitted attempts are rejected; only failures before any external side effect can retry under the same key.
Response:
success(boolean): Whether the operation succeededdata.orderInfo(object): Order information (on success)orderId(string): Unique order identifierside(string): “BUY” or “SELL”size(string): Order sizepriceUsd(string): Price per share in USDtotalPriceUsd(string): Total order value in USDtxHashes(string[]): Transaction hashes
error(string | null): Error message (on failure)
Redeem Positions
Redeem settled positions and claim winnings. Pass the token IDs of the positions to redeem — fetch your positions first and filter forisRedeemable. Agents only redeem positions their own session holds; there is no “redeem everything in the wallet” mode.
tokenIds(string[], required, non-empty): Token IDs of the positions to redeem. Requesting a token the session doesn’t hold fails with a 404.idempotencyKey(string, optional): Durable retry key for auto-mode redeem attempts. SDKs generate one perredeemPositions()/redeem_positions()call when omitted.
idempotencyKey only when retrying the same logical redeem after a process restart. Active attempts and attempts that already reached an onchain side effect return a conflict for the same key; only failures before any broadcast can retry under the same key.
Response:
success(boolean): Whether the operation succeededdata(array): Array of redemption resultssuccess(boolean): Whether this redemption succeededposition(object | null): Position details (on success)question(string): Market question textoutcome(string): Outcome name (e.g., “Yes”, “No”)valueUsd(string): Position value in USDpnlUsd(string): Profit/loss in USDpnlPercent(string): Profit/loss percentageisRedeemable(boolean): Whether position can be redeemedcontractAddress(string): Token contract addresstokenId(string | null): Token IDdecimals(number): Token decimalsconditionId(string): Market condition IDformattedShares(string): Human-readable share countshares(string): Raw share countaveragePriceUsd(string): Average entry priceinitialValue(string): Original position valuepnlRealizedUsd(string): Realized PnL in USDpnlRealizedPercent(string): Realized PnL percentageisNegativeRisk(boolean): Whether this is a negative risk marketimageUrl(string): Market image URLendDate(string): Market end date
transactionHash(string | null): Transaction hash.nullwithsuccess: falsemeans the position was skipped (not settled yet) and no transaction was broadcast.skippedReason(string, optional): Why the position was skipped without broadcasting (e.g. not settled yet). Present only whensuccessis false.
error(string | null): Error message (on failure)
data is then a suggestion envelope ({ suggested: true, suggestionId }) instead of the redemption array. See Manual vs Auto Mode.
Example:
Get Positions
Get the session’s live Polymarket positions, enriched with market metadata (question, outcome, price, value, redeemability, PnL). Scoped to the outcome tokens this session holds. Use this for fresh state after trading within a run —agent.portfolio.positions carries the same enrichment but as a start-of-run snapshot, so it won’t reflect orders placed during the current run.
success(boolean): Whether the operation succeededdata.totalValue(number): Total USD value of the session’s Polymarket positionsdata.positions(array): Enriched positions — each withtokenId,question,outcome,priceUsd,valueUsd,shares,isRedeemable,pnlUsd,pnlPercent,endDate, …error(string): Error message (only present on failure)
Common Errors
Errors are returned from Polymarket’s API in theerror field. Common errors include:
| Error | Cause | Solution |
|---|---|---|
| ”Insufficient balance” | Not enough pUSD for BUY order | Check agent.portfolio.balances for pUSD balance |
| ”No orderbook exists for the requested token id” | Invalid tokenId or market doesn’t exist | Verify tokenId from Polymarket API |
error field in the response for specific details.
Getting Token IDs
ThetokenId identifies a specific outcome (e.g., “Yes” or “No”) in a Polymarket market. You can find token IDs through:
- Polymarket’s CLOB API:
GET https://clob.polymarket.com/marketsreturns all markets with theirtokensarray, each containing atoken_idandoutcome. - From your holdings: If you already hold shares,
agent.portfolio.positionsincludes Polymarket positions with token IDs inposition.polymarketMetadata.tokenId.
Notes
- Polymarket’s API accepts different decimal precision for buys and sells. This can result in dust positions if selling a position before expiry. Clean up dust with
redeemPositions({ tokenIds })after market expiry. - Negative risk markets: Some markets use an inverted pricing model. The SDK handles this transparently, but be aware that position values may display differently.
- Open positions are available via
agent.portfolio.positions(pUSD cash stays inagent.portfolio.balances). - Polymarket positions always include enriched metadata (question, outcome, PNL) on
position.polymarketMetadata. - SDKs generate
idempotencyKeyautomatically formarketOrderandredeemPositions; pass a stable key only when you need to resume the same logical action after a process restart.
See Also
- Polymarket Agent Example — Full working prediction market agent
- Positions — Polymarket positions include enriched metadata on
polymarketMetadata - Error Handling — Polymarket-specific error codes