Get Started with Permit2 (Advanced)

View as Markdown

⚠️ The Permit2 flow is intended for advanced integrators only.

If you’re new to 0x Swap API or want to avoid the double-signature UX, consider using AllowanceHolder instead.

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 Permit2 endpoint.

Try It Out

Example Code

Playground

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

Swap Token in 6 Steps

  1. Get a 0x API key
  2. Get an indicative price
  3. (If needed) Set token allowance
  4. Fetch a firm quote
  5. Sign the Permit2 EIP-712 message
  6. Append signature length and signature data to calldata
  7. Submit the transaction with Permit2 signature

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/permit2/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/permit2/price?" + priceParams.toString(),
16 { headers }
17);
18
19console.log(await priceResponse.json());

Example Response

You will receive a response that looks like this:

1{
2 "allowanceTarget": "0x000000000022d473030f116ddee9f6b43ac78ba3",
3 "blockNumber": "23234913",
4 "buyAmount": "457361651612757197478240",
5 "buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
6 "fees": {
7 "integratorFee": null,
8 "zeroExFee": {
9 "amount": "687073014602804210172",
10 "token": "0x6b175474e89094c44da98b954eedeac495271d0f",
11 "type": "volume"
12 },
13 "gasFee": null
14 },
15 "gas": "1276189",
16 "gasPrice": "1528346162",
17 "issues": {
18 "allowance": {
19 "actual": "0",
20 "spender": "0x000000000022d473030f116ddee9f6b43ac78ba3"
21 },
22 "balance": {
23 "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
24 "actual": "0",
25 "expected": "100000000000000000000"
26 },
27 "simulationIncomplete": false,
28 "invalidSourcesPassed": []
29 },
30 "liquidityAvailable": true,
31 "minBuyAmount": "452781164365759128399840",
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": "2919"
45 },
46 {
47 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
48 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
49 "source": "Uniswap_V4",
50 "proportionBps": "250"
51 },
52 {
53 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
54 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
55 "source": "Uniswap_V4",
56 "proportionBps": "250"
57 },
58 {
59 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
60 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
61 "source": "0x_RFQ",
62 "proportionBps": "1916"
63 },
64 {
65 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
66 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
67 "source": "0x_RFQ",
68 "proportionBps": "2166"
69 },
70 {
71 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
72 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
73 "source": "Swaap_V2",
74 "proportionBps": "1252"
75 },
76 {
77 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
78 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
79 "source": "Uniswap_V3",
80 "proportionBps": "249"
81 },
82 {
83 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
84 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
85 "source": "Uniswap_V3",
86 "proportionBps": "249"
87 },
88 {
89 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
90 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
91 "source": "Uniswap_V4",
92 "proportionBps": "249"
93 },
94 {
95 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
96 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
97 "source": "Ekubo",
98 "proportionBps": "1999"
99 },
100 {
101 "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
102 "to": "0x6b175474e89094c44da98b954eedeac495271d0f",
103 "source": "Uniswap_V3",
104 "proportionBps": "75"
105 },
106 {
107 "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
108 "to": "0x6b175474e89094c44da98b954eedeac495271d0f",
109 "source": "Maker_PSM",
110 "proportionBps": "9925"
111 }
112 ],
113 "tokens": [
114 {
115 "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
116 "symbol": "WETH"
117 },
118 {
119 "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
120 "symbol": "USDT"
121 },
122 {
123 "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
124 "symbol": "USDC"
125 },
126 {
127 "address": "0x6b175474e89094c44da98b954eedeac495271d0f",
128 "symbol": "DAI"
129 }
130 ]
131 },
132 "sellAmount": "100000000000000000000",
133 "sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
134 "tokenMetadata": {
135 "buyToken": {
136 "buyTaxBps": "0",
137 "sellTaxBps": "0"
138 },
139 "sellToken": {
140 "buyTaxBps": "0",
141 "sellTaxBps": "0"
142 }
143 },
144 "totalNetworkFee": "1950458560136618",
145 "zid": "0x8deeaf238ca7e5548bc00202"
146}

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 Permit2 contract - do not approve the Settler contract.

