Get Started with Swap API

View as Markdown

About Swap API

Swap API is a DEX aggregation and smart order routing REST API that finds the best price for crypto trades. With one API integration, you can easily add trading to your app. Swap API aggregates liquidity from 150+ sources , including AMMs and professional market makers, across the supported chains.

Swap API UI

The API handles three key tasks:

  • Queries ERC20 prices from decentralized exchanges and market makers
  • Aggregates liquidity for the best possible price
  • Returns a trade format executable via your preferred web3 library

What You Will Learn

This guide will cover the core steps to using Swap API, specifically using the Swap AllowanceHolder endpoint.

Playground

Try this code example directly in your browser—no installation needed!

Swap Token in 4 Steps

  1. Get a 0x API key
  2. Get an indicative price
  3. (If needed) Set token allowance
  4. Fetch a firm quote
  5. Submit the transaction

Follow along with the Swap Code Example.

0. Get a 0x API key

Every 0x API call requires an API key. Create a 0x account to get your live API key. Follow the setup guide for more details.

1. Get an Indicative Price

Let’s find the best price!

Use the /swap/allowance-holder/price endpoint to get an indicative price for an asset pair. This returns pricing information without creating a full order or transaction, allowing the user to browse potential prices before committing.

Think of /price as the “read-only” version of /quote, which you’ll use in step 3.

Example Request

Here is an example request to sell 100 WETH for DAI using /price.

See code example:

1const priceParams = new URLSearchParams({
2 chainId: "1", // / Ethereum mainnet. See the 0x Cheat Sheet for all supported endpoints: https://0x.org/docs/core-concepts/0x-cheat-sheet
3 sellToken: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", //ETH
4 buyToken: "0x6b175474e89094c44da98b954eedeac495271d0f", //DAI
5 sellAmount: "100000000000000000000", // Note that the WETH token uses 18 decimal places, so `sellAmount` is `100 * 10^18`.
6 taker: "$USER_TAKER_ADDRESS", //Address that will make the trade
7});
8
9const headers = {
10 "0x-api-key": "[api-key]", // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
11 "0x-version": "v2",
12};
13
14const priceResponse = await fetch(
15 "https://api.0x.org/swap/allowance-holder/price?" + priceParams.toString(),
16 {
17 headers,
18 }
19);
20
21console.log(await priceResponse.json());

Example Response

You will receive a response that looks like this:

1{
2 "allowanceTarget": "0x0000000000001ff3684f28c67538d4d072c22734",
3 "blockNumber": "23234885",
4 "buyAmount": "458050129388884000000000",
5 "buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
6 "fees": {
7 "integratorFee": null,
8 "zeroExFee": {
9 "amount": "688205446596484002742",
10 "token": "0x6b175474e89094c44da98b954eedeac495271d0f",
11 "type": "volume"
12 },
13 "gasFee": null
14 },
15 "gas": "1116817",
16 "gasPrice": "2558459858",
17 "issues": {
18 "allowance": {
19 "actual": "0",
20 "spender": "0x0000000000001ff3684f28c67538d4d072c22734"
21 },
22 "balance": {
23 "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
24 "actual": "0",
25 "expected": "100000000000000000000"
26 },
27 "simulationIncomplete": false,
28 "invalidSourcesPassed": []
29 },
30 "liquidityAvailable": true,
31 "minBuyAmount": "453462747021444000000000",
32 "route": {
33 "fills": [
34 {
35 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
36 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
37 "source": "Uniswap_V3",
38 "proportionBps": "500"
39 },
40 {
41 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
42 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
43 "source": "Uniswap_V3",
44 "proportionBps": "3251"
45 },
46 {
47 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
48 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
49 "source": "Uniswap_V4",
50 "proportionBps": "1250"
51 },
52 {
53 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
54 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
55 "source": "Uniswap_V4",
56 "proportionBps": "500"
57 },
58 {
59 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
60 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
61 "source": "0x_RFQ",
62 "proportionBps": "333"
63 },
64 {
65 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
66 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
67 "source": "Uniswap_V3",
68 "proportionBps": "499"
69 },
70 {
71 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
72 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
73 "source": "Uniswap_V3",
74 "proportionBps": "749"
75 },
76 {
77 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
78 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
79 "source": "Uniswap_V4",
80 "proportionBps": "749"
81 },
82 {
83 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
84 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
85 "source": "0x_RFQ",
86 "proportionBps": "2169"
87 },
88 {
89 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
90 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
91 "source": "Ekubo",
92 "proportionBps": "4166"
93 },
94 {
95 "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
96 "to": "0x6b175474e89094c44da98b954eedeac495271d0f",
97 "source": "Maker_PSM",
98 "proportionBps": "10000"
99 }
100 ],
101 "tokens": [
102 {
103 "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
104 "symbol": "WETH"
105 },
106 {
107 "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
108 "symbol": "USDT"
109 },
110 {
111 "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
112 "symbol": "USDC"
113 },
114 {
115 "address": "0x6b175474e89094c44da98b954eedeac495271d0f",
116 "symbol": "DAI"
117 }
118 ]
119 },
120 "sellAmount": "100000000000000000000",
121 "sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
122 "tokenMetadata": {
123 "buyToken": {
124 "buyTaxBps": "0",
125 "sellTaxBps": "0"
126 },
127 "sellToken": {
128 "buyTaxBps": "0",
129 "sellTaxBps": "0"
130 }
131 },
132 "totalNetworkFee": "2857331463231986",
133 "zid": "0x2450cc642b05b3cebfd9bf35"
134}

