Objective Build a backend API that allows placing trades on Polymarket using very simple parameters. The API must fully abstract the Polymarket CLOB complexity, including EIP-712 signing, payload construction, and wallet handling. The user of the API should only provide trade parameters and credentials once. Credential and Wallet Integration The API must integrate with a Polymarket-compatible wallet using server-side credentials. Supported configuration methods: Option A: Private Key The user provides a private key via environment variable. Example: POLYMARKET_PRIVATE_KEY=0x... Option B: API Key / Secret (if applicable) The user provides an API key and secret via environment variables. All credentials must remain server-side and never be exposed to the client. Public API Endpoints The API must expose exactly two endpoints: POST /trade/buy POST /trade/sell Both endpoints use the same request schema. The only logical difference is the trade side. Request Parameters (Simple Interface) Request body: tokenId: string side: "buy" or "sell" price: number amount: number expiration: number orderType: "limit" or "market" Parameter Rules tokenId ERC-1155 outcome token ID for the Polymarket market. side Trade direction. Must be "buy" or "sell". price Required only for limit orders. Ignored for market orders. amount Number of contracts to trade. expiration Optional. 0 means Good-Till-Cancel. orderType Optional. Defaults to "limit" if not provided. Internal Order Logic Normalize Inputs Convert side to "BUY" or "SELL". Default orderType to "limit". Limit Orders Use the provided price exactly as received. Market Orders (Simulated) Polymarket does not support native market orders. Market orders must be simulated using aggressive limit prices: For BUY orders, internally set price to 0.99. For SELL orders, internally set price to 0.01. Optionally, the API may read the orderbook and use bestBid or bestAsk with a small offset. CLOB Payload Construction (Internal Only) The API must internally construct a valid Polymarket CLOB payload with the following fixed rules: price: string size: string side: BUY or SELL tokenID: string expiration: number tickSize: 0.01 feeEnabled: true feeBps: 1000 chainId: 137 (Polygon) host: https://clob.polymarket.com The signer address must be derived from the configured wallet. Signature Handling The API must generate and sign EIP-712 typed data using the configured wallet or private key. The generated signature must be injected into the final payload before submission. The client must never perform any signing. API Response (Normalized) The API should return a normalized response with the following fields: status: success or error orderId: string side: BUY or SELL price: number amount: number filled: number remaining: number orderType: limit or market txHash: string or null rawResponse: full Polymarket response Error Handling The API must map Polymarket errors to clear error codes, including but not limited to: INSUFFICIENT_BALANCE SIGNATURE_ERROR MARKET_CLOSED INVALID_PRICE INVALID_TOKEN_ID Fixed Rules Tick size is always 0.01. Fees are always enabled with feeBps set to 1000. All trades occur on Polygon (chainId 137). Market orders should automatically cancel any unfilled remainder. Summary for Developer Build a backend trading API where the client only passes simple trade parameters. The API must fully manage wallet integration, EIP-712 signing, Polymarket CLOB payloads, and order execution internally.