> ## Documentation Index
> Fetch the complete documentation index at: https://pond.dflow.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Platform Fees

> How to implement platform fees with the DFlow Trading API

<Info>
  During development, you can use the [developer endpoints](/get-started/endpoints) without an API key. For production use, you'll need an [API key](/get-started/api-key) to avoid rate limits.
</Info>

<Steps>
  <Step title="Set up">
    Imports, env config, and the wallet used across every step.

    <AccordionGroup>
      <Accordion title="Imports, env, and wallet">
        ```typescript theme={null}
        import "dotenv/config";

        import { Keypair } from "@solana/web3.js";
        import bs58 from "bs58";

        const DFLOW_TRADE_API_URL = process.env.DFLOW_TRADE_API_URL ?? "https://dev-quote-api.dflow.net";
        const DFLOW_API_KEY = process.env.DFLOW_API_KEY; // optional; not needed for dev endpoints
        const DFLOW_SETTLEMENT_MINT = process.env.DFLOW_SETTLEMENT_MINT ?? "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"; // USDC

        const SOL_MINT = "So11111111111111111111111111111111111111112";

        const keypair = Keypair.fromSecretKey(
          bs58.decode(process.env.SOLANA_PRIVATE_KEY ?? "")
        );

        const headers: HeadersInit = {};
        if (DFLOW_API_KEY) headers["x-api-key"] = DFLOW_API_KEY;
        ```
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Fixed Fees with platformFeeBps">
    Use [`platformFeeBps`](/resources/trading-api/order/order#parameter-platform-fee-bps) to specify a fixed fee in basis points. Control which token the fee is collected from using [`platformFeeMode`](/resources/trading-api/order/order#parameter-platform-fee-mode).

    <AccordionGroup>
      <Accordion title="Basic Example">
        ```typescript theme={null}
        const queryParams = new URLSearchParams();
        queryParams.append("inputMint", SOL_MINT);
        queryParams.append("outputMint", DFLOW_SETTLEMENT_MINT);
        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(
          `${DFLOW_TRADE_API_URL}/order?${queryParams.toString()}`,
          { headers }
        ).then((x) => x.json());
        ```
      </Accordion>

      <Accordion title="Collect Fee from Input Mint">
        ```typescript theme={null}
        /// To collect fee from input mint instead, override the values from above with `.set()` (not `.append()`):
        queryParams.set("platformFeeMode", "inputMint");
        queryParams.set("feeAccount", "YourSOLTokenAccountAddress"); // Must match input mint
        ```
      </Accordion>

      <Accordion title="Other Endpoints">
        ```typescript theme={null}
        /// For /quote endpoint (imperative swaps)
        const quoteResponse = await fetch(
          `${DFLOW_TRADE_API_URL}/quote?${queryParams.toString()}`,
          { headers }
        ).then((x) => x.json());

        /// For /intent endpoint (declarative swaps)
        const intentResponse = await fetch(
          `${DFLOW_TRADE_API_URL}/intent?${queryParams.toString()}`,
          { headers }
        ).then((x) => x.json());
        ```
      </Accordion>
    </AccordionGroup>
  </Step>

  <Step title="Fee Account Setup">
    The [`feeAccount`](/resources/trading-api/order/order#parameter-fee-account) parameter is required and must match the fee mint. The account must exist before the trade executes.

    <Warning>
      The fee account must match the token you're collecting fees in: input or output mint, based on `platformFeeMode`.
    </Warning>

    <Note>
      For prediction market outcome token trades, fees use [`platformFeeScale`](/resources/trading-api/order/order#parameter-platform-fee-scale) instead, and the fee account must be a settlement mint token account. See [Add Platform Fees (Prediction Markets)](/prediction-markets/recipes/add-platform-fees).
    </Note>
  </Step>

  <Step title="Check Fee in Response">
    Both `/order` and `/quote` responses include `platformFee` information.

    <AccordionGroup>
      <Accordion title="Reading Platform Fee from Response">
        ```typescript theme={null}
        // Reuses the `orderResponse` from the request step above.
        if (orderResponse.platformFee) {
          console.log("Platform fee details:", {
            amount: orderResponse.platformFee.amount,
            feeBps: orderResponse.platformFee.feeBps,
            feeAccount: orderResponse.platformFee.feeAccount,
          });
        }
        ```
      </Accordion>
    </AccordionGroup>
  </Step>
</Steps>

## Related Resources

<CardGroup cols={2}>
  <Card title="Platform Fees" href="/spot/trading/platform-fees" icon="percent">
    How platform fees work, fee modes, and accepted denominations.
  </Card>

  <Card title="Send Orders" href="/spot/recipes/quickstart" icon="rocket">
    Send a spot order end-to-end.
  </Card>

  <Card title="Add Priority Fees" href="/spot/recipes/priority-fees" icon="bolt">
    Control execution speed with priority fees.
  </Card>

  <Card title="Authenticate Requests" href="/resources/recipes/api-keys" icon="key">
    Authenticate requests for production rate limits.
  </Card>
</CardGroup>

## API Routes

* [GET /order](/resources/trading-api/order/order)
* [GET /quote](/resources/trading-api/imperative/quote)
* [GET /intent](/resources/trading-api/declarative/quote)
