Skip to main content

Swap API with Permit2 (Advanced)

Learn how to send your first Swap API request using Permit2.

caution

⚠️ 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.

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).

const priceParams = new URLSearchParams({
chainId: '1', // / Ethereum mainnet. See the 0x Cheat Sheet for all supported endpoints: https://0x.org/docs/introduction/0x-cheat-sheet
sellToken: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', //ETH
buyToken: '0x6b175474e89094c44da98b954eedeac495271d0f', //DAI
sellAmount: '100000000000000000000', // Note that the WETH token uses 18 decimal places, so `sellAmount` is `100 * 10^18`.
taker: '$USER_TAKER_ADDRESS', //Address that will make the trade
});

const headers = {
'0x-api-key': '[api-key]', // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
'0x-version': 'v2',
};

const priceResponse = await fetch('https://api.0x.org/swap/permit2/price?' + priceParams.toString(), { headers });

console.log(await priceResponse.json());

Example Response

You will receive a response that looks like this:

Expand to see response
{
"allowanceTarget": "0x000000000022d473030f116ddee9f6b43ac78ba3",
"blockNumber": "23234913",
"buyAmount": "457361651612757197478240",
"buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
"fees": {
"integratorFee": null,
"zeroExFee": {
"amount": "687073014602804210172",
"token": "0x6b175474e89094c44da98b954eedeac495271d0f",
"type": "volume"
},
"gasFee": null
},
"gas": "1276189",
"gasPrice": "1528346162",
"issues": {
"allowance": {
"actual": "0",
"spender": "0x000000000022d473030f116ddee9f6b43ac78ba3"
},
"balance": {
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"actual": "0",
"expected": "100000000000000000000"
},
"simulationIncomplete": false,
"invalidSourcesPassed": []
},
"liquidityAvailable": true,
"minBuyAmount": "452781164365759128399840",
"route": {
"fills": [
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V3",
"proportionBps": "500"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V3",
"proportionBps": "2919"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V4",
"proportionBps": "250"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V4",
"proportionBps": "250"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "1916"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "2166"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "Swaap_V2",
"proportionBps": "1252"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "Uniswap_V3",
"proportionBps": "249"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "Uniswap_V3",
"proportionBps": "249"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "Uniswap_V4",
"proportionBps": "249"
},
{
"from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Ekubo",
"proportionBps": "1999"
},
{
"from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"source": "Uniswap_V3",
"proportionBps": "75"
},
{
"from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"source": "Maker_PSM",
"proportionBps": "9925"
}
],
"tokens": [
{
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"symbol": "WETH"
},
{
"address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"symbol": "USDT"
},
{
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"symbol": "USDC"
},
{
"address": "0x6b175474e89094c44da98b954eedeac495271d0f",
"symbol": "DAI"
}
]
},
"sellAmount": "100000000000000000000",
"sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"tokenMetadata": {
"buyToken": {
"buyTaxBps": "0",
"sellTaxBps": "0"
},
"sellToken": {
"buyTaxBps": "0",
"sellTaxBps": "0"
}
},
"totalNetworkFee": "1950458560136618",
"zid": "0x8deeaf238ca7e5548bc00202"
}

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.

danger
  • 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.

tip

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.

// Note abi and client setup not shown in this snippet

// Check if taker needs to set an allowance for Permit2
// If the sellToken is a native token (e.g. ETH), skip allowance
if (sellToken.address === CONTRACTS.ETH) {
console.log('Native token detected, no need for allowance check');
} else {
// Check if allowance is required
if (price.issues.allowance !== null) {
try {
const { request } = await sellToken.simulate.approve([price.issues.allowance.spender, maxUint256]);
console.log('Approving Permit2 to spend sellToken...', request);
// Set approval
const hash = await sellToken.write.approve(request.args);
console.log('Approved Permit2 to spend sellToken.', await client.waitForTransactionReceipt({ hash }));
} catch (error) {
console.log('Error approving Permit2:', error);
}
} else {
console.log('sellToken already approved for Permit2');
}
}

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.

const qs = require('qs');

const params = {
sellToken: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', //WETH
buyToken: '0x6b175474e89094c44da98b954eedeac495271d0f', //DAI
sellAmount: '100000000000000000000', // Note that the WETH token uses 18 decimal places, so `sellAmount` is `100 * 10^18`.
taker: '$USER_TAKER_ADDRESS', // Address that will make the trade
chainId: '1', // Ethereum mainnet. See the 0x Cheat Sheet for all supported endpoints: https://0x.org/docs/introduction/0x-cheat-sheet
};

const headers = {
'0x-api-key': '[api-key]', // Get your live API key from the 0x Dashboard (https://dashboard.0x.org/apps)
'0x-version': 'v2', // Add the version header
};

const response = await fetch(
`https://api.0x.org/swap/permit2/quote?${qs.stringify(params)}`, { headers }
); /

