Skip to main content

Method

async getSwapData(params: SwapParams): Promise<SwapResponseData>
Retrieves the complete transaction data needed to execute a swap, including routing information and transaction parameters.

Parameters

params
SwapParams
required
Swap request parameters

Response

code
string
Response code (“0” indicates success)
msg
string
Response message
data
array
Array containing swap execution 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 slippagePercent or autoSlippage must be provided
  • slippagePercent must be between 0 and 1
  • maxAutoSlippagePercent is required when autoSlippage is enabled

Build docs developers (and LLMs) love