Skip to main content
This guide shows you how to implement platform fees on trades using the DFlow Trade API.
1

Fixed Fees with platformFeeBps

Use platformFeeBps to specify a fixed fee in basis points. Control which token the fee is collected from using platformFeeMode.
const SOL = "So11111111111111111111111111111111111111112";
const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
const API_BASE_URL = "https://quote-api.dflow.net";

const queryParams = new URLSearchParams();
queryParams.append("inputMint", SOL);
queryParams.append("outputMint", USDC);
queryParams.append("amount", "1000000000");
queryParams.append("platformFeeBps", "50"); // 0.5% fee
queryParams.append("platformFeeMode", "outputMint"); // Collect from output (USDC)
queryParams.append("feeAccount", "YourUSDCTokenAccountAddress");
queryParams.append("userPublicKey", keypair.publicKey.toBase58());

const orderResponse = await fetch(
  `${API_BASE_URL}/order?${queryParams.toString()}`
).then((x) => x.json());
/// To collect fee from input mint instead
queryParams.append("platformFeeMode", "inputMint");
queryParams.append("feeAccount", "YourSOLTokenAccountAddress"); // Must match input mint
/// For /quote endpoint (imperative swaps)
const quoteResponse = await fetch(
  `${API_BASE_URL}/quote?${queryParams.toString()}`
).then((x) => x.json());

/// For /intent endpoint (declarative swaps)
const intentResponse = await fetch(
  `${API_BASE_URL}/intent?${queryParams.toString()}`
).then((x) => x.json());
2

Dynamic Fees with platformFeeScale

For outcome token trades, use platformFeeScale instead of platformFeeBps. The fee is calculated dynamically based on market probability.
const SOL = "So11111111111111111111111111111111111111112";
const OUTCOME_TOKEN_MINT = "YourOutcomeTokenMint";
const API_BASE_URL = "https://quote-api.dflow.net";

const queryParams = new URLSearchParams();
queryParams.append("inputMint", SOL);
queryParams.append("outputMint", OUTCOME_TOKEN_MINT);
queryParams.append("amount", "1000000000");
queryParams.append("platformFeeScale", "50"); // k = 0.050 in formula k * p * (1 - p) * c
queryParams.append("feeAccount", "YourSettlementMintTokenAccountAddress");
queryParams.append("userPublicKey", keypair.publicKey.toBase58());

const orderResponse = await fetch(
  `${API_BASE_URL}/order?${queryParams.toString()}`
).then((x) => x.json());
For outcome token trades, platformFeeMode is ignored and the fee is always collected in the settlement mint. See the Platform Fees Overview for details on the dynamic fee formula.
3

Fee Account Setup

The feeAccount parameter is required and must match the fee mint. You can use referralAccount to auto-create the fee account if it doesn’t exist.
/// Add referralAccount to auto-create fee account if it doesn't exist
queryParams.append("referralAccount", "YourReferralAccountAddress");
/// Fee account will be derived from referral account using Referral program
The fee account must match the token you’re collecting fees in. For outcome token trades, use a settlement mint token account. For other trades, match the input or output mint based on platformFeeMode.
4

Check Fee in Response

Both /order and /quote responses include platformFee information.
const orderResponse = await fetch(
  `${API_BASE_URL}/order?${queryParams.toString()}`
).then((x) => x.json());

if (orderResponse.platformFee) {
  console.log("Platform fee details:", {
    amount: orderResponse.platformFee.amount,
    feeBps: orderResponse.platformFee.feeBps,
    feeAccount: orderResponse.platformFee.feeAccount,
  });
}