2. Set a Token Allowance

Before proceeding with the swap, you’ll need to set a token allowance.

A token allowance lets a third party move your tokens on your behalf. Approve an allowance for the AllowanceHolder contract—do not approve the Settler contract.

Specify the amount of ERC20 tokens the contract can use.

For detailed instructions, see how to set your token allowances.

  • NEVER set an allowance on the Settler contract. Doing so may result in unintended consequences, including potential loss of tokens or exposure to security risks. The Settler contract does not support or require token allowances for its operation. Setting an allowance on the Settler contract will lead to misuse by other parties.

  • ONLY set allowances on AllowanceHolder or Permit2 contracts, as indicated by the API responses.

  • The correct allowance target is returned in issues.allowance.spender or allowanceTarget.

When setting the token allowance, make sure to provide enough allowance for the buy or sell amount as well as the gas; otherwise, you may receive a ‘Gas estimation failed’ error.

Example Code

Here is an example of how to check and set token approvals.

See code example.

1// Set up contracts. Note abi and client setup not show in this snippet
2
3// Check if taker needs to set an allowance for AllowanceHolder
4if (price.issues.allowance !== null) {
5 try {
6 const { request } = await usdc.simulate.approve([
7 price.issues.allowance.spender,
8 maxUint256,
9 ]);
10 console.log("Approving AllowanceHolder to spend USDC...", request);
11 // Set approval
12 const hash = await usdc.write.approve(request.args);
13 console.log(
14 "Approved AllowanceHolder to spend USDC.",
15 await client.waitForTransactionReceipt({ hash })
16 );
17 } catch (error) {
18 console.log("Error approving AllowanceHolder:", error);
19 }
20} else {
21 console.log("USDC already approved for AllowanceHolder");
22}

3. Fetch a Firm Quote

When you’re ready to execute a trade, request a firm quote from the Swap API using /swap/allowance-holder/quote. This signals a soft commitment to complete the trade.

The response includes a full 0x order, ready for submission to the network. The Market Maker is expected to have reserved the necessary assets, reducing the likelihood of order reversion.

Example Request

Here is an example to fetch a firm quote sell 100 WETH for DAI using /quote.

See code example.