Specify the amount of ERC20 tokens the contract can move.

For implementation details, 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// Note abi and client setup not shown in this snippet
2
3// Check if taker needs to set an allowance for Permit2
4// If the sellToken is a native token (e.g. ETH), skip allowance
5if (sellToken.address === CONTRACTS.ETH) {
6 console.log("Native token detected, no need for allowance check");
7} else {
8 // Check if allowance is required
9 if (price.issues.allowance !== null) {
10 try {
11 const { request } = await sellToken.simulate.approve([
12 price.issues.allowance.spender,
13 maxUint256,
14 ]);
15 console.log("Approving Permit2 to spend sellToken...", request);
16 // Set approval
17 const hash = await sellToken.write.approve(request.args);
18 console.log(
19 "Approved Permit2 to spend sellToken.",
20 await client.waitForTransactionReceipt({ hash })
21 );
22 } catch (error) {
23 console.log("Error approving Permit2:", error);
24 }
25 } else {
26 console.log("sellToken already approved for Permit2");
27 }
28}

3. Fetch a Firm Quote

When you’re ready to execute a trade, request a firm quote from the Swap API using /swap/permit2/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/permit2/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": "0x000000000022d473030f116ddee9f6b43ac78ba3",
3 "blockNumber": "23234917",
4 "buyAmount": "457372248072383000000000",
5 "buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
6 "fees": {
7 "integratorFee": null,
8 "zeroExFee": {
9 "amount": "687212589007049771188",
10 "token": "0x6b175474e89094c44da98b954eedeac495271d0f",
11 "type": "volume"
12 },
13 "gasFee": null
14 },
15 "issues": {
16 "allowance": {
17 "actual": "0",
18 "spender": "0x000000000022d473030f116ddee9f6b43ac78ba3"
19 },
20 "balance": {
21 "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
22 "actual": "0",
23 "expected": "100000000000000000000"
24 },
25 "simulationIncomplete": false,
26 "invalidSourcesPassed": []
27 },
28 "liquidityAvailable": true,
29 "minBuyAmount": "452791654701603000000000",
30 "permit2": {
31 "type": "Permit2",
32 "hash": "0x6b35d87d974ef207889b10326321b83869cac1f3b637656901bbd075b68e7123",
33 "eip712": {
34 "types": {
35 "TokenPermissions": [
36 {
37 "name": "token",
38 "type": "address"
39 },
40 {
41 "name": "amount",
42 "type": "uint256"
43 }
44 ],
45 "EIP712Domain": [
46 {
47 "name": "name",
48 "type": "string"
49 },
50 {
51 "name": "chainId",
52 "type": "uint256"
53 },
54 {
55 "name": "verifyingContract",
56 "type": "address"
57 }
58 ],
59 "PermitTransferFrom": [
60 {
61 "name": "permitted",
62 "type": "TokenPermissions"
63 },
64 {
65 "name": "spender",
66 "type": "address"
67 },
68 {
69 "name": "nonce",
70 "type": "uint256"
71 },
72 {
73 "name": "deadline",
74 "type": "uint256"
75 }
76 ]
77 },
78 "domain": {
79 "name": "Permit2",
80 "chainId": 1,
81 "verifyingContract": "0x000000000022d473030f116ddee9f6b43ac78ba3"
82 },
83 "message": {
84 "permitted": {
85 "token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
86 "amount": "100000000000000000000"
87 },
88 "spender": "0xdf31a70a21a1931e02033dbba7deace6c45cfd0f",
89 "nonce": "2241959297937691820908574931991770",
90 "deadline": "1756327693"
91 },
92 "primaryType": "PermitTransferFrom"
93 }
94 },
95 "route": {
96 "fills": [
97 {
98 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
99 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
100 "source": "Uniswap_V3",
101 "proportionBps": "249"
102 },
103 {
104 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
105 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
106 "source": "Uniswap_V3",
107 "proportionBps": "1249"
108 },
109 {
110 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
111 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
112 "source": "0x_RFQ",
113 "proportionBps": "1420"
114 },
115 {
116 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
117 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
118 "source": "0x_RFQ",
119 "proportionBps": "666"
120 },
121 {
122 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
123 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
124 "source": "0x_RFQ",
125 "proportionBps": "166"
126 },
127 {
128 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
129 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
130 "source": "0x_RFQ",
131 "proportionBps": "1083"
132 },
133 {
134 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
135 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
136 "source": "Uniswap_V3",
137 "proportionBps": "250"
138 },
139 {
140 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
141 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
142 "source": "0x_RFQ",
143 "proportionBps": "4834"
144 },
145 {
146 "from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
147 "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
148 "source": "0x_RFQ",
149 "proportionBps": "83"
150 },
151 {
152 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
153 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
154 "source": "Ekubo",
155 "proportionBps": "3101"
156 },
157 {
158 "from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
159 "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
160 "source": "Uniswap_V4",
161 "proportionBps": "2066"
162 },
163 {
164 "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
165 "to": "0x6b175474e89094c44da98b954eedeac495271d0f",
166 "source": "Maker_PSM",
167 "proportionBps": "10000"
168 }
169 ],
170 "tokens": [
171 {
172 "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
173 "symbol": "WETH"
174 },
175 {
176 "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
177 "symbol": "USDT"
178 },
179 {
180 "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
181 "symbol": "USDC"
182 },
183 {
184 "address": "0x6b175474e89094c44da98b954eedeac495271d0f",
185 "symbol": "DAI"
186 }
187 ]
188 },
189 "sellAmount": "100000000000000000000",
190 "sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
191 "tokenMetadata": {
192 "buyToken": {
193 "buyTaxBps": "0",
194 "sellTaxBps": "0"
195 },
196 "sellToken": {
197 "buyTaxBps": "0",
198 "sellTaxBps": "0"
199 }
200 },
201 "totalNetworkFee": "1364448183107568",
202 "transaction": {
203 "to": "0xdf31a70a21a1931e02033dbba7deace6c45cfd0f",
204 "data": "0x1fff991f000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa...truncated...",
205 "gas": "1138152",
206 "gasPrice": "1198827734",
207 "value": "0"
208 },
209 "zid": "0x777155a982e669283398f5bd"
210}

