Method
async getSwapData(params: SwapParams): Promise<SwapResponseData>
Parameters
Swap request parameters
Show properties
Show properties
Chain identifier (e.g., “1” for Ethereum, “501” for Solana)
Contract address of the token to swap from
Contract address of the token to swap to
Amount to swap (in token’s smallest unit)
Maximum acceptable slippage (0-1, e.g., “0.005” for 0.5%). Required if autoSlippage is false.
Enable automatic slippage calculation
Maximum slippage when autoSlippage is enabled (required with autoSlippage)
User’s wallet address
Address to receive swapped tokens (defaults to userWalletAddress)
Comma-separated list of DEX IDs to route through
Whether to use direct routing only
Maximum acceptable price impact (0-1)
Referral fee percentage
Referrer wallet address for source token fees
Referrer wallet address for destination token fees
Positive slippage percentage
Gas limit for the transaction (EVM chains)
Gas level: “low”, “medium”, or “high” (EVM chains)
Compute unit price (Solana only)
Compute unit limit (Solana only)
Memo data for the transaction
Response
Response code (“0” indicates success)
Response message
Array containing swap execution data
Show data properties
Show data properties
Routing and pricing information
Show properties
Show properties
Chain identifier
Source token information
Destination token information
Input token amount
Expected output token amount
Estimated gas fee
Price impact percentage
Trading fee amount
Router information
Swap mode identifier
List of DEX routers used
Context slot (Solana only)
Transaction data for execution
Show properties
Show properties
Sender address
Contract address to call
Encoded transaction data
Native token value to send
Gas limit
Gas price (legacy transactions)
Max priority fee (EIP-1559 transactions)
Minimum tokens to receive after slippage
Maximum tokens to spend
Applied slippage percentage
Additional signature data
Example
import { OKXClient } from '@okxweb3/okx-api';
const client = new OKXClient({
apiKey: 'your-api-key',
secretKey: 'your-secret-key',
apiPassphrase: 'your-passphrase',
projectId: 'your-project-id'
});
const swapData = await client.dex.getSwapData({
chainIndex: '1',
fromTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
toTokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
amount: '1000000000000000000', // 1 ETH
slippagePercent: '0.005',
userWalletAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e'
});
console.log('Transaction data:', swapData.data[0].tx);
console.log('Router result:', swapData.data[0].routerResult);
Response Example
{
"code": "0",
"msg": "",
"data": [{
"routerResult": {
"chainIndex": "1",
"fromTokenAmount": "1000000000000000000",
"toTokenAmount": "2487500000",
"estimateGasFee": "0.002",
"priceImpactPercent": "0.15"
},
"tx": {
"from": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"to": "0x1111111254fb6c44bAC0beD2854e76F90643097d",
"data": "0x12aa3caf...",
"value": "1000000000000000000",
"gas": "200000",
"gasPrice": "20000000000",
"minReceiveAmount": "2475037500"
}
}]
}
Validation
The method validates:- Either
slippagePercentorautoSlippagemust be provided slippagePercentmust be between 0 and 1maxAutoSlippagePercentis required whenautoSlippageis enabled