Demo
Route a Swap with 0x
Use the AllowanceHolder flow for a B20 or 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
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.
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.