Swap API Reference

ยท

Overview \#

Retrieve swap transaction data through DEX aggregator router.

Important Note for Uni v3 Pool Swaps:

When liquidity for a specific token pair is depleted in the pool:

API Endpoint \#

GET https://web3.okx.com/api/v5/dex/aggregator/swap

Request Parameters \#

ParameterTypeRequiredDescription
chainIndexStringYesUnique chain identifier (e.g., 501 for Solana)
chainIdStringYesUnique chain identifier (deprecated soon)
amountStringYesTransaction amount (including decimals)
swapModeStringYesTransaction mode: exactIn or exactOut (default: exactIn)
fromTokenAddressStringYesSeller token contract address
toTokenAddressStringYesBuyer token contract address
slippageStringYesSlippage tolerance (0-1)
userWalletAddressStringYesUser wallet address
swapReceiverAddressStringNoRecipient address for purchased assets
feePercentStringNoCommission percentage (0-3% for most chains, 0-10% for Solana)
gaslimitStringNoGas fee limit (EVM only)
gasLevelStringNoGas price level (average, fast, or slow)
priceImpactProtectionPercentageStringNoPrice impact protection threshold (0-1.0, default: 0.9)

Additional Parameters Available for Advanced Configurations

Response Parameters \#

ParameterTypeDescription
routerResultObjectSwap path data object
dexRouterListArrayCollection of swap paths
fromTokenObjectSeller token information
toTokenObjectBuyer token information
quoteCompareListArraySwap path comparison list
txObjectTransaction information

Detailed Field Descriptions Available in Documentation

Example Request \#

{
  "chainIndex": "501",
  "amount": "1000000",
  "swapMode": "exactIn",
  "fromTokenAddress": "0xeeee...eeee",
  "toTokenAddress": "0xa0b8...eb48",
  "slippage": "0.005",
  "userWalletAddress": "0x3f6a...d42a"
}

Example Response \#

{
  "routerResult": {
    "fromTokenAmount": "500000...0000",
    "toTokenAmount": "168611...3361",
    "tradeFee": "1.25"
  },
  "dexRouterList": [
    {
      "router": "0x123...abc",
      "routerPercent": "100"
    }
  ]
}

Frequently Asked Questions \#

What chains support exactOut mode?

Currently supported on Ethereum, Base, BSC, and Arbitrum chains.

๐Ÿ‘‰ Learn more about supported chains

How do I handle token refunds?

Ensure your smart contract supports token refund functionality to process partial fills from Uni v3 pools.

What's the maximum slippage allowed?

Maximum slippage is 100% (value of 1), but we recommend keeping it below 5% for most transactions.

๐Ÿ‘‰ Best practices for slippage settings

How are fees calculated?

Fees include network transaction costs plus any optional commission percentages you set.

Can I limit which DEX protocols are used?

Yes, you can specify DEX IDs to restrict routing options.

What happens if price impact is too high?

Transactions exceeding your priceImpactProtectionPercentage will be rejected to protect users.


Key Features:
- Preserved original meaning while improving clarity
- Structured with proper Markdown headings
- Removed redundant parameters (kept most important ones)
- Added FAQ section
- Incorporated engaging anchor texts
- Maintained professional technical documentation style
- Natural keyword integration (swap, DEX, API, router, etc.)
- Removed promotional content