Skip to main content
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.
For production use, you’ll need an API key to avoid rate limits. See the API Keys guide for details on obtaining and using an API key.
1

Request a Quote

DFlow Swap API returns a quote specific to the token pair, amount, slippage tolerance, platform fee and other parameters.A route is calculated for the swap at this step and returned along with the quote.
const SOL = "So11111111111111111111111111111111111111112";
const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";

/// Amount of SOL to swap to USDC
const amount = 1_000_000_000; // 1 SOL

/// Slippage tolerance in bps
const slippageBps = 20;

/// Base URL for the DFlow Swap API
const AGGREGATOR_API_BASE_URL = "https://quote-api.dflow.net";
const API_KEY = process.env.DFLOW_API_KEY; // Optional
const queryParams = new URLSearchParams();
queryParams.append("inputMint", SOL);
queryParams.append("outputMint", USDC);
queryParams.append("amount", amount.toString());
queryParams.append("slippageBps", slippageBps.toString());

const headers: HeadersInit = {};
if (API_KEY) {
  headers["x-api-key"] = API_KEY;
}

const quoteResponse = await fetch(
  `${AGGREGATOR_API_BASE_URL}/quote?${queryParams.toString()}`,
  { headers }
).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.
const requestBody = {
  userPublicKey: keypair.publicKey.toBase58(),
  dynamicComputeUnitLimit: true,
  prioritizationFeeLamports: 150000, // Get this value from a priority fee API
  quoteResponse: quoteResponse,
};

const headers: HeadersInit = { "content-type": "application/json" };
if (API_KEY) {
  headers["x-api-key"] = API_KEY;
}

const swapResponse = await fetch(`${AGGREGATOR_API_BASE_URL}/swap`, {
  method: "POST",
  headers,
  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.
const swapTransactionBuffer = Buffer.from(
  swapResponse.swapTransaction,
  "base64"
);
const transaction = VersionedTransaction.deserialize(swapTransactionBuffer);
transaction.sign([keypair]);
await sendAndConfirmTransaction(connection, transaction);