console.log(await response.json());

Example Response

You will receive a response that looks like this:

Expand to see response
{
"allowanceTarget": "0x000000000022d473030f116ddee9f6b43ac78ba3",
"blockNumber": "23234917",
"buyAmount": "457372248072383000000000",
"buyToken": "0x6b175474e89094c44da98b954eedeac495271d0f",
"fees": {
"integratorFee": null,
"zeroExFee": {
"amount": "687212589007049771188",
"token": "0x6b175474e89094c44da98b954eedeac495271d0f",
"type": "volume"
},
"gasFee": null
},
"issues": {
"allowance": {
"actual": "0",
"spender": "0x000000000022d473030f116ddee9f6b43ac78ba3"
},
"balance": {
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"actual": "0",
"expected": "100000000000000000000"
},
"simulationIncomplete": false,
"invalidSourcesPassed": []
},
"liquidityAvailable": true,
"minBuyAmount": "452791654701603000000000",
"permit2": {
"type": "Permit2",
"hash": "0x6b35d87d974ef207889b10326321b83869cac1f3b637656901bbd075b68e7123",
"eip712": {
"types": {
"TokenPermissions": [
{
"name": "token",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"PermitTransferFrom": [
{
"name": "permitted",
"type": "TokenPermissions"
},
{
"name": "spender",
"type": "address"
},
{
"name": "nonce",
"type": "uint256"
},
{
"name": "deadline",
"type": "uint256"
}
]
},
"domain": {
"name": "Permit2",
"chainId": 1,
"verifyingContract": "0x000000000022d473030f116ddee9f6b43ac78ba3"
},
"message": {
"permitted": {
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"amount": "100000000000000000000"
},
"spender": "0xdf31a70a21a1931e02033dbba7deace6c45cfd0f",
"nonce": "2241959297937691820908574931991770",
"deadline": "1756327693"
},
"primaryType": "PermitTransferFrom"
}
},
"route": {
"fills": [
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V3",
"proportionBps": "249"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V3",
"proportionBps": "1249"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "1420"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "666"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "166"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "0x_RFQ",
"proportionBps": "1083"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "Uniswap_V3",
"proportionBps": "250"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "0x_RFQ",
"proportionBps": "4834"
},
{
"from": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"source": "0x_RFQ",
"proportionBps": "83"
},
{
"from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Ekubo",
"proportionBps": "3101"
},
{
"from": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"source": "Uniswap_V4",
"proportionBps": "2066"
},
{
"from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"to": "0x6b175474e89094c44da98b954eedeac495271d0f",
"source": "Maker_PSM",
"proportionBps": "10000"
}
],
"tokens": [
{
"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"symbol": "WETH"
},
{
"address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"symbol": "USDT"
},
{
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"symbol": "USDC"
},
{
"address": "0x6b175474e89094c44da98b954eedeac495271d0f",
"symbol": "DAI"
}
]
},
"sellAmount": "100000000000000000000",
"sellToken": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"tokenMetadata": {
"buyToken": {
"buyTaxBps": "0",
"sellTaxBps": "0"
},
"sellToken": {
"buyTaxBps": "0",
"sellTaxBps": "0"
}
},
"totalNetworkFee": "1364448183107568",
"transaction": {
"to": "0xdf31a70a21a1931e02033dbba7deace6c45cfd0f",
"data": "0x1fff991f000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000005fe1e2814e1ae92d9e0000000000000000000000000000000000000000000000000000000000000000a0777155a982e669283398f5bd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000176000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000042000000000000000000000000000000000000000000000000000000000000006e000000000000000000000000000000000000000000000000000000000000008c00000000000000000000000000000000000000000000000000000000000000aa00000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000f800000000000000000000000000000000000000000000000000000000000001160000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000014a00000000000000000000000000000000000000000000000000000000000001560000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000e48d68a156000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f00000000000000000000000000000000000000000000000000000000000000f900000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cc02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000064a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e48d68a156000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000000000000000000000000000000000000000050200000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000001f4a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000028438c9c147000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000000000000682000000000000000000000000def1c0ded9bec7f1a1670819833240f027b25eff000000000000000000000000000000000000000000000000000000000000018400000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001a4dac748d4000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000171be3666f0000000000000000000000000000000000000000000000012caf7689d576b00000000000000000000000000051c72848c68a965f66fa7a88855f9f7784502a7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000068af6e1e000000000000000300000000000000000000000068af6de20000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001c70e70ad925f23c8096bf7e6fd4eebf4fc4ea76124d2bff0e803ae0204123a6ea06f014e3d8be8607144297d189dd19735fd5d289d0faca9e8491072117f6926b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a4d92aadfb000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000071c9e66195c978893cdff5453226d328783a6518acc762084d6da1d91ca295203f8ff8b700000000000000000000000000000000000000000000000000000000068af6e230000000000000000000000009ba0cf1588e1dfa905ec948f7fe5104dd40eda310000000000000000000000000000000000000000000000000000000000000120000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000005c84c203069aac0000000000000000000000000000000000000000000000000000000000000000411cd332c1c004150b9f8588fddd49b5f26eb144788466e7f35801283f93ffe3518a4c2672f2924ed5f040f5592d9c242aba5775e7cf3448bd1ee423c15955c270f6000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a4d92aadfb000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000001c738a7231dab4e92cc02485f0a3637c7daf9747a2f5f90dcc9dc59142647bde0116fb3590000000000000000000000000000000000000000000000000000000068af6e1e000000000000000000000000a8020ecbc321e0c8cea26b3507f207482d0100c20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000017213080c1a6ab000000000000000000000000000000000000000000000000000000000000000041f0729e9ae9b622550f2c565b2faf5ced2eaceb1fab635fb749c6b6d37d4aeab26531a32c9ad359a3402c79b1901979f764ef55128c0f57236205cc50908e24b51c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a4d92aadfb000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000b8dd5aea29855f24369fae4ae88c70d298a5de921df7f82162ef59c3457258d168f846e6c0000000000000000000000000000000000000000000000000000000068af6e42000000000000000000000000bb289bc97591f70d8216462df40ed713011b968a0000000000000000000000000000000000000000000000000000000000000120000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000009657bb44eabb58000000000000000000000000000000000000000000000000000000000000000041ea6cf48ebb231fed105805b4ccf23158ed2de8ea9d263d7c8594d47a87d7a1bc443b3c52f0b7446a1ea9f692ba41f29f19fe98c0100acf3af03a5cccefc6be191c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e48d68a156000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f00000000000000000000000000000000000000000000000000000000000001e300000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002cc02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000064dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a4d92aadfb000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000338c502e4ef476d74fc67d8cea95b481f4e975c16830e9fe01fa90936243fa280e0e142a850000000000000000000000000000000000000000000000000000000068af6e230000000000000000000000009ba0cf1588e1dfa905ec948f7fe5104dd40eda310000000000000000000000000000000000000000000000000000000000000120000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000029ec27e95efe1600000000000000000000000000000000000000000000000000000000000000000411b295299565fb08fd9be2b738e26dbd9994e103e92a6f2150c9f67d2cc8f5d5fb77d671ed2fac0d3a3ed329f266e1c024b3087a7243eb8691f6cd7a3d119aaa811000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a4d92aadfb000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000e38a26f2517fea359c23a9e4fde7aa351e4df92073c32806deaab6f75496802f6a6306060000000000000000000000000000000000000000000000000000000068af6e1e000000000000000000000000a8020ecbc321e0c8cea26b3507f207482d0100c20000000000000000000000000000000000000000000000000000000000000120000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000b90984060d355800000000000000000000000000000000000000000000000000000000000000041c5eeddf7f2b1f5845bcf7100050015f4326600227654b97b379c61680c6399d83f8dc1415f1bbdfb6bbbb9fc60b30662db3a01c683184df22ef3949d7bb5c37f1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001646c5f9cf9000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000176f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000ffffffffffffffc5000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037271001a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000053e2d6238da300000032000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000164af72634f000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000ffffffffffffffc5000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034271001a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000a0000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084bfda5292000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000000000000000000000000000000000000000271000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000064c876d21d000000000000000000000000f5c4f3dc02c3fb9279495a8fef7b0741da9561570000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000060ff73028b9714b106c800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012438c9c1470000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000ad01c20d5886137e056775af56915de824c8fce500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffc1fb425e000000000000000000000000df31a70a21a1931e02033dbba7deace6c45cfd0f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000056bc75e2d631000000000000000000000000000000000000000006e898131631616b1779bad70bcda0000000000000000000000000000000000000000000000000000000068af6f0d00000000000000000000000000000000000000000000000000000000000000c0",
"gas": "1138152",
"gasPrice": "1198827734",
"value": "0"
},
"zid": "0x777155a982e669283398f5bd"
}

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 response.

// Sign permit2.eip712 returned from quote
let signature: Hex;
signature = 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
import { concat, numberToHex, size } from 'viem';

if (permit2?.eip712) {
const signature = await signTypedDataAsync(permit2.eip712);
const signatureLengthInHex = numberToHex(size(signature), {
signed: false,
size: 32,
});
transaction.data = concat([transaction.data, signatureLengthInHex, signature]);
}

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.

const signedTransaction = await client.signTransaction({
account: client.account,
chain: client.chain,
gas: !!quote?.transaction.gas ? BigInt(quote?.transaction.gas) : undefined,
to: quote?.transaction.to,
data: quote.transaction.data,
gasPrice: !!quote?.transaction.gasPrice ? BigInt(quote?.transaction.gasPrice) : undefined,
nonce: nonce,
});

const hash = await client.sendRawTransaction({
serializedTransaction: signedTransaction,
});

See also

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