Skip to main content
Add token swaps to your app with the 0x Swap API. 0x searches liquidity across decentralized exchanges and market makers, then returns a transaction that the user’s wallet can simulate, sign, and submit on Base.

Demo

The demo above is mock only. If you want to see onchain demos on Vibenet, head to Base chain demos.

Route a Swap with 0x

Use the AllowanceHolder flow for a straightforward ERC-20 swap:
  1. Request an indicative /price while the user edits the trade.
  2. Request a firm /quote when the user is ready to review and sign.
  3. If issues.allowance is present, approve only the returned spender for the sell amount.
  4. Fetch a fresh quote, simulate its transaction, submit it, and wait for confirmation.
Never approve the 0x Settler contract. Approve only the AllowanceHolder or Permit2 address returned by the API in issues.allowance.spender or allowanceTarget.
Install 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 /price for frequently refreshed previews and reserve /quote for 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.