4. Sign the Permit2 EIP-712 Message

Now that we have our quote, the next step is to sign and append the necessary data before submitting the order to the blockchain.

First, sign the permit2.eip712 object that we received from the quote reponse.

See code example.

1// Sign permit2.eip712 returned from quote
2let signature: Hex;
3signature = await signTypedData(quote.permit2.eip712);

5. Append Signature Length and Signature Data to transaction.data

Next, append the signature length and signature data to transaction.data. The format should be <sig len><sig data>, where:

  • <sig len>: 32-byte unsigned big-endian integer representing the length of the signature
  • <sig data>: The actual signature data

See code example.

1import { concat, numberToHex, size } from "viem";
2
3if (permit2?.eip712) {
4 const signature = await signTypedDataAsync(permit2.eip712);
5 const signatureLengthInHex = numberToHex(size(signature), {
6 signed: false,
7 size: 32,
8 });
9 transaction.data = concat([
10 transaction.data,
11 signatureLengthInHex,
12 signature,
13 ]);
14}

6. Submit the Transaction with Permit2 Signature

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

This sends the prepared transaction data, including the Permit2 signature, to the blockchain.

See code example.

1const signedTransaction = await client.signTransaction({
2 account: client.account,
3 chain: client.chain,
4 gas: !!quote?.transaction.gas ? BigInt(quote?.transaction.gas) : undefined,
5 to: quote?.transaction.to,
6 data: quote.transaction.data,
7 gasPrice: !!quote?.transaction.gasPrice
8 ? BigInt(quote?.transaction.gasPrice)
9 : undefined,
10 nonce: nonce,
11});
12
13const hash = await client.sendRawTransaction({
14 serializedTransaction: signedTransaction,
15});

Learn More

This wraps up the Swap API Permit2 quickstart. See the links below for starter projects.