Adding Fees#
The OKX DEX API supports configuring referral fees and fee-receiving addresses for token swaps
You can extend the implementation to include fee parameters in your swap quotes and charge up to 3% per swap from your users for most supported networks; while for Solana you can charge up to 10% per swap.
The OKX DEX API plans to take a certain percentage of the referral fee charged by you to your users. For details, please visit the API fee page.
// Extended quoteParams with fee support
const quoteParams = {
chainId: SOLANA_CHAIN_ID,
amount: rawAmount,
fromTokenAddress,
toTokenAddress,
slippage: "0.005", // 0.5% slippage
userWalletAddress: userAddress,
// Fee-related parameters
fromTokenReferrerWalletAddress: "Your_REFERRER_WALLET_ADDRESS", // Optional: fee receiving address based on fromToken
toTokenReferrerWalletAddress: "Your_REFERRER_WALLET_ADDRESS", // Optional: fee receiving address based on toToken
feePercent: "1.5", // Optional: referrer fee percentage (max 9 decimal points)
} as Record<string, string>;
Important Fee Configuration Notes for feePercent parameter:
- Min percentage > 0. Max percentage: 10 for Solana, 3 for all other chains
- Maximum 9 decimal points, E.g. 1.3269018736% is the actual input, but the final calculation will only adopt 1.326901873%
- For Solana, the fee receiving address must have some SOL deposited for activation
- Each transaction can only choose referrer fee from either the fromToken or the toToken
Example Usage with Fees:
// .. Previous code implementation
// Get swap quote
const quoteParams = {
chainId: SOLANA_CHAIN_ID,
amount: rawAmount,
fromTokenAddress,
toTokenAddress,
slippage: "0.005", // 0.5% slippage
userWalletAddress: userAddress,
// Additional Fee params
fromTokenReferrerWalletAddress: "fee-recipient-wallet-address",
feePercent: "1",
// The wallet addresses to receive the referrer fee (Each transaction can only choose referrer fee from either the fromToken or the toToken)
// toTokenReferrerWalletAddress: "Fee receiving address,
// fromTokenReferrerWalletAddress: "Fee receiving address",
} as Record<string, string>;
const timestamp = new Date().toISOString();
const requestPath = "/api/v5/dex/aggregator/swap";
const queryString = "?" + new URLSearchParams(quoteParams).toString();
const headers = getHeaders(timestamp, "GET", requestPath, queryString);
const response = await fetch(
`https://web3.okx.com${requestPath}${queryString}`,
{ method: "GET", headers }
);
const data = await response.json();
// .. Continue code implementation
Command Line Usage with Fees:
# Example: Swap .01 SOL to USDC with 1.5% referrer fee
npx ts-node swap.ts .01 11111111111111111111111111111111 EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v --referrer YOUR_FEE_RECEIVING_ADDRESS --fee 1.5
Fee Calculation Example: For a trade of 100 USDC with a 1.5% fee:
- Fee amount: 1.5 USDC (1.5% of 100 USDC)
- Actual swap amount: 98.5 USDC
- The fee (1.5 USDC) will be sent to the fee receiving address