Demo
Route a Swap with 0x
Use the AllowanceHolder flow for a straightforward ERC-20 swap:- Request an indicative
/pricewhile the user edits the trade. - Request a firm
/quotewhen the user is ready to review and sign. - If
issues.allowanceis present, approve only the returnedspenderfor the sell amount. - Fetch a fresh quote, simulate its
transaction, submit it, and wait for confirmation.
viem@2.55.19, create Base publicClient and walletClient instances in clients.ts, and keep ZERO_EX_API_KEY on your server. This example sells 100 USDC for WETH on Base mainnet.
The verified sample uses a server-side wallet only to keep the example runnable. In a user-facing app, request the quote from your backend, return the reviewed transaction fields to the frontend, and submit them with the user’s connected wallet.
This snippet is illustrative. It shows the shape of a routed trading integration on Base, not production-ready code. Follow the 0x Swap API documentation for current API behavior, supported liquidity sources, fees, and contract details.
trade-0x.ts
Build a Safe Trade Review
Before the user signs, display values from the latest quote rather than calculating them yourself:
Request a new quote after an approval transaction or any change to the amount, tokens, taker, recipient, or slippage. Simulate the exact returned transaction against the user’s current state before sending it.
Production Checklist
- Proxy 0x requests through your backend so the API key never reaches the browser.
- Validate Base chain ID
8453, token addresses, the taker, and any separate recipient on the server. - Use
/pricefor frequently refreshed previews and reserve/quotefor the confirmation step. - Show the quoted output, minimum received, fees, gas estimate, and route before requesting a signature.
- Use an exact or deliberately bounded allowance instead of granting unlimited approval by default.
- Handle unavailable liquidity, insufficient balance, incomplete simulation, rate limits, reverted transactions, and user rejection.
- Refresh the quote after approval and refresh wallet balances only after the swap receipt confirms.
0x Documentation
Swap API Quickstart
Follow the full price, allowance, quote, and submission flow.
AllowanceHolder Quote
Review every request parameter and response field.
Token Allowances
Approve the correct spender and avoid unsafe approvals.
Supported Chains
Confirm Base support and the current Swap API feature matrix.
See Also
Integrate Lending
Let users supply USDC to a money market.
Integrate Borrowing
Open a collateralized loan against WETH.
Integrate an Earn Product
Route deposits into yield-bearing vaults.