1const qs = require('qs');
2
3const params = {
4 sellToken: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', //WETH
5 buyToken: '0x6b175474e89094c44da98b954eedeac495271d0f', //DAI
6 sellAmount: '100000000000000000000', // Note that the WETH token uses 18 decimal places, so `sellAmount` is `100 * 10^18`.
7 taker: '$USER_TAKER_ADDRESS', //Address that will make the trade
8 chainId: '1', // / Ethereum mainnet. See the 0x Cheat Sheet for all supported endpoints: https://0x.org/docs/core-concepts/0x-cheat-sheet
9};
10
11const headers = {
12 '0x-api-key': '[api-key]', // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
13 '0x-version': 'v2', // Add the version header
14};
15
16const response = await fetch(
17 `https://api.0x.org/swap/allowance-holder/quote?${qs.stringify(params)}`, { headers }
18); /
19
20console.log(await response.json());

Example Response

You will receive a response that looks like this:

1{
2 "allowanceTarget": "0x0000000000001ff3684f28c67538d4d072c22734",
3 "blockNumber": "23234770",
4 "buyAmount": "454443538468285000000000",
5 "buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
6 "fees": {
7 "integratorFee": null,
8 "zeroExFee": {
9 "amount": "682689324469874696651",
10 "token": "0x6b175474e89094c44da98b954eedeac495271d0f",
11 "type": "volume"
12 },
13 "gasFee": null
14 },
15 "issues": {
16 "allowance": {
17 "actual": "0",
18 "spender": "0x0000000000001ff3684f28c67538d4d072c22734"
19 },
20 "balance": {
21 "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
22 "actual": "0",
23 "expected": "100000000000000000000"
24 },
25 "simulationIncomplete": false,
26 "invalidSourcesPassed": []
27 },
28 "liquidityAvailable": true,
29 "minBuyAmount": "449892276190185000000000",
30 "route": {
31 "fills": [
32 {
33 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
34 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
35 "source": "Swaap_V2",
36 "proportionBps": "4667"
37 },
38 {
39 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
40 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
41 "source": "Uniswap_V4",
42 "proportionBps": "249"
43 },
44 {
45 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
46 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
47 "source": "Swaap_V2",
48 "proportionBps": "5084"
49 },
50 {
51 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
52 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
53 "source": "Ekubo",
54 "proportionBps": "2977"
55 },
56 {
57 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
58 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
59 "source": "Uniswap_V4",
60 "proportionBps": "2107"
61 },
62 {
63 "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
64 "to": "0x6b175474e89094c44da98b954eedeac495271d0f",
65 "source": "Maker_PSM",
66 "proportionBps": "10000"
67 }
68 ],
69 "tokens": [
70 {
71 "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
72 "symbol": "WETH"
73 },
74 {
75 "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
76 "symbol": "USDT"
77 },
78 {
79 "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
80 "symbol": "USDC"
81 },
82 {
83 "address": "0x6b175474e89094c44da98b954eedeac495271d0f",
84 "symbol": "DAI"
85 }
86 ]
87 },
88 "sellAmount": "100000000000000000000",
89 "sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
90 "tokenMetadata": {
91 "buyToken": {
92 "buyTaxBps": "0",
93 "sellTaxBps": "0"
94 },
95 "sellToken": {
96 "buyTaxBps": "0",
97 "sellTaxBps": "0"
98 }
99 },
100 "totalNetworkFee": "1394929353342525",
101 "transaction": {
102 "to": "0x0000000000001ff3684f28c67538d4d072c22734",
103 "data": "0x2213bc0b000000000000000000000000df31a70a21a1931e02033dbba7deace6c45...truncated...",
104 "gas": "832055",
105 "gasPrice": "1676486955",
106 "value": "0"
107 },
108 "zid": "0x9c2d3156ecfc7dd6b4acc463"
109}

4. Submit the Transaction

Finally, submit the transaction using your preferred web3 library. In this example, we use viem’s sendTransaction.

This sends the prepared transaction data to the blockchain.

See code example.

1await client.sendTransaction({
2 to: quote?.transaction.to,
3 data: quote?.transaction.data,
4 value: quote?.transaction.value ? BigInt(quote.transaction.value) : undefined, // value is used for native tokens
5});

Learn More

This wraps up the Swap API AllowanceHolder quickstart. Check out the starter projects below: