Introduction
Easily embed crypto trading in your app with Swap API
Prefer to watch a video or see a code demo instead? Jump to 0x Videos or 0x Code Examples.
What is Swap API?
Swap API is a professional-grade DEX aggregation and smart order routing REST API that allows you to find the best price and easily submit the trade with just a few lines of code.
Swap API allows you to add crypto trading in your application with just a single API integration. It finds the best real-time price across 100+ liquidity sources, both public (AMMs) and private (professional market makers), across the supported chains.
Want to build a gasless trading app? Check out Gasless API instead.
Why use Swap API?
Swap API watches out for your users, is easy for your developers, and your team can earn fee revenue!
- ⛓️ Multi-chain liquidity: Easily access liquidity across popular networks (see the full list of supported networks).
- 🤑 Monetize your app: Earn revenue through seamless swap integration.
- 🛡️ User protection: Our smart contracts eliminate allowance risk with standards like Permit2, ensuring secure token approvals.
- ↩ Best execution: Lowest revert rates in the industry.
- 💰 Exclusive RFQ liquidity: Competitive with AMMs, offering zero slippage and guaranteed MEV protection.
- 🚀 User-friendly: Quick and easy to integrate!Find the best price to buy DAI with WETH with the following request:
Try it out
Run this curl request to see a live quote response for selling 100 WETH to buy DAI:
// Replace API key in the header with your own key from https://dashboard.0x.org/
curl --request GET \
--url 'https://api.0x.org/swap/permit2/quote?sellAmount=100000000000000000000&taker=0xa423C7Be031E988B25fb7Ec39b7906582f6858c6&chainId=1&sellToken=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&buyToken=0x6b175474e89094c44da98b954eedeac495271d0f' \
--header '0x-api-key: YOUR_API_KEY'
--header '0x-version: v2'
You will receive a response that looks like this:
Expand to see response
{
"blockNumber": "20170903",
"buyAmount": "340843848647293527128015",
"buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
"fees": {
"integratorFee": null,
"zeroExFee": {
"amount": "512033823706500040753",
"token": "0x6b175474e89094c44da98b954eedeac495271d0f",
"type": "volume"
},
"gasFee": null
},
"gas": "522732",
"gasPrice": "14551300000",
"issues": {
"allowance": {
"actual": "0",
"spender": "0x000000000022d473030f116ddee9f6b43ac78ba3"
},
"balance": {
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"actual": "0",
"expected": "100000000000000000000"
},
"simulationIncomplete": false,
"invalidSourcesPassed": []
},
"liquidityAvailable": true,
"minBuyAmount": "337435410160820591856735",
"route": {
"fills": [
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V3",
"proportionBps": "10000"
},
{
"from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"source": "Maker_PSM",
"proportionBps": "10000"
}
],
"tokens": [
{
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"symbol": "WETH"
},
{
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"symbol": "USDC"
},
{
"address": "0x6b175474e89094c44da98b954eedeac495271d0f",
"symbol": "DAI"
}
]
},
"sellAmount": "100000000000000000000",
"sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"totalNetworkFee": "7606430151600000"
}
This is a valid unsigned Ethereum transaction that can be signed and submitted directly to a node to complete the swap. Read more about the parameters here.
How does it work?
Swap API is a professional-grade DEX aggregation and smart order routing API. Using the API, developers can easily and reliably tap into aggregated multi-chain DEX liquidity. Swap API finds the best executed price across 70+ liquidity sources, both public (AMMs) and private (professional market makers), across a growing number of blockchains
Below is a high-level diagram showing how Swap API works under the hood:
- Retail Trader Requests a quote
- Swap API simultaneously (a) retrieves quote from AMM and (b) requests quotes from Market Makers
- Market Makers responds with signed quote at their discretion
- Swap API aggregates the quotes it has received and compares the prices. It returns the best price to the Retail Trader.
- If the Retail Trader likes the quote, the Retail Trader signs the order. Swap API sends the signed order to the blockchain to be settled by the 0x Protocol.
For more details about how 0x orders are executed, check out How does 0x work?
Under the hood, Swap API performs a series of tasks:
- Queries prices from multiple DEXs and market makers and aggregates the liquidity from the queried sources to provide the best price possible. Think of how Google flights aggregates flight prices for a certain time and date to help you find the best price,
/swap
similarly helps you find the best price across DeFi liquidity sources. - Swap API’s smart order routing algorithm splits up your transaction across different sources to maximize the overall return on your swap. Read more about smart order routing here.
- The response returned by Swap API is a valid unsigned Ethereum transaction that can be signed and submitted directly to an Ethereum node. Easily execute using the transaction using the web3 library of your choice.
Supported Chains
Swap and Gasless v2 APIs are currently supported on the following chains. Select the chain in your request by providing the corresponding chainId
request parameter. To request support for a new chain, please submit a request here.
Chain | Chain ID | Swap API | Gasless API |
---|---|---|---|
Ethereum (Mainnet) | 1 | ✅ | ✅ |
Arbitrum | 42161 | ✅ | ✅ |
Avalanche | 43114 | ✅ | ✅ |
Base | 8453 | ✅ | ✅ |
Blast | 81457 | ✅ | ✅ |
BSC | 56 | ✅ | ✅ |
Linea | 59144 | ✅ | |
Mantle | 5000 | ✅ | ✅ |
Mode | 34443 | ✅ | ✅ |
Optimism | 10 | ✅ | ✅ |
Polygon | 137 | ✅ | ✅ |
Scroll | 534352 | ✅ | ✅ |
Get Started
To create an account, and get your live API keys to access 0x APIs, visit the 0x Dashboard.
Now that you're familiar with what Swap API offers, read Get started with Swap API to get started or choose one of the resources below.