Imperative Swaps allow Solana users to trade tokens with full control over the route and transaction execution.
Using Imperative Swaps through DFlow Swap API is simple. DFlow Swap API
aggregates liquidity from all the major Solana DEXs and offers an API for trading any SPL token.
This quickstart assumes familiarity with Solana’s transaction and network connection logic.
If unfamiliar, please refer to the Solana Cookbook.
1
Request a Quote
DFlow Swap API returns a quote specific to the token pair, amount, slippage tolerance,
platform fee, segmenter fee, and other parameters.A route is calculated for the swap at this step and returned along with the quote.
Request a Quote
Copy
Ask AI
const SOL = "So11111111111111111111111111111111111111112";const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";/// Amount of SOL to swap to USDCconst amount = 1_000_000_000; // 1 SOL/// Slippage tolerance in bpsconst slippageBps = 20;/// Base URL for the DFlow Swap APIconst AGGREGATOR_API_BASE_URL = "https://quote-api.dflow.net";const queryParams = new URLSearchParams();queryParams.append("inputMint", SOL);queryParams.append("outputMint", USDC);queryParams.append("amount", amount.toString());queryParams.append("slippageBps", slippageBps.toString());const quoteResponse = await fetch(`${AGGREGATOR_API_BASE_URL}/quote?${queryParams.toString()}`).then(x => x.json());
2
Request a Swap Transaction for the Quote
Given the quote, DFlow Swap API returns an executable transaction that encodes the route that
was calculated for the quote.
Request a Swap Transaction for the Quote
Copy
Ask AI
const requestBody = { userPublicKey: keypair.publicKey.toBase58(), dynamicComputeUnitLimit: true, prioritizationFeeLamports: 150000, // Get this value from a priority fee API quoteResponse: quoteResponse,};const swapResponse = await fetch(`${AGGREGATOR_API_BASE_URL}/swap`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(requestBody),}).then(x => x.json());
3
Submit the Transaction
Sign and then send the swap transaction directly to a Solana RPC or transaction submission endpoint
of your choice.