SWAP Token Pairs
This guide will explain how to SWAP tokens using the Wallet-API.
Due to the complexity of on chains swap performed across multiple decentralized exchanges the SWAP features are currently only supported on MAINNET (Production)
Token swapping is available on the Professional plan and higher. On other plans, the swap endpoints return
403with error codeinsufficient-permissions.
Different steps to perform a Token Swap

Token swap flow
The SWAP endpoints support the following chains:
- Ethereum
- Polygon (MATIC)
- Arbitrum
- Base
- BSC (Binance Smart Chain)
- VeChain
- Avalanche: only AVAX ⇄ WAVAX (wrapping and unwrapping)
1. Retrieve Token Pairs
The first step is to get a list of all the supported token pairs. The result varies according to the blockchain on which the wallet (walletId) is hosted.
If you feel you are missing a token pair, please contact us and we will look into making that available for you.
The token pairs are automatically filtered based on the blockchain that hosts the wallet and on the wallet's balances: only pairs whose source token the wallet currently holds are returned. For example, a wallet on Ethereum that only holds USDC will only get Ethereum pairs that start from USDC.
For pairs that start from the native token (e.g. POL on Polygon), the wallet needs a native balance. A wallet without any balance returns an empty list.
Call the following endpoint to get SWAP pairs:
Request Endpoint: reference
GET /api/wallets/{id}/swaps/pairsPath Parameter:
{id} : This is the walletId. The SWAP pairs will be displayed based on the blockchain of this wallet.
Example Request:
GET /api/wallets/b97e9e8b-035c-40a0-bac0-96b07fc0444a/swaps/pairsThe wallet in this example is created on the Polygon (MATIC) blockchain.
Response Body:
{
"success": true,
"result": [
{
"from": {
"secretType": "MATIC",
"symbol": "MATIC",
"tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
},
"to": {
"secretType": "MATIC",
"symbol": "USDT",
"tokenAddress": "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"
}
},
{
"from": {
"secretType": "MATIC",
"symbol": "MATIC",
"tokenAddress": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
},
"to": {
"secretType": "MATIC",
"symbol": "AAVE",
"tokenAddress": "0xd6df932a45c0f255f85145f286ea0b292b21c90b"
}
}
]
}Response Parameters under result array
result array| Parameter | Description |
|---|---|
from | This object holds details about the source token you desire to swap for another, including it's blockchain, symbol, and token address. |
to | This object holds details about the destination token you desire to swap to, including it's blockchain, symbol, and token address. |
secretType | The blockchain on which the wallet exists, for example: MATIC. |
symbol | The symbol name of the token. |
tokenAddress | Token address is the location of the smart contract responsible for managing the balances of all token holders. |
Save the details of your desired token pair, as they will be used later in the guide.
2. Retrieve Exchange Rate
Now you should have information about your desired swap pair. The next step is to calculate the exchange rate of the pair.
Request Endpoint: reference
GET /api/swaps/ratesThis endpoint returns information about the expected result of the swap. It provides the expected output amount, as well gives information about the slippage, and fee involved for that specific swap.
Query Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
fromSecretType | string | ✅ | The blockchain of the source token. |
toSecretType | string | ✅ | The blockchain of the destination token. Must be the same as fromSecretType; cross-chain swaps are not supported. |
fromToken | string | ✅ | The token address of the source token, e.g. from the response of the previous step. For the native token, use 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee. |
toToken | string | ✅ | The token address of the destination token, e.g. from the response of the previous step. For the native token, use 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee. |
amount | number | ✅ | The amount of source tokens you want to swap, in token units (e.g. 1.5), not in the smallest unit (wei). |
orderType | string | ✅ | The type of order. Use SELL: you specify the amount you want to swap, and the response gives the expected output. |
Example Request:
GET /api/swaps/rates?fromSecretType=MATIC&toSecretType=MATIC&fromToken=0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee&toToken=0x348e62131fce2f4e0d5ead3fe1719bc039b380a9&amount=1&orderType=SELLResponse Body:
{
"success": true,
"result": {
"exchangeRates": [
{
"exchange": "ONE_INCH",
"orderType": "SELL",
"inputAmount": 1.000000000000000000,
"outputAmount": 0.773096508478042589,
"slippage": 0.05,
"fee": 3.0
}
],
"bestRate": {
"exchange": "ONE_INCH",
"orderType": "SELL",
"inputAmount": 1.000000000000000000,
"outputAmount": 0.773096508478042589,
"slippage": 0.05,
"fee": 3.0
}
}
}In the result object you will find two more objects:
exchangeRates: This will give you the exchange rate of a specific exchange.bestRate: This will give you the best exchange rate out of all the exchanges listed.
Parameters Explained:
| Parameter | Description |
|---|---|
exchange | The exchange that will carry out the swap. Pass this value as exchange when you build the swap. |
orderType | The type of order: SELL. |
inputAmount | The amount of source tokens you want to swap. |
outputAmount | The expected amount of destination tokens you will receive, with the service fee already deducted. The amount you actually receive can be lower, by at most the slippage. |
slippage | The maximum allowed difference between the expected outputAmount and the amount actually received, as a fraction (e.g. 0.05 = 5%). It is set by Venly, may differ per exchange, and cannot be changed per request. |
fee | The service fee for this swap, as a percentage (e.g. 3.0 = 3%), already reflected in outputAmount. It does not include gas: gas is paid separately by the source wallet, in the chain's native token, when the swap transaction is executed. |
Save the details of the
bestRate, as they will be used later in the guide.
3. Build Swap Transaction
The next step is to build the swap transaction. The endpoint will return the transactions that are needed to perform the actual swap, based on information obtained from the previous, retrieve exchange rate endpoint.
This API endpoint will construct transactions for you. Subsequently, you or your user will be required to execute these transactions one by one.
Request Endpoint: reference
POST /api/wallets/{id}/swapsPath Parameter:
{id} : This is the ID of the source wallet. (Wallet that holds tokens to swap)
Request Body:
| Parameter | Type | Required | Description |
|---|---|---|---|
walletId | string | ✅ | ID of the source wallet (the wallet that holds the tokens to swap). Use the same ID as {id} in the path. |
destinationWalletId | string | ✅ | ID of the wallet that receives the destination tokens. For most exchanges, the destination tokens are received by the source wallet; set this to the same value as walletId. |
fromSecretType | string | ✅ | The blockchain of the source token. |
toSecretType | string | ✅ | The blockchain of the destination token. Must be the same as fromSecretType. |
fromToken | string | ✅ | The token address of the source token, as used in the rates request. |
toToken | string | ✅ | The token address of the destination token, as used in the rates request. |
inputAmount | number | ✅ | The amount of source tokens to swap, in token units. Use the same value as amount in the rates request. |
outputAmount | number | ✅ | The expected amount of destination tokens. Use the outputAmount from the rates response. |
orderType | string | ✅ | The type of order: SELL. |
exchange | string | ✅ | The exchange to use, taken from the rates response (e.g. the exchange of bestRate). |
enableGasEstimate | boolean | ❌ | Default false. When true, gas is estimated while the swap is built and returned in the gas field. This fails while a token approval is still needed (see step 4). Leave it false to have gas estimated when each transaction is executed. |
Example Request:
POST /api/wallets/b97e9e8b-035c-40a0-bac0-96b07fc0444a/swaps{
"walletId" : "b97e9e8b-035c-40a0-bac0-96b07fc0444a",
"destinationWalletId": "b97e9e8b-035c-40a0-bac0-96b07fc0444a",
"fromSecretType": "MATIC",
"toSecretType": "MATIC",
"fromToken": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
"toToken": "0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
"inputAmount": "1",
"outputAmount": "2.038411",
"orderType": "SELL",
"exchange": "ONE_INCH",
"enableGasEstimate": false
}Response Body:
{
"success": true,
"result": [
{
"walletId": "b97e9e8b-035c-40a0-bac0-96b07fc0444a",
"gasPrice": 20000000000,
"gas": null,
"nonce": null,
"value": 1000000000000000000,
"to": "0x11111112542d85b3ef69ae05771c2dccff4faa26",
"network": null,
"data": "0x7c0252000000000000000000000000000f85a912448279111694f4ba4f85dc641c54b59400000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f0000000000000000000000000f85a912448279111694f4ba4f85dc641c54b594000000000000000000000000150f50e0d321f4607d589f939a28c5f10b13d7b90000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000001d66ab00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f99000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000001fb53eb183b86582176f6aa8f4db72b62caf0d4b000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf127000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d7621dc5821000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004d0e30db000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4b3af37c0000000000000000000000000000000000000000000000000000000000000008080000000000000000000000000000000000000000000000000000000000000440000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf1270000000000000000000000000000000500000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f990000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf127000000000000000000000000055ff76bffc3cdd9d5fdbbc2ece4528ecce45047e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a4c9f12e9d00000000000000000000000055ff76bffc3cdd9d5fdbbc2ece4528ecce45047e0000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf1270000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f0000000000000000000000030f85a912448279111694f4ba4f85dc641c54b594000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4b3af37c000000000000000000000000000000000000000000000000000000000000000808000000000000000000000000000000000000000000000000000000000000044000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f000000000000000000000000000000010000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f99000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f000000000000000000000000150f50e0d321f4607d589f939a28c5f10b13d7b900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "MATIC_TRANSACTION"
}
]
}4. Execute SWAP
This is the last step in the swap process. This step contains the transactions that will execute the swap.
The previous endpoint returns the transactions that need to be executed to complete the swap. Execute them one after another, in the same order, never in parallel. Venly manages the nonce, so they are processed on-chain in the right order.
Token approvalsWhen you swap an ERC-20 token, the exchange contract needs permission to spend it. If the wallet's current allowance is too low, the build step automatically adds an approval transaction before the swap transaction:
- If the wallet already has a non-zero allowance, an approval that resets it to
0.- An approval for an unlimited amount.
- The swap transaction.
Because the approval is unlimited, this only happens the first time a wallet swaps a given token. After that, the build response contains just the swap transaction.
Wait for the approval to be confirmedWhen the list contains an approval, wait until it is confirmed on-chain before you execute the swap transaction. The swap's gas is estimated when it is executed, and that only works once the approval is on-chain. You can check this with
GET /api/transactions/{secretType}/{transactionHash}/status, using thetransactionHashreturned when you executed the approval.
Execute Transactions
The final step is executing the transactions. Send each transaction from the build response exactly as returned as the request body, including its type (e.g. MATIC_CONTRACT_EXECUTION for an approval, MATIC_TRANSACTION for the swap). Don't wrap it in another object. Pass the signing method in the Signing-Method header.
In our example, the build response contains only one transaction, so we execute it once.
Request Endpoint: reference
POST /api/transactions/executeIn the header request, the
Signing-Methodparameter is to be passed as detailed below in the example request.
Example Request:
POST /api/transactions/execute| Parameter | Param Type | Value | Description | Example Value |
|---|---|---|---|---|
Signing-Method | Header | id:value | id: This is the ID of the signing methodvalue: This is the value of the signing method | 756ae7a7-3713-43ee-9936-0dff50306488:123456 |
{
"walletId": "b97e9e8b-035c-40a0-bac0-96b07fc0444a",
"gasPrice": 20000000000,
"gas": null,
"nonce": null,
"value": 1000000000000000000,
"to": "0x11111112542d85b3ef69ae05771c2dccff4faa26",
"network": null,
"data": "0x7c0252000000000000000000000000000f85a912448279111694f4ba4f85dc641c54b59400000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000180000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f0000000000000000000000000f85a912448279111694f4ba4f85dc641c54b594000000000000000000000000150f50e0d321f4607d589f939a28c5f10b13d7b90000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000001d66ab00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000004e0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f99000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000001fb53eb183b86582176f6aa8f4db72b62caf0d4b000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf127000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d7621dc5821000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000004d0e30db000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4b3af37c0000000000000000000000000000000000000000000000000000000000000008080000000000000000000000000000000000000000000000000000000000000440000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf1270000000000000000000000000000000500000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f990000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf127000000000000000000000000055ff76bffc3cdd9d5fdbbc2ece4528ecce45047e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a4c9f12e9d00000000000000000000000055ff76bffc3cdd9d5fdbbc2ece4528ecce45047e0000000000000000000000000d500b1d8e8ef31e21c99d1db9a6444d3adf1270000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f0000000000000000000000030f85a912448279111694f4ba4f85dc641c54b594000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000001a4b3af37c000000000000000000000000000000000000000000000000000000000000000808000000000000000000000000000000000000000000000000000000000000044000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f000000000000000000000000000000010000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000064d1660f99000000000000000000000000c2132d05d31c914a87c6611c10748aeb04b58e8f000000000000000000000000150f50e0d321f4607d589f939a28c5f10b13d7b900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"type": "MATIC_TRANSACTION"
}Response Body:
{
"success": true,
"result": {
"transactionHash": "0x8c953e09d8cede9f4eb0d1ee96de4f5a99e31dba7e64312bb252a465de12d10d"
}
}Gas
Each transaction (approval and swap) costs gas, paid by the source wallet in the chain's native token (e.g. POL on Polygon, ETH on Ethereum), when the transaction is executed. Make sure the wallet holds enough of it, on top of the amount you're swapping when the source token is the native token.
Execute the transactions soon after building them. The swap transaction can contain gas prices that were set when it was built. If you execute it much later and network fees have risen, it may be slow to confirm. In that case, build the swap again.
This needs to be done for each transaction that was returned from Step 3. Build Swap Transaction, in the same order.
Updated 2 days ago