← Protocols
0x
01Description

DEX aggregation and Swap API (v2) routing across 150+ liquidity sources on EVM chains, with Permit2 approvals and an optional gasless flow.

02Best for
  • 01in-app token swaps
  • 02DEX aggregation
  • 03best-price routing
  • 04gasless approvals (Permit2)
  • 05embedded swap widgets
03Install
  • pnpm add @0x/swap-ts-sdk viem
04Environment variables
VariableScopeDescription
ZEROEX_API_KEYServer0x API key from the 0x Dashboard. Required for all Swap API v2 calls.
05Prompt snippet
Use the 0x Swap API v2 for DEX aggregation. Server-side, fetch a firm quote from `GET /swap/permit2/quote` (or `/swap/allowance-holder/quote`) with `chainId`, `sellToken`, `buyToken`, `sellAmount`, and `taker`. Always send the `0x-api-key` and `0x-version: v2` headers. Sign the returned Permit2 EIP-712 payload with the user's wallet, concatenate the signature to `transaction.data` per the docs, then submit `transaction` via viem `sendTransaction`. Use `/swap/permit2/price` for indicative pricing in the UI before requesting the firm quote.
06Gotchas
  • Swap API v2 requires the `0x-version: v2` header on every request — v1 endpoints are deprecated.
  • Permit2 quotes return an EIP-712 payload that must be signed and appended to `transaction.data` — submitting `transaction.data` unmodified will revert.
  • Quotes expire quickly (seconds) and price moves invalidate them — re-quote on user confirmation rather than caching.
  • Always pass realistic `slippageBps` (default behavior is conservative); tokens with transfer fees need explicit `taker` simulation or they will fail on-chain.
  • @0x/swap-ts-sdk is server-only — never ship the API key to the browser; proxy through your own route.
  • Coverage is EVM-only; for Solana use Jupiter and for cross-chain use LI.FI or Socket.
07Alternatives